Working with Passwords and SecureStrings on Powershell


Folks below are some help with working on password on Powershell.

Most of this time, I have face issue using plain password while working with plink.exe, psexec.exe or robocopy. Below script help you to ‘input’ password in secure format and convert it in to plain format ot call it on a psexec.exe or other command lets.

#————————————————————————————–

# SecureString to PlainText (Password)

# Author : Vipin Vasudevan

#————————————————————————————–

function ConvertFrom-SecureToPlain {

param( [Parameter(Mandatory=$true)][System.Security.SecureString] $SecurePassword)

# Create a “password pointer”

$PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)

# Get the plain text version of the password

$PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)

# Free the pointer

[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)

# Return the plain text password

$PlainTextPassword

}

$password = Read-host “Please enter the password for store it in Secure” – AsSecureString

$PlainTextPassword = ConvertFrom-SecureToPlain $Password

#Read out the password as below

Write-host “Your password read as $PlainTextPassword”

#————————–Script End —————————

Other way around is calling the password as network credential using “get-Credential” and retrieve the password in text format, user or domain field if needed.

 

#————————————————————————————–

# Retrieve username and password out from PSCredentials get-credentials

# Author : Vipin Vasudevan

#————————————————————————————–

#Get credential using PSCredentials get-credential

$credential = Get-Credential

#retrieve the credential details

$username = $credential.GetNetworkCredential().UserName

$password = $credential.GetNetworkCredential().password

# get domain details if require using $domain = $credential.GetNetworkCredential().Domain

#Read out the user ID and password as below

Write-host “Your user account $username password as $password”

#————————–Script End —————————

 

Another big time need is creating Pscredentials out of plain text for accessing network resources or running privileged scripts using constructed the Pscredentials from plain text or secure text stored on some files!

First we need to create secure string using below command save it on a text format

$SecureString = $SecureStringAsPlainText  | ConvertTo-SecureString

Add-content -path \securepass.txt -value $SecureString

Next would be reading this encrypted the text to a variable and construct the Pscredential for running some scheduled jobs

 #————————————————————————————–

# Retrieve username and password out from PSCredentials get-credentials

# Author : Vipin Vasudevan

#————————————————————————————–

# Read the Secure string plain text in and convert to secure string

$SecureStringAsPlainText = Get-content -path \securepass.txt

$SecureString = $SecureStringAsPlainText  | ConvertTo-SecureString

$UserName = “YourDomain\UserID” # you can even read-host if you are not scheduling it

# Covert the plain text username and secure password to PS credentials

$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword

# you may use it as shown below

Get-WmiObject Win32_OperatingSystem -Computername -Credential $Credentials

#—————————————— Script End ————————————-

Advertisements

One thought on “Working with Passwords and SecureStrings on Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s