edm365f31
 Basic Member Posts:100

 |
| 29 Jun 2009 07:36 AM |
|
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. |
|
|
|
|
edm365f31
 Basic Member Posts:100

 |
| 29 Jun 2009 12: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
|
|
|
|
|
JKav
 New Member Posts:70

 |
| 29 Jun 2009 01: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). |
|
|
|
|
edm365f31
 Basic Member Posts:100

 |
| 30 Jun 2009 05:41 AM |
|
I am searching but maybe one of the POSH Gurus here might have an easy simple answer that I missed.. |
|
|
|
|
Marco Shaw
 Veteran Member Posts:1684

 |
|
JKav
 New Member Posts:70

 |
| 30 Jun 2009 06:47 AM |
|
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 Shaw
 Veteran Member Posts:1684

 |
| 30 Jun 2009 06:59 AM |
|
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... |
|
|
|
|
Poshoholic PowerShell MVP, Community Director
 Basic Member Posts:110

 |
| 30 Jun 2009 08:02 AM |
|
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 |
|
|
JKav
 New Member Posts:70

 |
| 30 Jun 2009 06:11 PM |
|
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. |
|
|
|
|
JKav
 New Member Posts:70

 |
| 02 Jul 2009 09:26 AM |
|
Poshoholic I have to ask; is there a reason gwmi -query versus the DependentService value returned from get-service ? |
|
|
|
|
JKav
 New Member Posts:70

 |
| 02 Jul 2009 10:41 AM |
|
Something like.... $test = gwmi win32_service -computerName "mssupport" | where {$_.Caption -eq "Telephony"} | Get-Service | Select DependentServices foreach ($depsvc in $test.DependentServices){ $depsvc.Name} |
|
|
|
|
Poshoholic PowerShell MVP, Community Director
 Basic Member Posts:110

 |
| 02 Jul 2009 06:18 PM |
|
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 |
|
|
JKav
 New Member Posts:70

 |
| 03 Jul 2009 12:52 PM |
|
I primarily use V1 and the Get-Service seems to work. |
|
|
|
|
Poshoholic PowerShell MVP, Community Director
 Basic Member Posts:110

 |
| 03 Jul 2009 12: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 |
|
|
JKav
 New Member Posts:70

 |
| 05 Jul 2009 03:04 PM |
|
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. |
|
|
|
|
JKav
 New Member Posts:70

 |
| 05 Jul 2009 03:50 PM |
|
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? |
|
|
|
|
Poshoholic PowerShell MVP, Community Director
 Basic Member Posts:110

 |
| 06 Jul 2009 05:48 AM |
|
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 |
|
|
JKav
 New Member Posts:70

 |
| 06 Jul 2009 05:54 AM |
|
Hmmm works like a charm local and remote... must have had a typo...... Thanks |
|
|
|
|