0

Im trying to force reboot machines that have not been rebooted for a period of time (14 days ) . I have a powershell Script that i got it from another resource and i made some adjustment to it to fit my company's requirement . The problem that i have is that SCCM does not read the return code from Powershell . Here is what i have so far

1- A script to detect machines that have not rebooted for 14 days , if its true then it ll exit with 3010 code ( the 3010 code is a SCCM behavior for soft reboot)
2- A SCCM application deployed to all clients with the following parameter - in the package proprieties , i have it setup to determine behavior based on
return Code ( which should be 3010 )

Script works fine except that once the script is completed I need SCCM 2012 to get a exit code of 3010 so that it would schedule a restart of the PC. Could you please help . enter image description here

Dandano
  • 23
  • 2
  • 9
  • Can you verify via the execution history (manually in regedit or with roger zanders sccm client center whether the return code 3010 is indeed received by the sccm? I suspect this is related to a problem where powershell does not correctly pass it's exit code on and not one where sccm does get the 3010 but handles it wrong – Syberdoor Jul 26 '18 at 14:14
  • How are you calling the Powershell script ? Directly from run command. I have found that I have had to run cmd / c to call a .bat file that in turn calls the .ps1 file as there seems to be a bug in getting error codes directly from Powershell back to SCCM 2012 – Simon B Jul 26 '18 at 14:22
  • @Syberdoor : Can you clarify how to check that in the registry ? – Dandano Jul 26 '18 at 14:45
  • @SimonB : I tried both ways with no success . I used bat file to call the powershell script as well as running it directly using the following powershell.exe -ExecutionPolicy Bypass ".\Mandatory Reboot.ps1" – Dandano Jul 26 '18 at 14:47
  • Try this method of executing your script to see if you get the exit code back: powershell.exe -Command "& { & '.\Mandatory Reboot.ps1'; Exit $LastExitCode }" – user1367200 Jul 27 '18 at 01:13
  • Sorry I missed that you are probably using applications not packages here. For a package you would check under HKLM\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\ but applications are far more tedious to check so in that case it would probably be easier to just assume that it doesn't work and try to call powershell without file parameter as user1367200 suggested – Syberdoor Jul 27 '18 at 06:25

1 Answers1

0

On PowerShell 2.0 and also perhaps 3.0 at least, there is a bug with the -File parameter of PowerShell so that it does not pass the exit code back to the calling process. In such cases, you have to use this method to get the exit code:

powershell.exe -Command "& { & '.\Mandatory Reboot.ps1'; Exit $LastExitCode }"
user1367200
  • 300
  • 2
  • 14