Starting and Stopping a Remote Service with PowerShell

A recent project I was working on required me to move folders from a staging location to a processing location.  I found that PowerShell was the easiest way to accomplish that.  After getting that part of the project working a new requirement came up.  In order to move the folders I needed to stop an import service prior to the move and then start the service back up.  Since I had a new found love for PowerShell I figured why not use it for this as well.

I spent about 30 minutes researching how to do this online.  Many of the postings I found didn’t work for me.  I would get various errors which after researching seemed very common.  I was then lucky enough to stumble across an MSDN article which was rather helpful.  In the end I was able to get a working script that works very well for my purpose.

I had to combine WMI and PowerShell.  For the purpose of this article I have changed my server name and will use a different service.  I also added a 20 second wait in case you want to test this on your system.  For me I am going to use the Start-Sleep to add a buffer of time between the folder move and the starting of the service as well as a buffer to allow enough time for the import service to stop.

.        (gwmi win32_service -computername myservername -filter "name='RemoteRegistry'").stopservice()
.        Start-Sleep -s 20
.        (gwmi win32_service -computername myservername -filter "name='RemoteRegistry'").startservice()

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *