Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-CachedCredential
  2.  {
  3.     <#
  4.         .SYNOPSIS
  5.             Return a list of cached credentials
  6.         .DESCRIPTION
  7.             This function wraps cmdkey /list and returns an object that contains
  8.             the Targetname, user and type of cached credentials.
  9.         .PARAMETER Type
  10.             To filter the list provide one of the basic types of  
  11.             cached credentials
  12.                 Domain
  13.                 Generic
  14.         .EXAMPLE
  15.             Get-CachedCredential
  16.  
  17.             Target                         Type            User                  
  18.             ------                         ----            ----                  
  19.             Domain:target=server-02        Domain Password COMPANY\Administrator
  20.             Domain:target=server-01        Domain Password COMPANY\Administrator
  21.             LegacyGeneric:target=server-04 Generic         COMPANY\Administrator
  22.             LegacyGeneric:target=server-03 Generic         COMPANY\Administrator
  23.  
  24.             Description
  25.             -----------
  26.             This example shows using the syntax without passing a type parameter, which is
  27.             the same as passing -Type All
  28.         .EXAMPLE
  29.             Get-CachedCredential -Type Domain
  30.  
  31.             Target                         Type            User                  
  32.             ------                         ----            ----                  
  33.             Domain:target=server-02        Domain Password COMPANY\Administrator
  34.             Domain:target=server-01        Domain Password COMPANY\Administrator
  35.  
  36.             Description
  37.             -----------
  38.             This example shows using type with one of the valid types available
  39.         .NOTES
  40.             FunctionName : Get-CachedCredential
  41.             Created by   : jspatton
  42.             Date Coded   : 06/23/2014 10:11:42
  43.  
  44.             **
  45.             This function does not return a cached credential that doesn't hold
  46.             a value for user
  47.             **
  48.         .LINK
  49.             https://code.google.com/p/mod-posh/wiki/CachedCredentialManagement#Get-CachedCredential
  50.         .LINK
  51.             http://technet.microsoft.com/en-us/library/cc754243.aspx
  52.         .LINK
  53.             http://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/
  54.     #>
  55.     [CmdletBinding()]
  56.     Param
  57.         (
  58.         [ValidateSet("Generic","Domain","Certificate","All")]
  59.         [string]$Type
  60.         )
  61.     Begin
  62.     {
  63.         $Result = cmdkey /list
  64.         }
  65.     Process
  66.     {
  67.         $Return = @()
  68.         $Temp = New-Object -TypeName psobject
  69.         foreach ($Entry in $Result)
  70.         {
  71.             if ($Entry)
  72.             {
  73.                 $Line = $Entry.Trim();
  74.                 if ($Line.Contains('Target: '))
  75.                 {
  76.                     Write-Verbose $Line
  77.                     $Target = $Line.Replace('Target: ','');
  78.                     }
  79.                 if ($Line.Contains('Type: '))
  80.                 {
  81.                     Write-Verbose $Line
  82.                     $TargetType = $Line.Replace('Type: ','');
  83.                     }
  84.                 if ($Line.Contains('User: '))
  85.                 {
  86.                     Write-Verbose $Line
  87.                     $User = $Line.Replace('User: ','');
  88.                     Add-Member -InputObject $Temp -MemberType NoteProperty -Name Target -Value $Target
  89.                     Add-Member -InputObject $Temp -MemberType NoteProperty -Name Type -Value $TargetType
  90.                     Add-Member -InputObject $Temp -MemberType NoteProperty -Name User -Value $User
  91.                     $Return += $Temp;
  92.                     Write-Verbose $Temp;
  93.                     $Temp = New-Object -TypeName psobject
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     End
  99.     {
  100.         if ($Type -eq "All" -or $Type -eq "")
  101.         {
  102.             Write-Verbose "ALL"
  103.             return $Return;
  104.             }
  105.         else
  106.         {
  107.             Write-Verbose "FILTERED"
  108.             if ($Type -eq "Domain")
  109.             {
  110.                 $myType = "Domain Password"
  111.                 }
  112.             if ($Type -eq "Certificate")
  113.             {
  114.                 $myType = "Generic Certificate"
  115.                 }
  116.             return $Return |Where-Object -Property Type -eq $myType
  117.             }
  118.         }
  119.     }
  120. Function Add-CachedCredential
  121. {
  122.     <#
  123.         .SYNOPSIS
  124.             Add a cached credential to the vault
  125.         .DESCRIPTION
  126.             This function wraps cmdkey /add and stores a TargetName and
  127.             user/pass combination in the vault
  128.         .PARAMETER TargetName
  129.             The name of the object to store credentials for, typically
  130.             this would be a computer name
  131.         .PARAMETER Type
  132.             Add credentials in one of the few valid types of
  133.             cached credentials
  134.                 Domain
  135.                 Generic
  136.         .PARAMETER Credential
  137.             A PSCredential object used to securely store user and  
  138.             password information
  139.         .EXAMPLE
  140.             Add-CachedCredential -TartName server-01 -Type Domain -Credential (Get-Credential)
  141.  
  142.             CMDKEY: Credential added successfully.
  143.  
  144.             Description
  145.             -----------
  146.             The basic syntax of the command
  147.         .EXAMPLE
  148.             "server-04","server-05" |Add-CachedCredential -Type Domain -Credential $Credential
  149.  
  150.             CMDKEY: Credential added successfully.
  151.  
  152.             CMDKEY: Credential added successfully.
  153.  
  154.             Description
  155.             -----------
  156.             This example shows passing in Targetnames on the pipeline
  157.         .NOTES
  158.             FunctionName : Add-CachedCredential
  159.             Created by   : jspatton
  160.             Date Coded   : 06/23/2014 12:13:21
  161.         .LINK
  162.             https://code.google.com/p/mod-posh/wiki/CachedCredentialManagement#Add-CachedCredential
  163.         .LINK
  164.             http://technet.microsoft.com/en-us/library/cc754243.aspx
  165.         .LINK
  166.             http://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/
  167.     #>
  168.     [CmdletBinding()]
  169.     Param
  170.         (
  171.         [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
  172.         [string]$TargetName,
  173.         [ValidateSet("Generic","Domain")]
  174.         [string]$Type,
  175.         [Parameter(Mandatory=$true)]
  176.         [System.Management.Automation.PSCredential]$Credential
  177.         )
  178.     Begin
  179.     {
  180.         $Username = $Credential.UserName;
  181.         $Password = $Credential.GetNetworkCredential().Password;
  182.         }
  183.     Process
  184.     {
  185.         foreach ($Target in $TargetName)
  186.         {
  187.             switch ($Type)
  188.             {
  189.                 "Generic"
  190.                 {
  191.                     $Result = cmdkey /generic:$Target /user:$Username /pass:$Password
  192.                     if ($LASTEXITCODE -eq 0)
  193.                     {
  194.                         Return $Result;
  195.                         }
  196.                     {
  197.                         Write-Error $Result
  198.                         Write-Error $LASTEXITCODE
  199.                         }
  200.                     }
  201.                 "Domain"
  202.                 {
  203.                     $Result = cmdkey /add:$Target /user:$Username /pass:$Password
  204.                     if ($LASTEXITCODE -eq 0)
  205.                     {
  206.                         Return $Result;
  207.                         }
  208.                     {
  209.                         Write-Error $Result
  210.                         Write-Error $LASTEXITCODE
  211.                         }
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.     End
  217.     {
  218.         }
  219.     }
  220. Function Remove-CachedCredential
  221. {
  222.     <#
  223.         .SYNOPSIS
  224.             Remove a target from the vault
  225.         .DESCRIPTION
  226.             This function wraps cmdkey /delete to remove a specific
  227.             target from the vault
  228.         .PARAMETER TargetName
  229.             The target to remove
  230.         .EXAMPLE
  231.             Remove-CachedCredential -TargetName server-04
  232.  
  233.             CMDKEY: Credential deleted successfully.
  234.  
  235.             Description
  236.             -----------
  237.             This example shows the only usage for this command
  238.         .NOTES
  239.             FunctionName : Remove-CachedCredential
  240.             Created by   : jspatton
  241.             Date Coded   : 06/23/2014 12:27:18
  242.         .LINK
  243.             https://code.google.com/p/mod-posh/wiki/CachedCredentialManagement#Remove-CachedCredential
  244.         .LINK
  245.             http://technet.microsoft.com/en-us/library/cc754243.aspx
  246.         .LINK
  247.             http://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/
  248.     #>
  249.     [CmdletBinding()]
  250.     Param
  251.         (
  252.         [Parameter(Mandatory=$true)]
  253.         [string]$TargetName
  254.         )
  255.     Begin
  256.     {
  257.         }
  258.     Process
  259.     {
  260.         $Result = cmdkey /delete:$TargetName
  261.         }
  262.     End
  263.     {
  264.         if ($LASTEXITCODE -eq 0)
  265.         {
  266.             Return $Result;
  267.             }
  268.         {
  269.             Write-Error $Result
  270.             Write-Error $LASTEXITCODE
  271.             }
  272.         }
  273.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement