I am completely new to PowerShell… As in I just started using it a few days ago. Much patience will be greatly appreciated…
Most of our workstations are Windows 7, so I am writing and testing the script using PowerShell ISE 2.0
I am attempting to write a script that will automate the installation of an application. Before doing so, I want the script to check for previous versions, if there are any, uninstall those version, wait for the uninstaller to finish, and reboot the computer. The part I am struggling with is how to get the script to reboot the computer ONLY if the uninstaller runs. Below is what I am attempting to use to accomplish this.
There are several other parameters that are needed but, currently don’t work… That is a separate issue though.
I have asked Google several times…
I have tried -Wait
I have tried Out-Null
Finally, I tried Start-Sleep
The only time anything works is when I put Restart-Computer outside the { }. Unfortunately, this caused the computer to reboot regardless of whether or not the uninstaller runs.
PowerShell will then check if the process is still running, and when it isn’t it will go to the next line in the script, which in this case is the computer restart.
If you only want Restart-Computer to run after your uninstaller runs, then Restart-Computer should probably be INSIDE the If{} construct, not outside it.
But you might look into Start-Process, instead. That will return a System.Diagnostics.Process object, and you can check to see that it’s still running. Or, don’t wrap in Invoke-Expression - just run the uninstaller directly. That should also give you an exit code when the thing finishes, and then you can go on and do whatever.
Alexander - I tried -Wait but it didn’t work. From the looks of it, that cmdlet was not introduced until PowerShell 3.0. I am doing this in v2.0 since all of our workstations are running Windows 7. I read a little bit about the Start-Process command, but haven’t tried it yet.
Don - I had previously tried putting Restart-Computer in the { } in the following format and it didn’t work.
I just changed it to read exactly as you recommended and that worked perfectly! Thanks! I have since changed it to the following because I didn’t like defining a specific wait time before the reboot. I don’t want the wait time to be too short or too long.
Notice the semicolon used to separate two commands when you aren’t using a carriage return to do so. I don’t think the pipe was actually appropriate there, either.