Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active March 6, 2019 13:48
Show Gist options
  • Select an option

  • Save JohnLBevan/6fed2e2b5cf93188fde2 to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/6fed2e2b5cf93188fde2 to your computer and use it in GitHub Desktop.
Get Current User's Credentials
if ($promptForCredentialsInInteractive)
{
$user = $env:USERDOMAIN, $env:USERNAME -join '\'
$cred = (get-credential -UserName $user -Message "Please enter the credentials you wish this script to use when accessing network resources")
Write-Verbose ("User entered username '{0}'" -f $cred.UserName)
}
else
{
#this doesn't work as some people claim
#$cred = [System.Net.CredentialCache]::DefaultCredentials
#however you can store an account's password in an enctrypted file
#and use that. The file can only be decrypted by the user
#who created the enctrypted file in the first place.
$passwordFile = '.\eu_myserviceaccount.txt' #http://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password
$username = 'eu\myserviceaccount'
#read-host -assecurestring | convertfrom-securestring | out-file $passwordFile #use this under the same user who'll be running the script to create secure file
$password = get-content $passwordFile | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -ArgumentList $username,$password
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment