【问题标题】:Invoke-AzureRmVMRunCommand parameter passing suddenly failsInvoke-AzureRmVMRunCommand 参数传递突然失败
【发布时间】:2020-02-29 19:46:54
【问题描述】:

上下文:运行 Azure 自动化帐户解决方案,其中调用者 PS 脚本执行另一个 PS 脚本(在 VM 上执行),参数通过“Invoke-AzureRmVMRunCommand”传递。

故事:我运行了一个 PowerShell(调用者)脚本,该脚本在远程 Azure Win VM 上执行另一个(称为)PowerShell 脚本。该流程每天都通过自动化帐户计划运行,但两天前突然停止工作,因为从调用者传递到被调用脚本的参数不再起作用。我目前责怪 MSFT Azure 人员破坏了我的 PRD 解决方案。

这里是要传递的参数的调用者 PS 脚本代码:

$hshParams = @{
    strSAName         = $hshParameters.strStagingSA
    strSAAccessKey    = $strSAAccessKey
    strFileShare      = '"' + $strFileShare + '"'
    strCopyObjects    = $hshParameters.strCopyObjects
    strSrcDriveLetter = $strSrcDriveLetter
    strDstDriveLetter = $strDstDriveLetter
}

这里是 VM 运行 PS 脚本的调用:

Invoke-AzureRmVMRunCommand -ResourceGroupName $objVM.ResourceGroupName -Name $objVM.Name `
    -CommandId 'RunPowerShellScript' -ScriptPath $strRemoteScriptFileNameTmp -Parameter $hshParams

这里是VM-run PS脚本端的参数接收代码:

# Parameters
Param (
    [string] $strCopyObjects = $null,
    [string] $strSAAccessKey = $null,
    [string] $strFileShare = $null,
    [string] $strSAName = $null,
    [string] $strDstDriveLetter = $null,
    [string] $strSrcDriveLetter = $null
)

直到两天前,所有这六个字符串值都根据哈希表“$hshParams”中的参数设置正确填充:

$strSAAccessKey = 92LO1Q4tuyeiqxxx
$strFileShare = 129xxxa1.file.core.windows.net\solutionfiles
$strSAName = 12xsa1
$strDstDriveLetter = D
$strSrcDriveLetter = Z
$strCopyObjects = AutoTopUp\Application\Live

问题:现在我看到五个字符串值突然不再被填充,其中一个是垃圾,这是它们今天的样子:

$strSAAccessKey = []
$strFileShare = []
$strSAName = []
$strDstDriveLetter = []
$strSrcDriveLetter = []
$strCopyObjects = AutoTopUp\Application\Live" -strSAAccessKey 92LO1Q4tuyeiqxxx -strFileShare 129xxxa1.file.core.windows.net\solutionfiles -strSAName 12xsa1 -strDstDriveLetter D -strSrcDriveLetter Z

解决方案没有被触及,它只是按计划运行。 VM 运行脚本上的 $Args.Count 返回“2”。

我的问题:有人对这种新行为有解释吗?令人沮丧的是,我没有设法以不同的方式安排参数传递,因为有点不清楚接收哈希表值的正确方法是什么。 'Invoke-AzureRmVMRunCommand' 的 MSFT 帮助页面(当然)在这里没有帮助,我也没有找到任何其他明确的方法来传递 SO 或 Google 上的参数...

【问题讨论】:

  • 为什么不清楚。它清楚地写着hashtable,示例显示:-Parameter @{"arg1" = "var1";"arg2" = "var2"}。我建议您在调用 cmdlet 之前打印哈希表或调试您的脚本以找出问题所在
  • “不清楚”是指如何在接收器脚本中提取值,因为每个人都只像您一样复制/粘贴该 MSFT 示例行,所以从来没有一个字。是的,我做的正是调试工作——结果是我帖子的一部分。你对我的问题有更多的内容吗?
  • 进一步调试工作的结果:VM 运行脚本上 $Args.Count 的输出返回“2”,其中 $Args(0) 是“-strCopyObjects”,而 $Args(1) 是上面 '$strCopyObjects' 中的长字符串。另请注意,单双引号挂在那里。

标签: azure powershell parameter-passing remote-access azure-automation


【解决方案1】:

this MSDN 线程中提出了相关问题;只是为了可能面临类似问题的更广泛受众的利益而分享此内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多