本文介绍了Powershell 将变量传递给开始作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 powershell 中,我想学习将变量调用到启动作业的最佳方法,这样我就不必为每个服务器编辑脚本,因为它将根据我放置脚本的客户端而特定

within powershell I'd like to learn the best way to call a variable to a start job so I don't have to edit the script for each server as it will be specific based on the client I've placed my script on.

$Servername = 'Server1'
    $pingblock = {
      pathping $servername | Out-File C:\client\PS\ServerPing.TXT
    }
    start-job $pingblock

当我运行上面的代码时,我只是在帮助下得到一个文件,就好像我忘记指定 $servername 一样.

when I run my code above I just get a file with the help as if I forgot the specify the $servername.

推荐答案

Start-Job 上使用 -ArgumentList 参数,例如:

Use the -ArgumentList parameter on Start-Job e.g.:

Start-Job -Scriptblock {param($p) "`$p is $p"} -Arg 'Server1'

就你而言:

$pingblock = {param($servername) pathping $servername | Out-File C:\...\ServerPing.txt}
Start-Job $pingblock -Arg Server1

这篇关于Powershell 将变量传递给开始作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:36