Azure 运行手册中的新 PSSession:访问被拒绝 (ARM)

New-PSSession in an Azure-runbook: Access denied (ARM)

根据 中提供的建议,我能够在 Azure VM(1) 上设置 winrm

现在,我可以与来自

New-PSSession 开启一个 PS-Session

但如果我在 Azure Runbook 中执行完全相同的操作,

$cred = Get-AutomationPSCredential -Name "admin"
InlineScript
{
   $vmSession = New-PSSession -ConnectionUri 'https://xxx.yyy.cloudapp.azure.com:5986' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
}

失败并显示错误消息:

新建-PS会话:[xxx.yyy.cloudapp.azure.com] 正在连接到远程服务器 xxx.yyy.cloudapp.azure.com 失败,出现以下错误消息:访问被拒绝。

作为用户,我使用“localhost\admin”并且我很肯定,密码是正确的(仔细检查)。

Q 如何克服 拒绝访问?

更新

PS-workflow 打败了我。所以,上面的代码中只有一个小的语法问题。如果有人分享正确答案,我很乐意投票并接受它。

据此official documents

By default, the variables that are defined in a workflow are not visible to the commands in the InlineScript script block. To make workflow variables visible to the InlineScript, use the $Using scope modifier. The $Using scope modifier is required only once for each variable in the InlineScript.

因此,您需要按如下方式修改脚本:

$cred = Get-AutomationPSCredential -Name "admin"
InlineScript
{
   $vmSession = New-PSSession -ConnectionUri 'https://xxx.yyy.cloudapp.azure.com:5986' -Credential $Using:cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
}