【问题标题】:PowerShell passing arguments to Invoke-Command in a foreach loopPowerShell 在 foreach 循环中将参数传递给 Invoke-Command
【发布时间】:2016-06-16 19:50:14
【问题描述】:

为什么在我已经为我的$PW 变量输入密码并使用它创建了正确的 PSCredentials 之后,PowerShell 要求我提供凭据以访问远程计算机?我遇到的另一个问题是 Start-Process 找不到指定的 FilePath(Null 或空)。

我猜这两个错误是因为同一个错误,一定是因为我的变量值没有传递给 foreach 循环。如您所见,我试图通过尝试-argumentlist 参数来实现解决方案,但它不起作用。我做错了什么?

function Install-Exe {
    param(
        [string[]]$ComputerName,
        [string]$UNCexepath,
        [string[]]$exeargs
    )
    $Admin = "domain\admin"
    $PW = Read-Host "Enter Password" -AsSecureString
    $cred = New-Object System.Management.Automation.PSCredential -argumentlist $Admin, $PW
    foreach ($c in $ComputerName) {
        Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
            Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs   
        }
    }}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您需要在Invoke-Command 脚本块中声明参数:

    foreach ($c in $ComputerName) {
        Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
            param($cred,$UNCexepath)
            Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs   
        }
    }}
    

    【讨论】:

    • 尽可能简单。我以为我试过了……好像我没有。谢谢!
    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多