Home > Net >  How do I normalize this URL in powershell?
How do I normalize this URL in powershell?

Time:04-28

I have the following code for Azure key vault resources:

$tempkeyid = (Get-AzKeyVaultKey -VaultName $vaultname -Name $tempkeyname).Id
$tempkeyid = $tempkeyid.Substring(0, $tempkeyid.LastIndexOf('/'))
$tempkeyid
New-AzSynapseWorkspaceKey -ResourceGroupName $resourcegroup -WorkspaceName $workspacename -Name $tempkeyname -EncryptionKeyIdentifier $tempkeyid

Line 2 is there because the key vault returns the versioned identifier but line 4 needs the versionless identifier

The value for $tempkeyid in line 3 is:

https://keyvaultname.vault.azure.net:443/keys/keyname

I get the error:

New-AzSynapseWorkspaceKey: Key Vault URL should be a normalized URL

I've tried to encode and decode the URL with the code below but the encode attempt gets the same error and the decode attempt gets an error that says "Invalid URI: The format of the URI could not be determined". I'm not sure how to normalize it otherwise, or what specifically isn't normalized. The value of $tempkeyid looks correct to me.

$tempkeyid = [System.Web.HttpUtility]::UrlEncode($tempkeyid) 
$tempkeyid = [System.Web.HttpUtility]::UrlDecode($tempkeyid)

CodePudding user response:

Encoding is not the same as URI normalisation which refers to expressing the uri in a standard format. (See wikipedia entry). In your case, I would guess it doesn't like the use of the default port (:443). To systematically get a normalised uri, try something like this:

$tempkeyiduri = [System.Uri]($tempkeyid)
$tempkeyid = $tempkeyiduri.AbsoluteUri
  • Related