function Handler($context, $inputs) {
# Validate input
if ((-Not $inputs.user) -or (-not $inputs.pass)) {
throw "Username or password missing"
}
# Build credential object
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $inputs.user, (ConvertTo-SecureString -String $inputs.pass -AsPlainText -Force)
# Get configurations
$getConfigParams = @{
Method = "Get"
Uri = "$($context.vcoUrl)/api/configurations"
AllowUnencryptedAuthentication = $true
Authentication = "Basic"
ContentType = "application/json"
Credential = $credentials
}
Write-Host "Get all configurations from $($getConfigParams.Uri)"
try {
$responseGetConfigurations = Invoke-RestMethod @getConfigParams
} catch {
throw "Failed to get all configuration"
}
# Find configuration in response
$configName = "TestConfiguration"
$configUrl = ($responseGetConfigurations.link | Where-Object {$_.attributes.value -eq $configName}).href
if (-not $configUrl) {
throw "Cannot find configuration $configName"
}
# Get configuration by name
$getConfigByNameParams = @{
Method = "Get"
Uri = $configUrl
AllowUnencryptedAuthentication = $true
Authentication = "Basic"
ContentType = "application/json"
Credential = $credentials
}
Write-Host "Get configuration $configName"
try {
$responseGetConfigByName = Invoke-RestMethod @getConfigByNameParams
} catch {
throw "Failed to get configuration"
}
# Create custom object from response
$configObject = @()
$responseGetConfigByName.attributes | ForEach-Object {
$configObject += [PSCustomObject]@{
Name = $_.name
Type = $_.type
Value = $_.value.psobject.Properties.value.value
}
}
# Output object to log
$configObject | Format-Table | Out-Host
# Return to output properties
$returnObject = [PSCustomObject]@{
Name = $configName
Elements = $configObject
}
return $returnObject
}