简体   繁体   English

带变量的Invoke-Command -asJob

[英]Invoke-Command -asJob with variables

I am trying to use Invoke-Command -asJob to run a PS1 with arguments on a list of remote machines, and I am having a bugger of a time getting variables to work. 我正在尝试使用Invoke-Command -asJob在远程计算机列表上运行带有参数的PS1,而且我正在花时间让变量起作用。 As I understand it the code block that runs remotely cannot normally use local variables, but I have found a number of solutions that seem like they are supposed to solve the problem. 据我了解,远程运行的代码块通常不能使用局部变量,但是我发现了许多似乎可以解决问题的解决方案。 I started with this, but it has the local variables. 我从这里开始,但是它具有局部变量。

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
}

I also tried using -argumentList where $arguments includes the two arguments that contain variables. 我也尝试使用-argumentList,其中$ arguments包含两个包含变量的参数。 (I also tried a variant of that where -Path is a literal and and just the -jobFile argument for the PS1 is included in 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
}

Lastly I tried the using: approach. 最后,我尝试了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
}

So far nothing works, so any pointers are greatly appreciated. 到目前为止,任何方法都无效,因此非常感谢任何指针。

for remote powershell if you are in workgroup in clients run powershell as administrator 对于远程PowerShell,如果您在客户端的工作组中,请以管理员身份运行PowerShell

Enable-PSRemoting -Force

for configure trust host for connect you should trusted from clients 用于配置信任主机进行连接,您应该受到客户端的信任

Set-Item wsman:\localhost\client\trustedhosts *

reset winrm service 重置Winrm服务

Restart-Service WinRM

now you can use 现在您可以使用

Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME

you should configure firewall for winrm you can use this method for run your script 您应该为winrm配置防火墙,可以使用此方法运行脚本

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

then 然后

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))

get encoded command and use like 获得编码的命令并像

powershell -encodedcommand **encoded**

that is possible when you enter with psremoting 当您使用psremoting输入时可以

Enter-PSSession -ComputerName COMPUTER -Credential USER

your code: 您的代码:

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
}

my code: 我的代码:

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

So, the working solution was 因此,有效的解决方案是

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM