New-IpsCredentials.ps1
<# .Synopsis Create new credential in customer's credential wallet. .Description Create new credential in customer's credential wallet. This function supports Creating different types of credential in customer's credential wallet. #> Function New-IpsCredentials { [CmdletBinding()] Param( # Citrix Cloud customer id. [Parameter(Mandatory = $true)] [string]$CustomerId, # Credential type of target platform. [Parameter(Mandatory = $true)] [ValidateSet("Azure", "Gcp", "UsernamePassword")] [string]$CredentialType, [Parameter(Mandatory = $false)] [string]$SecureClientId = "", [Parameter(Mandatory = $false)] [string]$SecureSecret = "", [Parameter(Mandatory = $true)] [string]$CredentialId, # Azure credentials to create an Azure Credential Wallet entry from. [Parameter(Mandatory = $true, ParameterSetName = 'Azure')] [string]$AzureTenantId, [Parameter(Mandatory = $true, ParameterSetName = 'Azure')] [string]$AzureClientId, [Parameter(Mandatory = $true, ParameterSetName = 'Azure')] [string]$AzureSecret, # GCP JSON credentials file to create an GCP Credential Wallet entry from. [Parameter(Mandatory = $true, ParameterSetName = 'Gcp')] [string]$GcpServiceAccountKeyFile, # SMB or vSphere Credentials. [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')] [string]$UserDomain, [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')] [string]$UserName, [Parameter(Mandatory = $true, ParameterSetName = 'UsernamePassword')] [string]$UserPassword, [Parameter(Mandatory = $false)] [string]$LogFileName = 'Credentials.log', [Parameter(Mandatory = $false)] [switch]$Force ) Begin { Add-PSSnapin Citrix.* } Process { # Initialize Logger # Set parameter 'Verbose' by internal parameter 'VerbosePreference', since the option -Verbose is occupied by powershell cmdlet if ($VerbosePreference -eq 'Continue') { $Verbose = $True } else { $Verbose = $False } LogInit $LogFileName $Force $Verbose # Check Credential Type if ($PSCmdlet.ParameterSetName -ne $CredentialType) { LogFatal "CredentialType $CredentialType does not match the type of selected parameter set $PSCmdlet.ParameterSetName" } try { # Authenticate to Citrix Cloud $parameters = AuthToCitrixCloud $CustomerId $SecureClientId $SecureSecret if ([string]::IsNullOrWhiteSpace($SecureClientId) -Or [string]::IsNullOrWhiteSpace($SecureSecret)) { $SecureClientId = $parameters.ApiKey $SecureSecret = $parameters.SecretKey } } catch { return } # Create Credential switch ($CredentialType) { 'Azure' { $credentialCreate = @{ id = $CredentialId type = $CredentialType tenantId = $AzureTenantId clientId = $AzureClientId clientSecret = $AzureSecret } } 'Gcp' { $gcpJson = Get-Content -Raw -Path $GcpServiceAccountKeyFile | ConvertFrom-Json $credentialCreate = @{ id = $CredentialId type = $CredentialType serviceAccountKey = $gcpJson } } 'Usernamepassword' { $credentialCreate = @{ id = $CredentialId type = $CredentialType domain = $UserDomain username = $UserName password = $UserPassword } } } # Convert the object to JSON to use in the POST body (Note: Default depth is 2 when serializing) $json = $credentialCreate | ConvertTo-Json -Depth 10 # Send the POST try { LogIt "Creating new $CredentialType credential $CredentialId" $response = Invoke-CCRestMethod 'Post' '' 'credentials' $CustomerId $SecureClientId $SecureSecret $false $json $credentialId = $response.id LogIt "Created credential id $credentialId for name $CredentialId" } catch { LogFatal "Failed to create credentials: $_" } } } |