Skip to main content

Posts

Showing posts with the label code

Check and Create a Node in XML, if it does not existing using PowerShell

Here is a quick code snippet to Add a Node in XML from from the XPath, the following PowerShell function will search for the node hierarchy, if it does not exist then it will create it. function   CheckForXMLNode  {      param  (          $File , # File Path of XML to search the node          $NodePath     )      $nodes  =  $NodePath  -split  "/"      $path  =  "//Configuration"      # My Sample Node Path is like this # //Configuration/Farm/CustomParent/CustomChild      foreach  ( $node   in   $nodes ) {          if  ( ( $node  -ne  "" ) -and ( $node  -ne  "Configuration" ) ) {   ...

PowerShell: Get Strong Random Password

function GetRandomPassword { $ strSrc = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$^&(){}[],. ' $ max = $ strSrc.Length $ strPass = '' for ($ i = 0 ; $ i -lt 20 ; $ i += 1 ) { $ index = Get-Random - Minimum 0 - Maximum $ max $ c = $ strSrc [$ index ] $ strPass += $ c } return $ strPass } create the above function in your powershell profile and you can generate random password at anytime. Happy Coding!!

Save User Credentials in Secured File

If you want to save credentials in a file and later want to use it in a script here is the trick. You can use  PowerShell's built-in XML serialization (Clixml): #1. Get The Credentials $cred = Get-Credentails #2. Save the Credentails as a file $cred | Export-CliXml "MyFileName.xml" #3. Read the Credentials back from file $myCred = Import-CliXml "<Path>/MyFileName.xml"

MS-DOS and INI file

I came into a requirement where I had to separate a DOS batch file and move all the variable and user specific setting into a separate file. So here is the solution: I created a new INI config file and declared my available in it. MyINIFile.ini UserName=MyUserName Datapath=C:\data ServerName=MyServer Here is how I am calling the INI file inside the batch file. MyBatchFile.bat FOR /F "tokens=1,2 delims=^=" %%A IN (MyINIFile.ini) DO (SET %%A=%%B ) ECHO %Datapath% ECHO %ServerName%