PowerShell passing arguments to Invoke-Command in a foreach loop

后端 未结 1 1941
无人共我
无人共我 2021-01-28 07:26

Why does PowerShell asks me for Credentials to access a remotecomputer, after I already put in the password for my $PW variable and created the correct PSCredential

相关标签:
1条回答
  • 2021-01-28 08:13

    You need to declare the parameters inside the Invoke-Command scriptblock:

    foreach ($c in $ComputerName) {
        Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
            param($cred,$UNCexepath)
            Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs   
        }
    }}
    
    0 讨论(0)
提交回复
热议问题