Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. function GetAutomationPSCredentialProxy ($Name) {
  2. #requires -module CredentialManagement
  3. try {
  4. Get-StoredCredentials -target $name -erroraction stop
  5. } catch [Management.Automation.RuntimeException] {
  6. $NotFoundErrorRegex = '-2147023728$'
  7. #Prompt to store if not found
  8. if ($PSItem -match $NotFoundErrorRegex) {
  9. $newCred = Get-Credential -UserName $name -Message 'Credential Not Found, please enter password to store it'
  10. Add-StoredCredentials -ErrorAction stop -UserName $Name -Password $newcred.GetNetworkCredential().Password
  11. return $newCred
  12. } else {
  13. write-error $PSItem
  14. }
  15. }
  16. }
  17. Set-Alias -Name 'Get-AutomationPSCredential' -Value 'GetAutomationPSCredentialProxy'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement