基本職位創造
啟動指令碼塊作為後臺作業:
$job = Start-Job -ScriptBlock {Get-Process}
啟動指令碼作為後臺作業:
$job = Start-Job -FilePath "C:\YourFolder\Script.ps1"
在遠端計算機上使用 Invoke-Command
啟動作業:
$job = Invoke-Command -ComputerName "ComputerName" -ScriptBlock {Get-Service winrm} -JobName "WinRM" -ThrottleLimit 16 -AsJob
以其他使用者身份開始工作(提示輸入密碼):
Start-Job -ScriptBlock {Get-Process} -Credential "Domain\Username"
要麼
Start-Job -ScriptBlock {Get-Process} -Credential (Get-Credential)
以其他使用者身份啟動作業(無提示):
$username = "Domain\Username"
$password = "password"
$secPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username, $secPassword)
Start-Job -ScriptBlock {Get-Process} -Credential $credentials