Invoke-Command -asJob 变量
Invoke-Command -asJob with variables
我正在尝试使用 Invoke-Command -asJob 来 运行 a PS1 和远程机器列表上的参数,我有一段时间让变量起作用。据我了解,运行s 远程的代码块通常不能使用局部变量,但我发现了一些看起来应该可以解决问题的解决方案。我从这个开始,但它有局部变量。
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 -argumentList:$arguments –scriptblock {
& powershell.exe -executionpolicy bypass
} -credential:$credential -authentication:CredSSP –asJob -jobName:$machine > $null
}
最后我尝试了 using: 方法。
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}
然后
[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))
获取编码命令并像
一样使用
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
}
我正在尝试使用 Invoke-Command -asJob 来 运行 a PS1 和远程机器列表上的参数,我有一段时间让变量起作用。据我了解,运行s 远程的代码块通常不能使用局部变量,但我发现了一些看起来应该可以解决问题的解决方案。我从这个开始,但它有局部变量。
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 -argumentList:$arguments –scriptblock {
& powershell.exe -executionpolicy bypass
} -credential:$credential -authentication:CredSSP –asJob -jobName:$machine > $null
}
最后我尝试了 using: 方法。
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}
然后
[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))
获取编码命令并像
一样使用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
}