Weird behaviour when sending SAS Token to Set-AzVMDscExtension
Description
When taking the SAS token from New-AzStorageContainerSASToken and sending it to Set-AZVMDscExtension via the -ConfigurationArgument parameter the token is not translated into the DSC Correctly. If the DSC dumps the variable to a file as part of the DSC (for troubleshooting purposes) it appears that the string is converted to a hashtable with a single item in it, this has a name of CliXml and a value of:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>?sv=2018-11-09&sr=c&sig=removed%2B4Y8%3D&se=2019-05-07T18%3A09%3A40Z&sp=r</S>
</Objs>
Whats really weird is when the SAS token is hard coded into the script such as
$StorageToken = "?sv=2018-11-09&sr=c&sig=removed%removed%3D&se=2019-05-07T19%3A40%3A06Z&sp=r"
The script works fine. My first thought was that New-AzStorageContainerSASToken didn't return a string however
write-output $StorageToken.GetType().FullName
write-output "Token is: $StorageToken"
returns the expected behaviour as part of the launch script
Steps to reproduce
$ResourceGroupName = "RGName"
$VMName = "VMName"
$DSCConfigFile = Join-Path $PSScriptRoot -ChildPath "TroubleShootDSC.ps1"
$DSCStorageAccountName = "StorageAccount"
$DSCStorageAccountResourceGroupName = "RGName"
$StorageAccount = Get-AzStorageAccount -ResourceGroupName $DSCStorageAccountResourceGroupName -Name $DSCStorageAccountName
#Get 6 hour SAS token to download source files
$StorageToken = New-AzStorageContainerSASToken -Container "install-files" -Context $StorageAccount.Context -Permission r -ExpiryTime (Get-Date).AddHours(6)
write-output $StorageToken.GetType().FullName
write-output "Token is: $StorageToken"
#$StorageToken = "?sv=2018-11-09&sr=c&sig=removed%removed%3D&se=2019-05-07T19%3A40%3A06Z&sp=r"
$DSCParameters = @{
Value = $StorageToken
}
$BlobName = Publish-AzVMDscConfiguration -ConfigurationPath $DSCConfigFile -ResourceGroupName $DSCStorageAccountResourceGroupName -StorageAccountName $DSCStorageAccountName -Force
Set-AzVMDscExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -Name "TroubleShoot" -ArchiveResourceGroupName $DSCStorageAccountResourceGroupName -ArchiveStorageAccountName $DSCStorageAccountName -ConfigurationArgument $DSCParameters -ArchiveBlobName ($BlobName.Substring($BlobName.LastIndexOf('/')+1)) -Version 2.77 -ConfigurationName "TroubleShoot" -AutoUpdate
DSC
Configuration Troubleshoot
{
Param
(
[Parameter(Mandatory = $true)]
$Value
)
$Value | Out-File C:\temp\variable.txt
Node $AllNodes.NodeName
{
File "NewFile"
{
DestinationPath = "C:\Temp\Content.txt"
Type = 'File'
Contents = $Value
}
}
}
Environment data
Name Value
---- -----
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Module versions
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.5.2 Az.Accounts {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount...}
Script 2.0.0 Az.Compute {Add-AzContainerServiceAgentPoolProfile, Add-AzImageDataDisk, Add-AzVhd, Add-AzVMAdditionalUnattendContent...}
Script 1.2.0 Az.Monitor {Add-AzAutoscaleSetting, Add-AzLogProfile, Add-AzMetricAlertRule, Add-AzMetricAlertRuleV2...}
Script 1.8.0 Az.Network {Add-AzApplicationGatewayAuthenticationCertificate, Add-AzApplicationGatewayBackendAddressPool, Add-AzApplicationGatewayBackendHttpSetting, Add-AzApplicatio...
Script 1.3.0 Az.Storage {Add-AzRmStorageContainerLegalHold, Add-AzStorageAccountManagementPolicyAction, Add-AzStorageAccountNetworkRule, Disable-AzStorageBlobDeleteRetentionPolicy...}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}
Script 0.7.3 posh-git {Add-PoshGitToProfile, Add-SshKey, Enable-GitColors, Expand-GitCommand...}
Script 0.2.0 PowerShellEditorServices.Commands {ConvertFrom-ScriptExtent, ConvertTo-ScriptExtent, Find-Ast, Get-Token...}
Script 0.2.0 PowerShellEditorServices.VSCode {Close-VSCodeHtmlContentView, New-VSCodeHtmlContentView, Set-VSCodeHtmlContentView, Show-VSCodeHtmlContentView...}
Debug output
Error output
Set-AzVMDscExtension : Long running operation failed with status 'Failed'. Additional Info:'VM has reported a failure when processing extension 'TroubleShoot'. Error message: "The DSC Extension received an incorrect
input: Compilation errors occurred while processing configuration 'Troubleshoot'. Please review the errors reported in error stream and modify your configuration code appropriately. Invalid MOF definition for node
'localhost': Exception calling "ValidateInstanceText" with "1" argument(s): "Convert property 'Contents' value from type 'INSTANCE' to type 'STRING' failed
At line:27, char:2
Buffer:
e = "Troubleshoot";
};^
insta
" Exception calling "ValidateInstanceText" with "1" argument(s): "Convert property 'Contents' value from type 'INSTANCE' to type 'STRING' failed
At line:27, char:2
Buffer:
e = "Troubleshoot";
};^
insta
"
Another common error is to specify parameters of type PSCredential without an explicit type. Please be sure to use a typed parameter in DSC Configuration, for example:
configuration Example {
param([PSCredential] $UserAccount)
...
}.
Please correct the input and retry executing the extension.".'
At C:\Users\leo.darcy\OneDrive - PowerONPlatforms\Documents\Projects\Pulse DMS\Troubleshoot\LaunchTroubleShootDSC.ps1:20 char:1
+ Set-AzVMDscExtension -ResourceGroupName $ResourceGroupName -VMName $V ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (:) [Set-AzVMDscExtension], CloudException
+ FullyQualifiedErrorId : InvalidResult,Microsoft.Azure.Commands.Compute.Extension.DSC.SetAzureVMDscExtensionCommand
This specific error is related to the fact that I am trying to save the string to a file using the DSC File Resource and it can't translate the hashtable. If I change the DSC to $Value.CliXml then the value is successfully saved to the file but the value is
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>?sv=2018-11-09&sr=c&sig=removed%2B4Y8%3D&se=2019-05-07T18%3A09%3A40Z&sp=r</S>
</Objs>
rather than the expected
?sv=2018-11-09&sr=c&sig=removed%2B4Y8%3D&se=2019-05-07T18%3A09%3A40Z&sp=r
EDIT: For some reason wrapping the Token in a new string appears to solve the issue
$DSCParameters = @{
Value = [string]::new($StorageToken)
}
@danielsollondon Can you take a look?
Adding Roiy to look at this as well since Roiy has been looking after extensions recently.
@ld0614, Is this a problem you are still seeing? I just realized that this issue was assigned to someone who had left Microsoft. So I need to get someone new looking into this issue if it is still impacting you.
Hi @Drewm3, I have just retested and I am still seeing this behaviour if I don't use the workaround
@axayjo, could you please look into this issue?
@Bhargava-Chary-Chollaty did some further investigation and this is the expected behavior. The workaround provided in the repro is not a workaround, but the right way of using the command.
Here are the findings:
• “ConfigurationArgument” is an argument of type hashtable for command “Set-AzVMDscExtension”. I believe there is a difference in PowerShell behavior when it passes on arguments to underlying code. When a string object is defined as one of the value in hashtable, in execution code its default type is reflecting as “System.Management.Automation.PSObject" as is treated as string. whereas when a string literal is defined as value its default type is reflecting as “System.string” as treated as string. • In the execution code for this command we are simply trying to read the type of the argument passed on (here) and pass it on to the VM using a library function (here). Since it is defined as a PSObject, it is being converted to xml format before it is submitted to VM. • In the repro steps provided in the github issue, cx is trying to pass below object as an argument. Hence it is being treated as PSObject and we are seeing the behaviour reported. If the argument is passed by explicitly converting the object to string literal everything works fine. o Argument Causing the faulty behavior. $DSCParameters = @{ Value = $StorageToken } o Argument with expected behavior. $DSCParameters = @{ Value = “$StorageToken” }