Powershell - Reboot and Continue Script

前端 未结 7 2064
误落风尘
误落风尘 2020-12-02 11:07

I\'m looking for a way to continue a Powershell script from where it left off after calling a reboot in the script. For example, I am building a DC via Powershell automation

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 12:10

    The above answer is true, but it will only apply to remote execution of powershell scripts. According to the windows web portal, the way to have your locally running script resume from where it left off after the local machine restarted is like so:

    workflow Resume_Workflow
    {
        .....
        Rename-Computer -NewName some_name -Force -Passthru
        Restart-Computer -Wait
        # Do some stuff
        .....
    }
    # Create the scheduled job properties
    $options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
    $secpasswd = ConvertTo-SecureString "Aa123456!" -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential ("WELCOME\Administrator", $secpasswd)
    $AtStartup = New-JobTrigger -AtStartup
    
    # Register the scheduled job
    Register-ScheduledJob -Name Resume_Workflow_Job -Trigger $AtStartup -ScriptBlock ({[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true; Import-Module PSWorkflow; Resume-Job -Name new_resume_workflow_job -Wait}) -ScheduledJobOption $options
    # Execute the workflow as a new job
    Resume_Workflow -AsJob -JobName new_resume_workflow_job
    

    Note that the [System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager flag should be set to true only if the workflow actions are meant to execute locally after the restart.

提交回复
热议问题