DEV Community

ToolGBRMaker
ToolGBRMaker

Posted on • Originally published at toolgbrmaker.wordpress.com on

Get-Service cmdlet [Continuation] | PowerShell

Version: 5.1.x

As the title suggests, let’s continue to explore the Get-Service PowerShell cmdlet and the inherent regular tasks that could come with it.

We ended on the previous PowerShell category post with a customized view by using the Select PowerShell cmdlet and now we’ll go a deep further by sharing some examples on where we can use the cmdlet.

When we speak about services, the most common tasks that come to our mind, are tasks like start, stop, restart and display results as a report. But, how to use the acquired knowledge, till now, to do it!?

As first step we need to start by getting the list of services where we want to perform one of the above tasks…

Get-Service -ComputerName 'PTPOPF247203' -DisplayName '*Pharma*'
Enter fullscreen mode Exit fullscreen mode

Now, let’s add one of the mentioned tasks on our pipeline as next instruction. I’ll start with the Restart-Service cmdlet.

#restarting the filtered services
Get-Service -ComputerName 'PTPOPF247203' -DisplayName '*Pharma*' |
Restart-Service -PassThru
Enter fullscreen mode Exit fullscreen mode

Your list of services will get restarted in sequence with the above command avoiding manual work like navigating throw instances and, one by one restarting them. But, well, why not strive for a little bit of elegance? Even, if your stopped instance gets started with the Restart-Service cmdlet, I’m not comfortable with this lack of distinction between services status. Let’s increment a check to our script and give a more visual output of the things that are being done backstage.

To accomplish that, I’ll use the Where-Object cmdlet, and here comes a suggestion of how can be done.

#starting the stopped services or restarting the running ones
Get-Service -ComputerName 'PTPOPF247203' -Name '*Pharma*' |
Where-Object { 
    if ($_.Status -eq 'Running') {
        Write-Host 'Attempting to restart' $_.DisplayName 'with status' $_.Status -BackgroundColor DarkRed
        Restart-Service $_
    }
    elseif ($_.Status -eq 'Stopped') {
        Write-Host 'Attempting to start' $_.DisplayName 'with status' $_.Status -BackgroundColor DarkGreen
        Start-Service $_
    }      
}
Enter fullscreen mode Exit fullscreen mode

Result:

Now I’m better with myself… Looks much more intuitive this script, when we read it and we can understand better its goal.

We’re now able to manage the status of the services, therefore, we’re only missing examples of how to report the results from our scripts. On the following two examples I’ll use common formats, that are CSV and HTML.

For CSV extraction I’ll use the cmdlet Export-Csv and can be seen on the following script suggestion:

#Exporting the filtered services -CSV
Get-Service -ComputerName 'PTPOPF247203' -Name '*Pharma*' |
Select-Object DisplayName,Status |
Export-Csv "C:\Bruno_s Documents\Powershell Scripts\ServicesCSV.csv"
Enter fullscreen mode Exit fullscreen mode

And by using the above code you should get something similar to the result on the image (some data manipulation was needed on the excel side to reach the table disposal)…

If we want a result, able to be displayed on a browser… we can approach it by using an HTML file data extraction. Let’s see how we can do it.

#Exporting the filtered services -HTML
Get-Service -ComputerName 'PTPOPF247203' |
Where-Object -Property Status -eq -Value "Running" |
Select-Object DisplayName,Status |
ConvertTo-Html |
Out-File -FilePath 'C:\Bruno_s Documents\Powershell Scripts\stats.htm'
Enter fullscreen mode Exit fullscreen mode

On the script, I needed to combine the ConvertTo-Html cmdlet and also Out-File cmdlet. After file creation, I only needed to open the file on the browser.

I’m ending by here this long post, hoping that wasn’t too massive and that this sharing could help you in some way.

For a better understanding, go ahead and create your own scripts by mixing some of the shared concepts with the goal of getting your own different results.

Stay safe!

Top comments (0)