Start-Job 中的变量没有得到评估 [英] Variables in Start-Job do not get evaluated

查看:46
本文介绍了Start-Job 中的变量没有得到评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Powershell 代码不评估 $agent 变量:

My Powershell code doesn't evaluate the $agent variable:

foreach ($agent in $agentcomputers) {
    Write-Output 'Starting agent on '$agent
    # psexc to start the agent
    Start-Job -ScriptBlock {& psexec $agent c:\grinder\examples\startAgent.cmd}
}

这个链接是类似于我的问题,除了我没有调用外部 Powershell 脚本.

This link is similar to my problem, except I'm not calling an external Powershell script.

我尝试添加它,使用 $args[0] 作为 $agent,并添加 -ArgumentList 参数,但没有不行.

I tried adding that in, using $args[0] for $agent, and adding the -ArgumentList parameters, but that didn't work.

编辑/回复

$agentcomputers 只是一个计算机名称列表 - 每个名称各占一行:

$agentcomputers is just a list of computer names - each on its own line:

$agentcomputers = Get-Content c:\grinder-dist\agent-computers.txt

我也试过这个 - $args[0] 不评估:

I have also tried this - and $args[0] doesn't evaluate:

Start-Job -ScriptBlock {& psexec $args[0] c:\grinder\examples\startAgent.cmd} -ArgumentList @($agent)

推荐答案

这里是解决方案.正如安迪所说,我需要将 $args 数组与 -ArgumentList 参数一起使用.另一个线程很有帮助:Powershell:将参数传递给作业

Here is the solution. As Andy said, I needed to use $args array with the -ArgumentList parameter. This other thread was helpful: Powershell: passing parameters to a job

foreach($agent in $agentcomputers){
$agentslash = "\\"+$agent
$args = ($agentslash,"c:\grinder\examples\startAgent.cmd")
Write-Output 'Starting agent on '$agent

#psexc to start the agent
$ScriptBlock = {& 'psexec' @args } 

Start-Job -ScriptBlock $ScriptBlock -ArgumentList $args

}

这篇关于Start-Job 中的变量没有得到评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆