Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell : Using .NET to manage Remote services

On the Scriptinganswers PowerShell forum are some nice threads 

especialy JVierra is very active there and gives excelent answers 

today I did see this thread : 

http://www.scriptinganswers.com/forum/forum_posts.asp?TID=340 ,

it starts about errorhandling but then goes to WMI and services on remote computers and JVierra gives an nice WMI example to wait for a service to stop on a remote computer

I will give a sample of using .NET for this below, again as I explain on my old blog here :PowerShell : Access remote eventlogs :

the CMDlet get-service does not support this but if we use the .net class directly we can use it remote in almost the same way as we work with the get-Service cmdlet after that, for more information see the former post

 

# Manage remote Services

# do not forget to set a TimeOut on the waitForStatus as ctr-C will not cancel it
 
[System.ServiceProcess.ServiceController]::GetServices('server')

(new-Object System.ServiceProcess.ServiceController('Service','server')).Start()
(new-Object System.ServiceProcess.ServiceController('Service','server')).WaitForStatus('Running',(new-timespan -seconds 5))

(new-Object System.ServiceProcess.ServiceController('Service','server')).Stop()
(new-Object System.ServiceProcess.ServiceController('Service','server')).WaitForStatus('Stopped'',(new-timespan -seconds 5))

I posted it here also as I not seem to login on the forum at the moment.

 

Enjoy,

Greetings /\/\o\/\/

Tags : PowerShell

Published Wednesday, January 03, 2007 5:26 PM by admin

Comments

# re: PowerShell : Using .NET to manage Remote services

Thanks MOW - you read my mind.  That was to be my next step but I was thinking it might be the beginning of a good utility CmdLet.

Admins seem to always want to restart services.  This makes it quite simple.  Add a wrapper and some pipeling automation and it could take over the enterprise.

Thursday, January 04, 2007 7:51 PM by JVierra

# re: PowerShell : Using .NET to manage Remote services

This is simply great stuff, I can explore the possibility of all scripts at techstarts.blogspot.tocm across multiple computers. in fact uptime utility is successfully working. Lovely.

Mow tell me onething why I'm getting "Unable to find type [system.serviceprocess.servicecontroller]: make sure that the assembly containing this type is load" But when I just run get-services and then [system.serviceprocess.servicecontroller]::getservices('ServerName') it works..A tip will help me writing a good script.

Wednesday, January 10, 2007 5:08 PM by TECstarts

# re: PowerShell : Using .NET to manage Remote services

hiya, TECstarts,

I did run to the same problem, if you do :

[appdomain]::CurrentDomain.getassemblies()

before you use get-service the dll is not loaded yet, its dynamicly loaded when needed,

but you can also load it by hand in front :

[System.Reflection.Assembly]::LoadWithPartialName('system.serviceprocess')

 this code will test if this dll is loaded allready and else load it

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

 

Greetings /\/\o\/\/

Friday, January 12, 2007 4:05 AM by MoW

# re: PowerShell : Using .NET to manage Remote services

Nice one - I was looking for something like this.

btw, any idea how you would change the password  and/or user credentials a remote service is runnning at?

Friday, January 12, 2007 7:24 AM by filip

# re: PowerShell : Using .NET to manage Remote services

WMI can do this, see following script generated by get-wmiMethodHelp (needed some psbase changes as it was pre-RC2, I will look in to it a bit more and do an enrty about it, hope thid helps for now :

# win32_service change-Method Sample Script

# Created by Get-WmiMethodHelp

# /\/\o\/\/ 2005

# Fill InParams values before Executing

# InParams that are Remarked (#) are Optional

$Class = "win32_service"

$Method = "change"

$Computer = "."

#win32_service Key Properties :

`n$filter = ""

$MC = get-WMIObject $class -computer $Computer -filter $filter`n

$InParams = $mc.psbase.GetMethodParameters($Method)`n

$InParams["DesktopInteract"] = [boolean]

$InParams["DisplayName"] = [string]

$InParams["ErrorControl"] = [uint8]

$InParams["LoadOrderGroup"] = [string]

$InParams["LoadOrderGroupDependencies"] = [string]

$InParams["PathName"] = [string]

$InParams["ServiceDependencies"] = [string]

$InParams["ServiceType"] = [uint8]

$InParams["StartMode"] = [string]

$InParams["StartName"] = [string]

$InParams["StartPassword"] = [string]

"Calling win32_service : change "

`n$R = $mc.InvokeMethod($Method,$Null)

`"Result : `"

$R

gr /\/\o\/\/

Sunday, January 14, 2007 4:21 PM by MoW

# Endless Wire » Blog Archive » Remote restarting services with Powershell

# detecting dynamic loaded types | keyongtech

Thursday, January 22, 2009 4:09 AM by detecting dynamic loaded types | keyongtech

# The PowerShell Guy : PowerShell : Using .NET to manage Remote services

How you can use .NET to directly manage services on a remote machine. The PowerShell Guy : PowerShell

Wednesday, February 18, 2009 7:46 PM by It's my life... And I live it...

# re: PowerShell : Using .NET to manage Remote services

MOW Great Article,

So no my question is how about changing/setting a password? I have been searching for this call but have not found a thing. if I was looking to do it with vbscript I would have been done days ago but PowerShell is definitely not as well documented. I prefer to use PowerShell as I have all of my VMware management/jobs/utilies using it and find it very robust

Tuesday, March 03, 2009 9:56 AM by

# ???????????? ?????? ???????????????????? ???????????? ?????????????? ???????????? ?????????????????????????????? ????????????????????. « ShS's Blog

# PowerShell – Remote Service Manager « Kevin Pelgrims

Wednesday, February 24, 2010 5:03 AM by PowerShell – Remote Service Manager « Kevin Pelgrims

# ???????????? ?????? ???????????????????? ???????????? ??????????????????????????????????????????????????????????????????????????????????. - ShS's Blog

# [WSUS] ?????? ???????????????? ????, ?????????????? ?????? ?? ???????????? WSUS | ???????? - ???????? ???????????????? ?????? ????????????????

# В продолжении разборок со WSUS | Блог Бафа

Anonymous comments are disabled