Further PowerShell scripts to manage Windows Services

Following on from the previous post I published on remotely managing Windows Services with PowerShell I thought it would be useful to add some extra scripts that you can use to work with Windows Services.

GET DETAILS FOR A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME]  | Format-Table –AutoSize

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Format-Table –AutoSize
GET DETAILS FOR WINDOWS SERVICE THAT MATCH SUPPLIED NAME CRITERIA
Get-Service -Name [INSERT_PART_OF_SERVICE_NAME]*  -ComputerName [INSERT_COMPUTER_NAME]  | Format-Table –AutoSize

Example:
This script will find all services that start with Jonathan.Welch

Get-Service -Name Jonathan.Welch*  -ComputerName jon-app-01 | Format-Table –AutoSize
GET DETAILS FOR A WINDOWS SERVICE ON MULTIPLE SERVERS
$computers = @("[INSERT_COMPUTER_NAME_1]", "[INSERT_COMPUTER_NAME_2]")
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName $computers | Select MachineName, Name, Status | Sort MachineName, Name | Format-Table –AutoSize

Example:

$computers = @("jon-app-01", "jon-app-02")
Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName $computers | Select MachineName, Name, Status | Sort MachineName, Name | Format-Table –AutoSize
WRITE A COMMENT TO CONSOLE
#Hello World!

Install a Windows Service with PowerShell

Here is a snippet of code that you can use to install a Windows Service using PowerShell:

new-service -Name [INSERT SERVICE NAME] -DisplayName "[INSERT DISPLAY NAME]" -Description "[INSERT DESCRIPTION]" -BinaryPathName "[INSERT BINARY PATH]" -StartupType Manual -Credential [INSERT CREDENTIALS]

Here is an example for a pretend service:

new-service -Name Jonathan.Welch.Test.Service -DisplayName "Jonathan Welch Test Service" -Description "Test Service that does some stuff" -BinaryPathName "C:\Jon\Jonathan.Welch.Test.Service.exe" -StartupType Manual -Credential JonDomain\Jon.Welch

To install a service with startup type Automatic just change -StartupType Manual to be -StartupType Automatic as shown below:

new-service -Name Jonathan.Welch.Test.Service -DisplayName "Jonathan Welch Test Service" -Description "Test Service that does some stuff" -BinaryPathName "C:\Jon\Jonathan.Welch.Test.Service.exe" -StartupType Automatic -Credential JonDomain\Jon.Welch

PowerShell scripts to remotely manage Windows Services

Following on from a previous post I published on how to remotely manage IIS via PowerShell scripts I thought it would be useful to detail how to also manage Windows Services remotely.

RESTART A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Restart-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Restart-service
STOP A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Stop-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Stop-service
START A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Start-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Start-service
START A DISABLED WINDOWS SERVICE
Get-WmiObject Win32_Service -ComputerName [INSERT_COMPUTER_NAME] | ? { $_.Name -Match '[INSERT_SERVICE_NAME]' -and $_.StartMode -ne 'Disabled' } | % { $_.StartService() }

Example:

Get-WmiObject Win32_Service -ComputerName jon-app-01 | ? { $_.Name -Match 'Jonathan.Welch.Test.Service' -and $_.StartMode -ne 'Disabled' } | % { $_.StartService() }

Note that in the examples above you can append a * to the end of service name to find services that match that name. So you could shorten the name to Jonathan.Welch* to find all services with names that start with Jonathan.Welch

PowerShell scripts to remotely manage IIS

I was getting tired of opening a Remote Desktop Connection to our development environment web server to stop, start or restart IIS after making a configuration change so I found out how to do this remotely using PowerShell.  Thought I’d post the scripts on here so I can reference them in the future and also so others can make use of them.

Restart IIS
invoke-command -computername [INSERT_COMPUTER_NAME] -scriptblock {iisreset}
Stop IIS
invoke-command -computername [INSERT_COMPUTER_NAME] -scriptblock {iisreset /STOP}
Start IIS
invoke-command -computername [INSERT_COMPUTER_NAME] -scriptblock {iisreset /START}