Last active
March 6, 2019 13:48
-
-
Save JohnLBevan/6fed2e2b5cf93188fde2 to your computer and use it in GitHub Desktop.
Get Current User's Credentials
This file contains hidden or 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
| 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