Functions/New-PSCredential.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<#
.SYNOPSIS Create a new PowerShell Credential object with a random password. Encrypts and saves the Credential object to file or to Azure KeyVault. .DESCRIPTION New-PSCredential is used to create a new PowerShell Credential object [System.Management.Automation.PSCredential] with the provided username and a strong random password. The resulting credential object is returned as well as saved to disk or to Azure KeyVault so that it can be retrieved later. When saving to disk, the password is encrypted with either a pre-shared key or PKI certificate. .PARAMETER Username Username to use for the Credential to be created. .PARAMETER Path Path to the JSON file that will be created to save the encrypted credential. .PARAMETER SecureKey A SecureString that is used as a Pre-Shared-Key for encrypting the credential password. .PARAMETER Thumbprint The ThumbPrint of a certificate on the local computer that will be used to encrypt the credential password. .PARAMETER CertificateFile Path to a .CER certificate public key file that will be used to encrypt the credential password. .PARAMETER CertificateStore Specifies the certifcate store of the specified certificate thumbprint. Either LocalMachine or CurrentUser. .PARAMETER KeyVault The name of the Azure KeyVault that will be used to store the exported credential. .PARAMETER SecretName The name of the Azure KeyVault secret to create that will be used to store the exported credential. .EXAMPLE $Credential = New-PSCredential -Username 'svc.SharePoint.farm' -Path ./savedcredential.json -Thumbprint '87BB70A19A7671D389F49AF4C9608B2F381FDD80' New-ADUser -Name $Credential.Username -AccountPassword $Credential.Password -Enabled:$true Creating a credential to be used as a service account, and creating the account. #> function New-PSCredential { [CmdletBinding()] param ( [Parameter(Mandatory = $true, ParameterSetName="LocalKey",Position=1,ValueFromPipeline=$true)] [Parameter(Mandatory = $true, ParameterSetName="KeyVault",Position=1,ValueFromPipeline=$true)] [Parameter(Mandatory = $true, ParameterSetName="CertificateThumbprint",Position=1,ValueFromPipeline=$true)] [Parameter(Mandatory = $true, ParameterSetName="CertFile",Position=1,ValueFromPipeline=$true)] [System.String] $Username, [Parameter(Mandatory = $true, ParameterSetName="LocalKey",Position=2)] [Parameter(Mandatory = $true, ParameterSetName="CertificateThumbprint",Position=2)] [Parameter(Mandatory = $true, ParameterSetName="CertFile",Position=2)] [ValidateNotNullorEmpty()] [System.String] $Path, [Parameter(Mandatory = $true, ParameterSetName="LocalKey")] [ValidateNotNullorEmpty()] [System.Security.SecureString] $SecureKey, [Parameter(Mandatory = $true, ParameterSetName="CertificateThumbprint")] [ValidateNotNullorEmpty()] [System.String] $Thumbprint, [Parameter(Mandatory = $true, ParameterSetName="CertFile",Position=1,ValueFromPipeline=$true)] [ValidateScript({test-path $_})] [System.String] $CertificateFile, [Parameter(Mandatory = $false, ParameterSetName="CertificateThumbprint")] [ValidateSet("LocalMachine","CurrentUser")] [System.String] $CertificateStore, [Parameter(Mandatory = $true, ParameterSetName="KeyVault")] [System.String] $KeyVault, [Parameter(Mandatory = $true, ParameterSetName="KeyVault")] [System.String] $SecretName ) $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($Username, (ConvertTo-SecureString -String (New-Password) -AsPlainText -Force) ) $PSBoundParameters.Remove('Username') | Out-Null $ExportParameters = $PSBoundParameters $ExportParameters.Add('Credential',$Credential) Export-PSCredential @ExportParameters | Out-Null return $Credential } |