Share This Post

Manage Remote Services with Powershell(.NET)

There is a start-service cmdlet in Powershell which unfortunately can’t run on remote computer, not yet at least.

One way to get around this is to use Get-WMIObject cmdlet.

Another very cool way to do this is to use .NET classes

#$erroractionpreference = "SilentlyContinue"
foreach ($strComputer in get-content C:\MachineList.Txt)
{
$strComputer

Function RestartSav
{
$SAVSVC = [System.ServiceProcess.ServiceController]::GetServices($strComputer) | where{$_.name -eq ‘Symantec Antivirus’}
$SAVSVC.NAME
If($SAVSVC.status -eq "Running")
{
Write-host $SAVSVC.stop()
$SAVSVC.WaitForStatus(”stopped”, (New-TimeSpan -seconds 15))
Write-host $SAVSVC.displayname been successfully stopped on $strComputer
Write-host $SAVSVC.start()
$SAVSVC.WaitForStatus(”Running”, (New-TimeSpan -seconds 15))
Write-host $SAVSVC.displayname been successfully started on $strComputer
}
ElseIf($SAVSVC.status -eq "Stopped")
{
Write-host $SAVSVC.displayname is already in $SAVSVC.status state on $strcomputer -foregroundcolor "RED"
Write-host $SAVSVC.start()
$SAVSVC.WaitForStatus(”Running”, (New-TimeSpan -seconds 15))
Write-host $SAVSVC.displayname been successfully started on $strComputer
}
Else
{
Write-host Please check if $SAVSVC.displayname Service exists on $strComputer -foregroundcolor "RED"
}
}

RestartSav

$strParentServer = $Null
}

Using .Net classes, we should be aware of one very important thing – which classes to load? In the above case, it is [System.ServiceProcess.ServiceController]

When I run it the first time, I got the below error:

“Unable to find type [System.ServiceProcess.ServiceController]: make sure that the assembly containing this type is loaded.”

I then run the following code from /\/\o\/\/

if (-not ([appdomain]::CurrentDomain.getassemblies() |? {$_.ManifestModule -like “system.serviceprocess”})) {[void][System.Reflection.Assembly]::LoadWithPartialName(’system.serviceprocess’)}

The code will test if the dll is loaded already and else load it.

Share This Post

Ying Li obtained a Master degree of Medicine in China and worked in the medical field before he came to the states in 1996. He made the career switch from troubleshooting people to troubleshooting computers after he came to the states. Not entirely a surprise there, if you know that his second preference for college is Mathematics. Ying is two times Admin Framework MVP awardee (2007-2008).His previous employment experience in the US includes UUNET, MCI WorldCom. MMC (Marsh & McLennan Companies), Merrill Lynch. He is currently employed by Related Companies as a Senior Infrastructure Engineer. He is proficient in virtually all Microsoft products (Office/Back office). His craving for efficiency naturally leads him to scripting, automation and before you know it, PowerShell! He is currently a MCSE (NT4, 2000, 2003) and MCITP Server Administrator. Ying enjoys wines and love to travel. His non work related passion is to advocate Health Living!

Leave a Reply