Variables 使用变量调用命令-asJob

Variables 使用变量调用命令-asJob,variables,powershell,remoting,Variables,Powershell,Remoting,我正试图使用Invoke命令-asJob在远程机器列表上运行带有参数的PS1,我在让变量工作时遇到了一个麻烦。据我所知,远程运行的代码块通常不能使用局部变量,但我发现了一些解决方案,似乎它们应该解决这个问题。我从这个开始,但它有局部变量 foreach ($machine in $Machines) { Invoke-Command -computerName:$machine –scriptblock { & powershell.exe -execution

我正试图使用Invoke命令-asJob在远程机器列表上运行带有参数的PS1,我在让变量工作时遇到了一个麻烦。据我所知,远程运行的代码块通常不能使用局部变量,但我发现了一些解决方案,似乎它们应该解决这个问题。我从这个开始,但它有局部变量

foreach ($machine in $Machines) {
    Invoke-Command -computerName:$machine –scriptblock {
        & powershell.exe  -executionpolicy bypass -file $filePath -jobsfile $jobsFile
    } -credential:$credential  -authentication:CredSSP –asJob -jobName:$machine > $null
}
我还尝试使用-argumentList,其中$arguments包含两个包含变量的参数。(我还尝试了一种变体,其中-Path是一个文本,参数中只包含PS1的-jobFile参数。)

最后,我尝试了使用:方法

foreach ($machine in $Machines) {
    Invoke-Command -computerName:$machine –scriptblock {
        & powershell.exe  -executionpolicy bypass -file $using:filePath -jobsfile $using:jobsFile
    } -credential:$credential  -authentication:CredSSP –asJob -jobName:$machine > $null
}
到目前为止,一切都不起作用,因此非常感谢任何指针。

用于远程powershell 如果您在客户端的工作组中,请以管理员身份运行powershell

Enable-PSRemoting -Force
要为连接配置信任主机,您应该从客户端获得信任

Set-Item wsman:\localhost\client\trustedhosts *
重置winrm服务

Restart-Service WinRM
现在你可以使用

Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME
您应该为winrm配置防火墙 可以使用此方法运行脚本

$script = {Get-EventLog -LogName System -Newest 10 | where { $_.Index -ge 5071811 } | sort Index}
然后

获取编码的命令并使用

powershell -encodedcommand **encoded**
当您使用psremoting输入时,这是可能的

Enter-PSSession -ComputerName COMPUTER -Credential USER
您的代码:

foreach ($machine in $Machines) {
    Invoke-Command -computerName:$machine –scriptblock {
        & powershell.exe  -executionpolicy bypass -file $using:filePath -jobsfile $using:jobsFile
    } -credential:$credential  -authentication:CredSSP –asJob -jobName:$machine > $null
}
我的代码:

$s = New-PSSession -ComputerName Server1.Domain44.Corpnet.Fabrikam.com -Credential Domain01\Admin01
Invoke-Command -FilePath c:\scripts\test.ps1 -session $s -Asjob

因此,有效的解决方案是

foreach ($machine in $Machines) {
    Invoke-Command -computerName:$machine -argumentList: $filePath, $jobsFile –scriptblock {
        param (
            [String]$file,
            [String]$jobsFile
        )
        & powershell.exe  -executionpolicy bypass -file $file -jobsFile $jobsFile
    } -credential:$credential  -authentication:CredSSP –asJob -jobName:$machine > $null
}

Hidd3n,很抱歉我的帖子有点不清楚。我确实解决了这些设置,当我使用文字字符串而不是变量时,代码就像一个符咒。我遇到的问题只是代码块中的局部变量问题。啊,顿悟袭来。-ArgumentList的内容不会发送到Invoke命令中引用的PS1,而是发送到包含Invoke命令的代码块。代码块需要两个参数来接收这些参数并使用它们。我已经更新了我的代码,我有一些新的错误要处理,但看起来至少有进展。
$s = New-PSSession -ComputerName Server1.Domain44.Corpnet.Fabrikam.com -Credential Domain01\Admin01
Invoke-Command -FilePath c:\scripts\test.ps1 -session $s -Asjob
foreach ($machine in $Machines) {
    Invoke-Command -computerName:$machine -argumentList: $filePath, $jobsFile –scriptblock {
        param (
            [String]$file,
            [String]$jobsFile
        )
        & powershell.exe  -executionpolicy bypass -file $file -jobsFile $jobsFile
    } -credential:$credential  -authentication:CredSSP –asJob -jobName:$machine > $null
}