Created
June 12, 2019 10:44
-
-
Save roe3p/e8f21f5af51a351c21b6437e72bba896 to your computer and use it in GitHub Desktop.
Use Stored Password to Invoke a PS command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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