Generate new API Key with powershell

Hi,
I am creating a new API Key for a user in Octopus using powershell script.

function CreateAPIKey{
Param(
    $OctopusURL,
    $APIKey,
    $UserName,
    $APIKeyPurpose,
    $NewAPIKey

)
##PROCESS
$header = @{ “X-Octopus-ApiKey” = $APIKey }

$body = @{
    Purpose = $APIKeyPurpose
} | ConvertTo-Json

#Getting all users to filter target user by name
$allUsers = (Invoke-WebRequest "$OctopusURL/api/users/all" -Headers $header -Method Get -UseBasicParsing).content | ConvertFrom-Json

#Getting user that owns API Key that will be deleted
$User = $allUsers | where{$_.username -eq $UserName}

#Creating API Key
$CreateAPIKeyResponse = (Invoke-WebRequest "$OctopusURL/api/users/$($User.id)/apikeys" -Method Post -Headers $header -Body $body -UseBasicParsing -Verbose).content | ConvertFrom-Json

#Printing new API Key
Write-output "API Key created: $($CreateAPIKeyResponse.apikey)"
$NewAPIKey = $CreateAPIKeyResponse.apikey

}

CreateAPIKey $OctopusURL $APIKey $UserName $APIKeyPurpose $NewAPIKey

Then I am assigning variables in Octopus but I am not sure what value to assign $NewAPIKey.
I have left it blank and I am then sending myself an email with the new API Key but I get an error in that step:
Could not replace variables in the email body: Parsing failure: unexpected ‘#’; expected end of input (Line 1, Column 14); recently consumed: API Key:

It creates the API Key for the user but I am not sure how to assign the variable $NewAPIKey to make it display in my email

Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.