JobParamMethods.ps1
# # Citrix IPS job parameter support # function Test-IsCloudPlatform([string]$Platform) { return ($Platform.ToLower() -In @( 'aws', 'azure', 'gcp')) } function Get-JobParameter([hashtable]$Parameters, [string]$ParameterName, [psobject]$ConfigData=$Null, [string]$ConfigParameterName = $Null, [bool]$Verbose = $False) { if ($ConfigData) { $name = if ($ConfigParameterName) {$ConfigParameterName} else {$ParameterName} if ($ConfigData.PSobject.Properties.name -contains $name) { $Value = $ConfigData.$name if ($Null -ne $Value) { $type = $Value.GetType() } else { $type = "Null" } LogIt "Get-JobParameter $name value [$type]$Value from config" $Verbose if ($Value -Is [Array]) { return ,$Value } return $Value } LogIt "Get-JobParameter $name not found in config" $Verbose } if ($Parameters.Contains($ParameterName)) { $Value = $Parameters[$ParameterName] if ($Null -ne $Value) { $type = $Value.GetType() } else { $type = "Null" } LogIt "Get-JobParameter $ParameterName value [$type]$Value from params" $Verbose if ($Value -Is [Array]) { return ,$Value } return $Value } LogIt "Get-JobParameter $ParameterName not found in parameters" $Verbose return $Null } function Set-JobParameter([hashtable]$Parameters, [string]$ParameterName, [hashtable]$JobData, [psobject]$ConfigData=$Null, [string]$ConfigParameterName = $Null, [string]$OutputName = $Null, [bool]$Verbose = $False) { $value = Get-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName $ParameterName -ConfigParameterName $ConfigParameterName -Verbose $Verbose if ($value) { if ($OutputName) { $JobData[$OutputName] = $value } else { $JobData[$ParameterName] = $value } } } function Get-PlatformCredentialId([string]$Platform, [hashtable]$Parameters, [psobject]$ConfigData=$Null, [bool]$Verbose = $False) { $credentialName = @{ ahv = 'AhvCwSecretId' aws = 'AwsCwSecretId' azure = 'AzureCwSecretId' gcp = 'GcpCwSecretId' vsphere = 'VsphereCwSecretId' xen = 'XenCwSecretId' } $configCredentialName = @{ ahv = '' # config not supported on this platform aws = 'CloudCwSecretId' azure = 'CloudCwSecretId' gcp = 'CloudCwSecretId' vsphere = 'VsphereCwSecretId' xen = '' # config not supported on this platform } return Get-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName $credentialName[$Platform.ToLower()] -ConfigParameterName $configCredentialName[$Platform.ToLower()] -Verbose $Verbose } function Get-ResourceLocationId([psobject]$ConfigData, [hashtable]$Parameters, [string]$JobResourceLocationIdName = $Null, [bool]$Verbose = $False) { if ($ConfigData) { if ($JobResourceLocationIdName -And $ConfigData.PSobject.Properties.name -contains $JobResourceLocationIdName) { LogIt "Get-JobParameter $JobResourceLocationIdName from config" $Verbose return $ConfigData.$JobResourceLocationIdName } elseif ($ConfigData.PSobject.Properties.name -contains 'ResourceLocationId') { LogIt "Get-JobParameter ResourceLocationId from config" $Verbose return $ConfigData.ResourceLocationId } LogIt "Get-JobParameter ResourceLocationId not found in config" $Verbose return $Null } if ($Parameters.Contains('ResourceLocationId')) { LogIt "Get-JobParameter ResourceLocationId from params" $Verbose return $Parameters['ResourceLocationId'] } LogIt "Get-JobParameter ResourceLocationId not found" $Verbose return $Null } function Set-Tags([string]$Platform, [psobject]$ConfigData, [hashtable]$Parameters, [hashtable]$JobData, [bool]$Verbose = $False) { if ($Platform -In @("Aws", "Azure", "Gcp", "vSphere", "Xen")) { $Tags = Convert-Object (Get-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName "Tags" -Verbose $Verbose) $UserTag = ($env:UserName).ToLower() if ($Platform -eq "Gcp") { $UserTag = $UserTag.Replace('.', '_').SubString(0, [System.Math]::Min(63, $UserTag.Length)) } $Tags['ctx-user'] = $UserTag $JobData['tags'] = $Tags } } function Format-JobData([string]$Platform, [hashtable]$Parameters, [psobject]$ConfigData = $Null, [string]$JobResourceLocationIdName = $Null, [bool]$Verbose = $False) { $JobData = @{ platform = $Platform platformCredentialId = Get-PlatformCredentialId -Platform $Platform -ConfigData $ConfigData -Parameters $Parameters -Verbose $Verbose resourceLocationId = Get-ResourceLocationId -ConfigData $ConfigData -Parameters $Parameters -JobResourceLocationIdName $JobResourceLocationIdName -Verbose $Verbose overwriteTargetFile = $Parameters.Contains('Force') -And $Parameters.Force } Set-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName "Prefix" -ConfigParameterName 'Prefix' -JobData $JobData -Verbose $Verbose Set-Tags -Platform $Platform -ConfigData $ConfigData -Parameters $Parameters -JobData $JobData -Verbose $Verbose Set-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName "JobDebug" -ConfigParameterName 'Debug' -OutputName 'Debug' -JobData $JobData -Verbose $Verbose Set-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName "AssetsId" -JobData $JobData -Verbose $Verbose Set-JobParameter -ConfigData $ConfigData -Parameters $Parameters -ParameterName "Timeout" -OutputName 'timeoutInSeconds' -JobData $JobData -Verbose $Verbose return $JobData } # # SMB parameter support # function Get-SmbConfigParameter([psobject]$ConfigData, [hashtable]$Parameters, [string]$SmbConfigNode, [string]$ParameterName, [bool]$Verbose) { if ($ConfigData.$SmbConfigNode.PSobject.Properties.name -contains $ParameterName) { LogIt "Get-SmbConfigParameter $ParameterName from Smb config node $SmbConfigNode" $Verbose return $ConfigData.$SmbConfigNode.$ParameterName } LogIt "Get-SmbConfigParameter $ParameterName not found in Smb config node $SmbConfigNode" $Verbose return $Parameters[$ParameterName] } function Get-SmbLocation([psobject]$ConfigData, [hashtable]$Parameters, [string]$SmbConfigNode, [bool]$Verbose) { if ($ConfigData) { $Hostname = Get-SmbConfigParameter -ConfigData $ConfigData -Parameters $Parameters -SmbConfigNode $SmbConfigNode -ParameterName 'Host' -Verbose $Verbose $Share = Get-SmbConfigParameter -ConfigData $ConfigData -Parameters $Parameters -SmbConfigNode $SmbConfigNode -ParameterName 'Share' -Verbose $Verbose $Path = Get-SmbConfigParameter -ConfigData $ConfigData -Parameters $Parameters -SmbConfigNode $SmbConfigNode -ParameterName 'Path' -Verbose $Verbose return [System.UriBuilder]::new("smb", $Hostname, -1, "$Share/$Path").Uri } if ($Parameters['SmbLocation']) { $SmbLocation = $Parameters['SmbLocation'] LogIt "Get-SmbLocation SmbLocation from params '$SmbLocation'" $Verbose return $SmbLocation } elseif ($Parameters['SmbHost']) { if ($Parameters['SmbPath']) { $smbDiskPath = "$($Parameters['SmbShare'])/$($Parameters['SmbPath'])" } else { $smbDiskPath = $Parameters['SmbShare'] } $SmbLocation = [System.UriBuilder]::new('smb', $Parameters['SmbHost'], -1, $smbDiskPath).Uri LogIt "Get-SmbLocation SmbLocation constructed from params '$SmbLocation'" $Verbose return $SmbLocation } LogIt "Get-SmbLocation SmbLocation not found" $Verbose return $Null } function Get-SmbParameter([hashtable]$Parameters, [string]$SmbConfigNode, [string]$ParameterName, [psobject]$ConfigData = $Null, [string]$ConfigParameterName = $Null, [bool]$Verbose = $False) { if ($ConfigData) { $name = if ($ConfigParameterName) {$ConfigParameterName} else {$ParameterName} return Get-SmbConfigParameter -ConfigData $ConfigData -Parameters $Parameters -SmbConfigNode $SmbConfigNode -ParameterName $name -Verbose $Verbose } if ($Parameters.Contains($ParameterName)) { LogIt "Get-SmbParameter $ParameterName from params" $Verbose return $Parameters[$ParameterName] } LogIt "Get-SmbParameter $ParameterName not found" $Verbose return $Default } |