将安全字符串传递给Invoke-AzVMRunCommand

时间:2019-02-04 13:51:56

标签: azure azure-resource-manager

我需要将安全字符串作为参数传递给Invoke-AzVMRunCommand。我的代码是

$pass=Read-Host "Enter Password for sysadmin" -AsSecureString
$stdstring  = ConvertFrom-SecureString $pass 

 $runcmdparameters=@{
      "sysadmincredential"=$stdstring           
                       }
        Invoke-AzVMRunCommand -ResourceGroupName $ServerResourceGroupName -VMName $VMVame -ScriptPath $TemplateFile  -CommandId 'RunPowerShellScript' -Parameter $runcmdparameters -Verbose

Invoke-AzVMRunCommand脚本

Param(
[parameter(Mandatory=$true)][string]$sysadmincredential
)

$cr=ConvertTo-SecureString $sysadmincredential

Out-File "C:\Temp\log.txt" -InputObject $cr -Append

但是,脚本失败并显示

  

ConvertTo-SecureString:系统找不到指定的路径。   在   C:\ Packages \ Plugins \ Microsoft.CPlat.Core.RunCommandWindows \ 1.1.0 \ Downloads \ s   cript11.ps1:4 char:5   + $ cr = ConvertTo-SecureString $ sysadmincredential   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:InvalidArgument:(:) [ConvertTo-SecureString],C ryptographicException       + FullyQualifiedErrorId:ImportSecureString_InvalidArgument_Cryptographic
  错误,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

谢谢

1 个答案:

答案 0 :(得分:0)

我可以在我这边重现您的问题,您需要传递-AsPlainText -Force,命令应该为$cr = ConvertTo-SecureString $sysadmincredential -AsPlainText -Force,这样才能正常工作。

enter image description here