header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

How to Stop Service and dependent services on remote machine
Last Post 06 Jul 2009 02:54 PM by jkavanagh58. 17 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
edm365f31User is Offline
New Member
New Member
Posts:98
Avatar

--
29 Jun 2009 04:36 PM  
Is there a way to stop a service and its dependent service when using using wmi to query for the the service collection remotely.

$colItems=Gwmi Win32_Service -comp $server | Where-Object{$_.name -eq "IMAService"}

If ($colitems -eq "running") {
$colitems.stopservice() } # this fails because of the dependency services.
edm365f31User is Offline
New Member
New Member
Posts:98
Avatar

--
29 Jun 2009 09:56 PM  

I know u can do this
$colItems = gwmi win32_service | ? {$_.name -like "w32time*" -or $_.name -like "spooler*" -or $_.name -like "termservice*"}

I am thinking about:
if there is something equivalent to the cmdlet
stop-service -force





jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
29 Jun 2009 10:25 PM  
I know I can list all of the dependent services with something like:
Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='NetDDE'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
but I have not figured out how to get this to work with Powershell.

I am curious of the answer here because I have been working with a script to shutdown a list of services and then disable them but there are some services that the stop just places the service in degraded mode (Telephony and Remote Access Connection Manager).
edm365f31User is Offline
New Member
New Member
Posts:98
Avatar

--
30 Jun 2009 02:41 PM  
I am searching but maybe one of the POSH Gurus here might have an easy simple answer that I missed..
marco.shawUser is Offline
Site Moderator
Basic Member
Basic Member
Posts:457
Avatar

--
30 Jun 2009 03:31 PM  
Do you have to use PowerShell v1?

http://msdn.microsoft.com/en-us/library/microsoft.powershell.commands.getservicecommand.dependentservices(VS.85).aspx
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
30 Jun 2009 03:47 PM  
Shame stop-service doesn't work. So can I assume that the get-service call works against the bound gwmi piping to it?

gwmi win32_service -computerName "mssupport" | where {$_.Caption -eq "Telephony"} | Get-Service | Select DependentServices
marco.shawUser is Offline
Site Moderator
Basic Member
Basic Member
Posts:457
Avatar

--
30 Jun 2009 03:59 PM  
I'd have to play with all the different options. I tried a quick test and get-service does accept pipeline input in this case.

Another goodie in v2 is that stop-service has a -force parameter which the docs says will stop all dependent services...
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:94
Avatar

--
30 Jun 2009 05:02 PM  
PowerGUI lets you do this using the Network PowerPack. Below you will find the script contained within the Stop service action that makes this work. It takes Win32_Service objects as input, and it uses WMI to determine the list of dependent services that are running, makes sure that those services support stopping, notifies you of the services that it will stop and then stops the services. You will have to modify this if you want it to run unattended by removing the message box commands.

function Get-DependentService {
param(
$Service,
[string]$State = 'Running',
[string]$ComputerName = '.',
$Credential = $null,
[Switch]$DoNotCacheDependentServices,
[REF]$Reserved = ([REF]$null)
)

if ($Service -is [string]) {
if ($Credential) {
$Service = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name='$Service'" -ComputerName $ComputerName -Credential $Credential
} else {
$Service = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name='$Service'" -ComputerName $ComputerName
}
}

$dependentServicesCollection = New-Object System.Collections.ArrayList

if ((-not $Reserved.Value) -or ($Reserved.Value -isnot [System.Collections.Hashtable])) {
$Reserved.Value = @{}
}

$dependentServices = $null
if ($DoNotCacheDependentServices -or ($Reserved.Value.Keys -notcontains $Service.Name)) {
if ($Credential) {
$dependentServices = Get-WmiObject -Namespace root\cimv2 -Query "Associators of {Win32_Service.Name='$($Service.Name)'} Where AssocClass=Win32_DependentService Role=Antecedent" -ComputerName $Service.__SERVER -Credential $Credential | Where-Object {$_.State -eq $State}
} else {
$dependentServices = Get-WmiObject -Namespace root\cimv2 -Query "Associators of {Win32_Service.Name='$($Service.Name)'} Where AssocClass=Win32_DependentService Role=Antecedent" -ComputerName $Service.__SERVER | Where-Object {$_.State -eq $State}
}
$Reserved.Value[$Service.Name] = @{'Service'=$Service;'DependentServices'=$dependentServices}
} else {
$dependentServices = $Reserved.Value[$Service.Name].DependentServices
}

if ($dependentServices) {
foreach ($item in $dependentServices) {
$dependentServicesCollection.Insert(0,$item.Name) | Out-Null
foreach ($childItem in (Get-DependentService -Service $item -Credential $Credential -DoNotCacheDependentServices:$DoNotCacheDependentServices -Reserved ([REF]$Reserved.Value))) {
if ($dependentServicesCollection -contains $childItem.Name) {
$dependentServicesCollection.Remove($childItem.Name)
}
$dependentServicesCollection.Insert(0,$childItem.Name) | Out-Null
}
}
}

$dependentServicesCollection | ForEach-Object {$Reserved.Value[$_].Service}
}

$input | ForEach-Object {
if (-not $_.AcceptStop) {
[System.Windows.Forms.MessageBox]::Show("$($_.DisplayName) cannot be stopped because it does not support stopping.",'Unable to Stop Service',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
return
}

$dependentServices = Get-DependentService -Service $_ -ComputerName $_.__SERVER
if ($dependentServices) {
$dependentServiceNames = @($dependentServices | ForEach-Object {$_.DisplayName})
foreach ($item in $dependentServices) {
if (-not $item.AcceptStop) {
[System.Windows.Forms.MessageBox]::Show("$($_.DisplayName) cannot be stopped because it or one of the services that depends on it does not support stopping. The following services are dependent on $($_.DisplayName).`n`n$([string]::join(', ',$dependentServiceNames))",'Unable to Stop Service',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
return
}
}
$dialogResult = [System.Windows.Forms.MessageBox]::Show("When $($_.DisplayName) stops, these other services will also stop.`n`n$([string]::join(', ',$dependentServiceNames))`n`nDo you want to stop these services?",'Stop Other Services',[System.Windows.Forms.MessageBoxButtons]::YesNo,[System.Windows.Forms.MessageBoxIcon]::Warning)
if ($dialogResult -eq [System.Windows.Forms.DialogResult]::Yes) {
foreach ($item in $dependentServices) {
$item.StopService()
}
} else {
return
}
}
$_.StopService()
}
Kirk Munro [MVP]
Poshoholic

My blog: http://poshoholic.com
Follow me on Twitter: http://twitter.com/poshoholic
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
01 Jul 2009 03:11 AM  
Thanks for that. I love PowerGui but I have several hundred servers to run this against to include different DMZs plus need to include it with a server build procedures.
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
02 Jul 2009 06:26 PM  
Poshoholic I have to ask; is there a reason gwmi -query versus the DependentService value returned from get-service ?
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
02 Jul 2009 07:41 PM  
Something like.... $test = gwmi win32_service -computerName "mssupport" | where {$_.Caption -eq "Telephony"} | Get-Service | Select DependentServices
foreach ($depsvc in $test.DependentServices){
$depsvc.Name}
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:94
Avatar

--
03 Jul 2009 03:18 AM  
I totally get that you don't want to use the PowerGUI Admin Console UI for your work due to the environment you're working with. I was merely pointing out the script because it contains some of the things you were asking about earlier on the thread, in particular using Get-WmiObject with an associators query to retrieve dependent services.

Regarding using Get-WmiObject with an Associators query vs DependentService from Get-Service, it simply depends on the PowerShell version. When I wrote that functionality PowerShell v2 wasn't even in CTP1 yet, so I couldn't use the newer functionality. If you're ok using v2, go ahead and use DependentService and Get-Service with -ComputerName. I was just trying to point out how to get similar results using v1 with Get-WmiObject. At some point I'll update the PowerPack to use the newer method if it gives me everything I need because it will be less script to maintain, but I'm not at the point where I'm switching PowerPacks over to v2 just yet.
Kirk Munro [MVP]
Poshoholic

My blog: http://poshoholic.com
Follow me on Twitter: http://twitter.com/poshoholic
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
03 Jul 2009 09:52 PM  
I primarily use V1 and the Get-Service seems to work.
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:94
Avatar

--
03 Jul 2009 09:54 PM  
Sorry, wasn't being clear.  Get-Service doesn't handle remote management in v1.  In v2 sure, but not in v1.  I needed remote management and that's why I have all of that WMI script worked out.
Kirk Munro [MVP]
Poshoholic

My blog: http://poshoholic.com
Follow me on Twitter: http://twitter.com/poshoholic
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
06 Jul 2009 12:04 AM  
Ahhhhh, so even though I ran the get-service pipelined to the remote gwmi call it will only run it local (as per MS recommendations I am trying to stick with V1).  Thanks for the information.
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
06 Jul 2009 12:50 AM  
Just trying to understand all of the PowerGUI function but working with:
Get-WmiObject -Namespace root\cimv2 -Query "Associators of {Win32_Service.Name='$($Service.Name)'} Where AssocClass=Win32_DependentService Role=Antecedent" -ComputerName $Service.__SERVER

I wanted to run this, replacing the $Service variables with hard coded server name and Service Name of TapiSrv, just to see what the output would be but it constantly returns Invalid class error and the character position would be the whitespace after gwmi or get-wmiobject... what am I missing here?
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:94
Avatar

--
06 Jul 2009 02:48 PM  
Does this work for you?

gwmi -namespace root\cimv2 -query "Associators of {Win32_Service.Name='TapiSrv'} Where AssocClass=Win32_DependentService Role=Antecedent" -ComputerName localhost

If that works try it again replacing localhost with your computer name.
Kirk Munro [MVP]
Poshoholic

My blog: http://poshoholic.com
Follow me on Twitter: http://twitter.com/poshoholic
jkavanagh58User is Offline
New Member
New Member
Posts:33
Avatar

--
06 Jul 2009 02:54 PM  
Hmmm works like a charm local and remote... must have had a typo...... Thanks
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer