PowerShell工作流学习-4-工作流中重启计算机

关键点:

a)工作流中重新启动计算机,请使用Restart-Computer的Wait参数,Wait参数不仅适用于本地计算机,也适用于远程计算机。

b)重启运行工作流的计算机,手工恢复请使用Resume-Job,自动恢复请在运行工作流的计算机上创建计划任务、当工作流运行完成后注销该计划任务。

例a)

workflow Test-WFRestartServer 
{
 Param
    (
        [string]$ServerName="localhost"
    )
  Get-Process -Name WMSvc -PSComputerName $ServerName
  Restart-Computer -PSComputerName $ServerName  -Wait
  Set-Content -Path "\$ServerNamecWFTest1.txt" -Value 123
}

ls '\website14cWFTest1.txt'
Test-WFRestartServer -ServerName "website14"
ls '\website14cWFTest1.txt'


ls : 找不到路径“\website14cWFTest1.txt”,因为该路径不存在。
所在位置 行:12 字符: 1
+ ls '\website14cWFTest1.txt'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\website14cWFTest1.txt:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                          PSComputerName                     
-------  ------    -----      ----- -----   ------     --  -- -----------                          --------------                     
    255      50    20480      19768   515     0.25   9280   0 WMSvc                                website14                       

LastWriteTime : 2017/7/6 12:55:47
Length        : 5
Name          : 1.txt

例b:

workflow Test-WFRestartServer 
{
 Param
    (
        [string]$ServerName="localhost"
    )
  Get-Process -Name WMSvc -PSComputerName $ServerName
  Restart-Computer -PSComputerName $ServerName  -Wait
  ls '\localhostcWFTest1.txt'
}

$AtStartup = New-JobTrigger -AtStartup Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job ComputerSetup -State Suspended | Resume-Job} Test-WFRestartServer
Unregister-ScheduledJob -Name ResumeWorkflow
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                          PSComputerName                     
-------  ------    -----      ----- -----   ------     --  -- -----------                          --------------                     
    251      47    20176      18124   516     8.34   1960   0 WMSvc                                localhost                          

LastWriteTime  : 2017/7/6 16:36:34
Length         : 5
Name           : 1.txt
PSComputerName : localhost
原文地址:https://www.cnblogs.com/lixiaonuohao/p/7127129.html