locked
Continue a Windows PowerShell Script After Restart RRS feed

  • Question

  • hallo

     I created a simple script to delete a terminal server profile

    Start-Process -FilePath 'C:\TEMP_TECH\Delprof2 1.6.0\DelProf2.exe'/u
     Restart-Computer 

    i need After the EXE file is finished running, the server will restart and then run again delete profile and restart again
    Thank you

    Wednesday, June 14, 2017 5:47 AM

All replies

  • hallo

     I created a simple script to delete a terminal server profile

    Start-Process -FilePath 'C:\TEMP_TECH\Delprof2 1.6.0\DelProf2.exe'/u
     Restart-Computer 

    i need After the EXE file is finished running, the server will restart and then run again delete profile and restart again
    Thank you

    At least, there're two ways to do it:

        1>using:GPO, saving this script as .ps1 file then create a logon/startup script

        2>using workflow, for instance:

             
    workflow Join-Domain
    {
        param([string[]] $ComputerName, [PSCredential] $DomainCred, [PsCredential] $MachineCred)
    
    
        foreach -parallel($Computer in $ComputerName)
        {
            sequence {
            Get-WmiObject -PSComputerName $Computer -PSCredential $MachineCred
            Add-Computer -PSComputerName $Computer -PSCredential $DomainCred
            Restart-Computer -ComputerName $Computer -Credential $MachineCred -For PowerShell -Force -Wait -PSComputerName ""
            Get-WmiObject -PSComputerName $Computer -PSCredential $MachineCred
            }
        }
     } 
    

    https://blogs.technet.microsoft.com/heyscriptingguy/2012/12/26/powershell-workflows-the-basics/

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 14, 2017 8:15 AM
  • So the big question here, which you haven't answered and as such can lead to some confusing/wrong answers is:

    How many times do you want the cycle of run script, restart, run script, restart to run?

    Somehow I can't imagine you wanting it on an endless loop, so how many times do you want it to run?

    Depending on your answer to this, there are various things you can do.

    Wednesday, June 14, 2017 8:34 AM
  • Hi,

    You can use  HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce key to start your script after reboot.

    I suggest adding some parameter to it to detect that it's ran at startup.

    Sergei.

    Wednesday, June 14, 2017 9:01 AM
  • @Sergei, using the registry is (no longer) a preferred solution for something like this. With the introduction of the new task scheduler in Windows Vista, that would be a much better approach which also offers a lot more control
    Wednesday, June 14, 2017 12:20 PM