Performing Get-Service with failback using Get-WMIObject ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 months and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Performing Get-Service with failback using Get-WMIObject ?

Post by ITEngineer »

Hi All,

How can I change the script below to make sure the service-checking mechanism first uses Get-Service and then Get-WMIObject as the fallback plan?

Because I'm getting the following error when executing the below cmdlet:
  1. Get-Service -ComputerName $_.DnsHostName | Where-Object { ($_.Name -like '*SVC1*') }
  2. Get-Service : Cannot open Service Control Manager on computer 'AppSVR1.com'. This operation might require other privileges.
cmdlet:
  1. Get-WmiObject -Class Win32_Service -ComputerName $_.DnsHostName | Where-Object { ($_.Name -like '*SVC1*') }
  2. Get-WmiObject : The RPC server is unavailable.
  3. This is What I've produced so far, so not sure if there is any better way in doing this ?
  1. $ous = Get-ADOrganizationalUnit -Filter "Name -like '*Application Servers*'" -SearchBase (Get-ADRootDSE).defaultNamingContext
  2. $onlineServers = $ous | ForEach-Object {
  3.     $servers = Get-ADComputer -SearchBase $_.DistinguishedName -Filter { (Enabled -eq $TRUE -and OperatingSystem -like 'Windows*Server*') }
  4.     $servers | ForEach-Object {
  5.  
  6.         Write-Host "Processing $($_.DnsHostName)" -ForegroundColor Yellow
  7.  
  8.         $MSMQStatus = (Get-Service -ComputerName $_.DnsHostName -ErrorAction SilentlyContinue | Where-Object { ($_.Name -like '*MSMQ*') }) -or (Get-WmiObject -Class Win32_Service -ComputerName $_.DnsHostName -ErrorAction SilentlyContinue | Where-Object { ($_.Name -like '*MSMQ*') })
  9.         If ( (Test-Connection -ComputerName $_.DnsHostName -Count 2 -Quiet) -and ( $MSMQStatus ) ) {
  10.             Write-Host "`t$($_) [$($([System.Net.Dns]::GetHostEntry($_.Name).AddressList.IPAddressToString -join ', '))]" -ForegroundColor Green
  11.             Write-Host "`t`t$($_) [$( $MSMQStatus.DisplayName -join ', ')]" -ForegroundColor DarkGreen
  12.         }
  13.     }
  14. }
  15.  
  16. $onlineServers | Out-GridView
The result is also not showing on the Out-GridView.

Thank you.

Thank you in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Performing Get-Service with failback using Get-WMIObject ?

Post by jvierra »

You do not have access rights on the services and on WMI. Please ask you server administrator for help with this.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Performing Get-Service with failback using Get-WMIObject ?

Post by ITEngineer »

jvierra wrote: Thu Mar 23, 2023 5:29 pm You do not have access rights on the services and on WMI. Please ask you server administrator for help with this.
Hi Mr. Vierra,

I am the Domain Admins, my user account is also part of the Enterprise ADministrator group.

So not sure why this is the issue.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Performing Get-Service with failback using Get-WMIObject ?

Post by jvierra »

Can be firewall or other network issues. PowerShell has nothing to do with the errors.

Who is you network engineer. Just having an admin account does not guarantee access to the service control manager and the RPC ports for WMI. Client computer firewalls block this access by default. Sometimes the server may not have these ports opened.
This topic is 11 months and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked