网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Powershell如何在Start-Job的Scriptblock里传参?

如何在Start-Job的Scriptblock里传参?

方法1:

利用本地变量,从一个可扩展的字符串,使用[scriptblock]::create方法创建脚本块:

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比临沧网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式临沧网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖临沧地区。费用合理售后完善,十多年实体公司更值得信赖。

$v1 = "123"
$v2 = "asdf"
$sb = [scriptblock]::Create("Write-Host 'Values are: $v1, $v2'")
$job = Start-Job -ScriptBlock $sb

# 另一种写法
[scriptblock]$sb =
{
Write-Host "Values are: $v1, $v2"
}



方法2:

在InitializationScript中设置变量

$Init_Script = {
$v1 = "123"
$v2 = "asdf"
}
$sb = {
    Write-Host "Values are: $v1, $v2"
}
$job = Start-Job -InitializationScript $Init_Script -ScriptBlock $sb



方法3:

使用-Argumentlist参数

$v1 = "123"
$v2 = "asdf"
$sb = {
    Write-Host "Values are: $($args[0]), $($args[1])"
}
$job = Start-Job  -ScriptBlock $sb -ArgumentList $v1,$v2

深入阅读

Using scope

PowerShell v3 introduced another level of scope called using. It doesn’t fit into the
hierarchical model described in figure 22.1, and the rules laid down in this chapter
don’t apply, because it’s designed to be used in situations where you want to access
a local variable in a remote session. The definition from the help file about_scopes is:
Using is a special scope modifier that identifies a local variable in a remote
command. By default, variables in remote commands are assumed to be defined
in the remote session.

More on using can be found in about_Remote_Variables. It’s used like this:
PS C:\> $s = New-PSSession -ComputerName W12Standard
PS C:\> $dt = 3
PS C:\> Invoke-Command -Session $s -ScriptBlock {Get-WmiObject -Class
➥ Win32_LogicalDisk -Filter "DriveType=$dt"}

Invalid query "select * from Win32_LogicalDisk where DriveType="
+ CategoryInfo : InvalidArgument: (:)
[Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,
Microsoft.PowerShell.Commands.GetWmiObjectCommand
+ PSComputerName : W12Standard

PS C:\> Invoke-Command -Session $s -ScriptBlock {Get-WmiObject -Class
➥ Win32_LogicalDisk -Filter "DriveType=$Using:dt"}
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 126600425472
Size : 135994011648
VolumeName :
PSComputerName : W12Standard

Create a remoting session to another machine and define a local variable. If you try
to use that variable (which is local) in the remote session, you’ll get the error shown
in the first command, but if you employ the using scope, it works.
In PowerShell v2, you could do this:
PS C:\> Invoke-Command -Session $s -ScriptBlock {param($d) Get-
➥ WmiObject -Class Win32_LogicalDisk -Filter "DriveType=$d" }
➥ -ArgumentList $dt

Using is a valuable addition to your toolbox when you’re working with remote machines.


名称栏目:Powershell如何在Start-Job的Scriptblock里传参?
当前链接:http://bjjierui.cn/article/ieohpi.html

其他资讯