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

2019-07-29 19:13发布

With the advice provided in this answer I was able to set up the winrm on a Azure VM(1).

Right now, I can open a PS-Session with New-PSSession from

  • any Azure VM(2) to the Azure VM(1)
  • my local machine which to the Azure VM(1)

But if I do exactly the same within an 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)
}

it fails with the error message:

New-PSSession : [xxx.yyy.cloudapp.azure.com] Connecting to remote server xxx.yyy.cloudapp.azure.com failed with the following error message : Access is denied.

As user I use `localhost\admin" and I'm positiv, the password is correct (double-checked it).

Q How can I overcome the Access denied?

Update

PS-workflow got the best of me. So, there is only a minor syntactical problem in the code above. If somebody shares the right answer I'm happy to up-vote and accept it.

1条回答
【Aperson】
2楼-- · 2019-07-29 19:28

According to this 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.

So, you need modify your script as below:

$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)
}
查看更多
登录 后发表回答