亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Powershell如何在Start-Job的Scriptblock里傳參?

發布時間:2020-07-04 21:30:10 來源:網絡 閱讀:4296 作者:UltraSQL 欄目:編程語言

如何在Start-Job的Scriptblock里傳參?

方法1:

利用本地變量,從一個可擴展的字符串,使用[scriptblock]::create方法創建腳本塊:

$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.

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

温泉县| 尤溪县| 湖北省| 延庆县| 吴桥县| 麟游县| 新蔡县| 若羌县| 桦川县| 宁南县| 杭锦旗| 佛山市| 攀枝花市| 昌吉市| 武宁县| 雷山县| 甘肃省| 密云县| 盘山县| 赤峰市| 扎兰屯市| 永德县| 科尔| 乌恰县| 隆昌县| 陆川县| 彰武县| 焉耆| 额敏县| 武功县| 松阳县| 五峰| 玉林市| 闽侯县| 高雄县| 博兴县| 木里| 秀山| 通城县| 都兰县| 凤山市|