SHARE
TWEET

Untitled

a guest Sep 17th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top