Description

A function I use in my profile for remote service start/stops.  It can read from a list or function with single targets.  After it's done it reports the current status of the service targeted.  I've intentionally formated it for easy csv export as I often times have to provide this data after the fact to verify work in process review meetings.

Command line structure: Set-RemoteService start/stop/restart service(actual name) machine(or filetoread)

Examples:

>Set-RemoteService start spooler

This will start the spooler service on the local machine

>Set-RemoteService restart lanmanserver dskincorp

This will attempt to stop then start the lanmanserver service on dskincorp

>Set-RemoteService start mssqlserver serverlist.csv | export-csv $home\desktop\servicereport.csv

This will attempt to start mssqlserver on every servername listed in column 1 of this csv and export the results to a file named servicereport.csv on the current users desktop.

Script

PowerShell
Edit|Remove
Function Set-RemoteService{ 
Param 
( 
[parameter(Mandatory=$TRUE, 
HelpMessage="You need a valid state entry. Start, Stop, or Restart")] 
[string]$STATE, 
[parameter(Mandatory=$TRUE, 
HelpMessage="Please enter a valid service name. example: spooler")] 
[string]$SERVICE, 
[parameter(Mandatory=$TRUE)][AllowEmptyString()][string]$TARGET 
)  
    Function SRS{ 
            PARAM ($SERVICE,$STATE,$MACHINE) 
        $SOBJ = New-Object PSObject 
$PING = gwmi -Query "select * from Win32_PingStatus where Address='$MACHINE'"  
    if ($PING.StatusCode -ne 0) 
{$SOBJ | Add-Member -MemberType NoteProperty -Name MachineName -Value $MACHINE 
 $SOBJ | Add-Member -MemberType NoteProperty -Name Status -Value "Unreachable"} 
    elseTRY{$ACTION = gwmi win32_service -filter "name like '$SERVICE'" ` 
                    -computername "$MACHINE" -ErrorAction continue 
switch ($STATE) 
           {stop{$ACTION.invokemethod("stopservice",$NULL| out-null} 
           start{$ACTION.invokemethod("startservice",$NULL| out-null} 
         restart{$ACTION.invokemethod("stopservice",$NULL| out-null 
                    start-sleep 2 
                $ACTION.invokemethod("startservice",$NULL| out-null}} 
 
$ACTION = gwmi win32_service -filter "name like '$SERVICE'" -computername "$MACHINE" 
    $SOBJ | Add-Member -MemberType NoteProperty -Name MachineName -Value $MACHINE 
    $SOBJ | Add-Member -MemberType NoteProperty -Name Name -Value $ACTION.Name 
    $SOBJ | Add-Member -MemberType NoteProperty -Name Status -Value $ACTION.State} 
CATCH [System.UnauthorizedAccessException]  
    {$SOBJ | Add-Member -MemberType NoteProperty -Name MachineName -Value $MACHINE 
     $SOBJ | Add-Member -MemberType NoteProperty -Name Status -Value "Access Denied"} 
                    }$SOBJ 
} 
Write-Host -foregroundcolor Yellow `nAttempting to $STATE $SERVICE 
        if($TARGET -eq "") 
            {$TARGET = gc env:computername} 
$FILE = test-path $TARGET -ErrorAction SilentlyContinue 
if($FILE -eq $TRUE) 
    {$READ = gc $TARGET 
            ForEach ($MACHINE in $READ){ 
                                        SRS $SERVICE $STATE $MACHINE                         
                                       }} 
else  
    { 
    SRS $SERVICE $STATE $TARGET 
    } 
}