Working With Windows Services In PowerShell

Wait..I thought Matt only did Docker stuff? Hmm..I guess he also knows a thing or two about Windows Services. Who knew? In this video, Matt demonstrates PowerShell cmdlets like Get-Service, Start-Service, and Restart-Service like a graceful swan meandering through a lake of glass.

this snip, we will be working with Windows services in PowerShell first to get a list of services on the current machine. We're going to run get service. To get a particular service on the current machine we’ll run get service dash name bits. This will return the bits service with its status name and display name. If we run get member against the get service cmdlet we could see a list of useful properties and methods available for the service controller object that gets returned. We can start stop as well as look up the status start type and service name. To get the full list of properties available from the service object. We're going to select object star and that will return all the properties here, we can see the status service type and name as well as the machine that it’s running on. Next I'm going to pull a list of services that are in the running status. You can also do the same thing for services in stopped status. The full list of services that are currently disabled we're going to use the startup type equals disabled. Next I'm going to get the list of services that are running on a remote server by running get service computer name server one. I can also filter by status or start type so I'm going to get the services on server one where startup type is equal to disabled. If I select all the properties for that remote service, you can see that the machine name is now filled in with server one. this can come in handy if you were pulling multiple services from multiple servers. Now that we have pulled information about services running on a machine I'm going to move on to interacting with those services, if we get service bits will notice that it's in the stop state right now. To start that service we're going to run start service name bits. And we can now see that it is in the running state. To stop the service we’ll just run stop service named bits and now the status is set back to stopped there is also a restart service method that we can run on the service. If we don't want to manually run stop then start service. You can see that the services in the running state status now. Next we're going to interact with services running on remote computers. First, we're going to get service bits from server one and will notice that is currently stopped. Now to start the service one would logically think that to do that, we would run start service -named bits -computer name server one. If we run that however you'll notice that an error gets thrown a parameter cannot be found that matches parameter name computer name start service does not have a computer name parameter. So how do you then start remote service? to do that we're going to use the pipeline we're going to run get service name bits computer name server one and we're going to pipe that to start service and you'll see now that the service is running. We're going to do the same thing except we're going to stop the service on the remote server. The bits services now currently stopped. Next I'm going to talk about storing service objects in variables. First, we're going to store get service name bits in this variable name service. We could see that it is currently stopped we can then pipe that variable to start service to set it to running. I'm going to stop the service again and we're going to store that in the variable again to see that it is stopped. the next thing I want to show you is what would happen if something outside of your script were to change the status of a service that you're interacting with I'm going to store the service bits in the service variable and we can see that is currently stopped. If I were to then start the service and you can see. Now that the service is actually running, however, our variable does not know that. As far as the variable’s concerned that service is still currently stopped. If you run get member against that variable you'll see a method called refresh. We're going to use that to refresh the variable and update it with the current state of that service, so if we run dollar sign service dot refresh and then display that variable you can see now that it updated the variable to say that the service is running. That was working with Windows services in power shell.