Chr*_*her 3 powershell start-job
我正在尝试通过 powershell 中的作业启动/停止服务。在我的本地机器上进行测试时,作业将进入完成状态,但服务不会更改其状态。有没有办法更好地了解我的工作做了什么?
这是代码:
$service = 'MSSQL$SQLEXPRESS'
$server = 'myPC'
$myJob = Start-Job -Scriptblock {Get-Service -Name $args[0] -computername $args[1] | Set-Service -Status Running} -ArgumentList @($service,$server)
sleep -Seconds 10
get-job
Run Code Online (Sandbox Code Playgroud)
当我运行该代码段时,SQL SQL (EXPRESS) 服务没有启动。
但是,如果我从提升的 shell 运行以下命令,它将启动:
Get-Service -Name $service -computername $server | Set-Service -Status Running
Run Code Online (Sandbox Code Playgroud)
当我运行 Get-Job 时,这是我看到的......
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
10     Job10           BackgroundJob   Completed     True            localhost            Get-Service -Name $arg...
Run Code Online (Sandbox Code Playgroud)
它们都从提升的 shell 运行,所以我绝对有权启动/停止服务。
使用Get-Job | Receive-Job让您能够看到作业(包括任何错误)的输出。
或者(更具体地说)对于您创建的工作:
$myjob | Receive-Job
Run Code Online (Sandbox Code Playgroud)
另外仅供参考,而不是Start-Sleep,您可以$myjob | Wait-Job在继续执行代码之前等待作业完成。你也可以:
$myjob | Wait-Job | Receive-Job
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           2024 次  |  
        
|   最近记录:  |