Use Secure password in your powershell code without a prompt

Use Secure password in your powershell code without a prompt


- Yesterday i was running a script where i had to type my credentials(Username and password) to get the mandatory results from it, now Normally if you want to use your credentials with one time prompt that will take your information and apply it where ever it is required, you can use somthing like this :






you can notice in that when you type : Get-Credential -UserName "Domain\UserName" -Message "Some Message" that you will get a prompt asking you for your password, once you put the password this credential can be use any where in the code when it's required, of course you can assign the CMDLET to a variable and you can use this variable anywhere you want, for example:

$cer = Get-Credential -UserName "Domain\UserName" -Message "Some Message"

now this is great if i am running a code every time by myself, but what if i want to schedule some code, and this code needs my Admin domain credentials, of course i cant be available every time the job needs to run, also i cant take the risk and put my password inside the code in a clear text or even put the password in another file and call it from there..



To fix this issues, PowerShell has it's own PScredentials which can be used in different ways to use my credentials securely without interacting with the script and put my password.


You can easily use this as following, first You have to save your password in a file securely like this :




This syntax will simply allow you to store your password in a file securely, if you open the file where you stored the password it will look something like this:



you can see that your password is secure a little, still that's not all, there are more coming, now once this is done, now you wanna see this is work, this is a small example :


$username = "domain01\admin01"
$password = cat C:\securestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

$serverNameOrIp = "192.168.1.1"
Restart-Computer -ComputerName $serverNameOrIp `
                 -Authentication default `
                 -Credential $cred
the above example was provided by this link :

in the above code, we called the file where the password was stored and transferred it into a secure string, when you do that you only need to do it once, after that your code will always call the password in your file in a secure string. than we used the "restart-computer" CMDLET to restart a remote PC.
also have a look at the "$cred" variable just as reference. 

sadly I gotta go now, I still have some work to do, Happy scripting everyone. Hasta luego todos...
 

Comments

  1. As long as you are aware that committing any credential or password to disk, even like this is not a security best practice. But this may be acceptable for some companies.

    ReplyDelete
  2. AWESOME will work well for me here.

    ReplyDelete

Post a Comment

Popular posts from this blog

IP Calculator in PowerShell...with IP exclusion