Greetings!
I would like to share a couple of nifty commandlets that i build into out costume module.
Start-RemoteService, Stop-RemoteService and Restart-RemoteService.
This is particular nice when PrintServer104 is troubeling you and you need to restart spooler. Easy task, but normally you would either log onto the server and fire up whatever favorite way to stop a service or on a remote server start services.msc and remote connect to a the printserver, find the service and restart it.
With these commandlets it have become faster and easier. In your favorite prompt (assuming that is a Powershell prompt) type
Restart-RemoteService -computer PrintServer104 -service spooler
You can also just choose to start a searvice by
Start-RemoteService -computer PrintServer104 -service spooler
or stop a service by
Stop-RemoteService -computer PrintServer104 -service spooler
Cut and past these functions into your custom module and save time and frustrations by getting the job done :)
Function Start-RemoteService
{
<#
.Synopsis
Send a command to remote server to start a specifik service
.Description
Send a command to remote server to start a specifik service
.Parameter $Computer
Computername
.Parameter $service
Service to restart
.Example
Start-RemoteService Server01 spoolsv
Description
-----------
Will start the service named spoolsv on server Server01
.Notes
NAME: Start-RemoteService
AUTHOR: Claus Søgaard
#>
[CmdletBinding()]
param([string]$strComputer,[String]$strService)
Process
{
$result=(Get-WmiObject -computer $strComputer Win32_Service -Filter "Name='$strService'").InvokeMethod("startService",$null)
if($result -eq "0")
{
Write-Host "Succesfully started $strService"
}
ELSE
{
Write-Host -ForegroundColor Red "Failed to start $service on $strComputer"
}
}
}
Function Stop-RemoteService
{
<#
.Synopsis
Send a command to remote server to stop a specifik service
.Description
Send a command to remote server to stop a specifik service
.Parameter $Computer
Computername
.Parameter $service
Service to stop
.Example
Stop-RemoteService Server01 spoolsv
Description
-----------
Will stop the service named spoolsv on server Server01
.Notes
NAME: Stop-RemoteService
AUTHOR: Claus Søgaard
#>
[CmdletBinding()]
param([string]$strComputer,[String]$strService)
Process
{
$result=(Get-WmiObject -computer $strComputer Win32_Service -Filter "Name='$strService'").InvokeMethod("stopService",$null)
if($result -eq "0")
{
Write-Host "Succesfully stopped $service on $strComputer"
}
ELSE
{
Write-Host -ForegroundColor Red "Failed in stopping $strService"
}
}
}
Function Restart-RemoteService
{
<#
.Synopsis
Send a command to remote server to restart a specifik service
.Description
Send a command to remote server to restart a specifik service
.Parameter $Computer
Computername
.Parameter $service
Service to restart
.Example
Restart-RemoteService Server01 spoolsv
Description
-----------
Will restart the service named spoolsv on server Server01
.Notes
NAME: Start-RemoteService
AUTHOR: Claus Søgaard
#>
[CmdletBinding()]
param([Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$Computer,
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][String]$service)
Process
{
Stop-RemoteService $computer $service
Start-Sleep 1
Start-RemoteService $computer $service
}
}
Ingen kommentarer:
Send en kommentar