SCCM - Script Must Process a System Restart before continuing

Robert Musick 1 Reputation point
2022-02-10T15:16:20.633+00:00

Hi everyone,
Could someone tell me how I can run a script in SCCM that will process a restart before continuing?
Ex. I need to run a registry modification, restart, and then stop a service.
Thanks.

Windows for business Windows Client for IT Pros Networking Network connectivity and file sharing
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-02-16T13:22:56.327+00:00

    Hello @Robert Musick

    The best way to perform a restart on multiple computers is to create a SCCM package then include script ( Reboot command ) in the package.

    Go to Configuration Manager console, click Software Library -> Click Scripts -> Click on Create Script
    Create a package that runs the commandline “shutdown /f /r /t <timeinseconds>”. It display a message “Reboot” to users who are still logged in.

    Hope this answers your question

    -----------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2022-02-17T14:41:20.573+00:00

    I don't have any SCCM experience, but from a server support perspective, I would use a temporary file to serve as a flag to tell the script which function it needs to process. The main question would be how the script gets restarted after the reboot. If you can do that with SCCM, then great. Otherwise the script would need to create a scheduled task that runs at system startup to complete the install.

    Here's a framework in Powershell that might help you. It uses C:\Windows\Temp for the file location. Change that to suit your requirements.

    Start-Transcript -append -Path ("$($env:SystemRoot)\temp\SccmScript-{0}.log" -f  (get-date -format "yyyyMMdd" ))            # This is your log file
    Function Phase1 {
        "This is Phase1."
        "Do what you need to do here." 
        "Do something here to cause the script to be executed after the reboot. Create a scheduled task maybe?" 
        "Phase1 complete." | Out-File $FlagFile
        "Schedule reboot here." 
    }
    
    Function Phase2 {
        "This is Phase2." 
        "This code runs after Phase1 has run and the machine has rebooted."
        "Do what you need to do here." 
        "Delete the scheduled task here?????"
        "Delete the flag file here?????" 
        "Phase2 complete." | Out-File $FlagFile
    }
    
    $FlagFile = $env:SystemRoot + "\temp\SCCM.flg"
    "Our flag file is $FlagFile"
    
    if ((Test-Path $FlagFile) -eq $false) {
        "File does not exist. Perform phase 1."
        Phase1                                       # Call the phase1 code.
    } elseif  ((Get-Content -Path $FlagFile) -match 'Phase2') {
        "Error! I guess."
        "Our flag file indicates that both Phase1 and Phase2 have completed."
        "Does a scheduled task need to be deleted?"
        "Should the flag file be deleted?"
        "Maybe warn someone."
    } else {
        Phase2                                       # Call the phase2 code.
    } 
    Stop-Transcript
    
     
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.