Skip to content

Instantly share code, notes, and snippets.

@roe3p
Created June 12, 2019 10:44
Show Gist options
  • Save roe3p/e8f21f5af51a351c21b6437e72bba896 to your computer and use it in GitHub Desktop.
Save roe3p/e8f21f5af51a351c21b6437e72bba896 to your computer and use it in GitHub Desktop.
Use Stored Password to Invoke a PS command
## Use Windows Credentials encrypted and stored in a file to invoke a PS command. Using the concept at:
## https://interworks.com/blog/trhymer/2013/07/08/powershell-how-encrypt-and-store-credentials-securely-use-automation-scripts
## Useful to run in situations where double-hop authentication is a problem
$Username = 'p_service_account'
$passfile = 'D:\scripts\p_service_account_password.txt'
$encrypted = Get-Content $passfile | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($Username, $encrypted)
$ServerName = 'SVR01.mydomain.com'
$file = 'e:\Backup\MyCubeBackup.abf'
$code =
{
## Example code to run remotely
(Get-Item $Using:file).length/1MB)
}
invoke-command -ScriptBlock $code -ComputerName $ServerName -Credential $Cred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment