VMware Cloud Community
cgarciamoran
Contributor
Contributor
Jump to solution

Get-wmiobject Filter Output

Hey All

I was working on a piece of a script to discover certain services like SQL or Hyper-V and create a report that gets emailed. I'm a little stuck on this piece

$report = Get-VM | where { $_.Guest.OSFullName -like "*micro*" } | Select Name,

                           PowerState,

                           @{ N = "OS"; E = { $_.Guest.OSFullName } },

                           @{ N = "HyperV Present"; E = { Get-WmiObject -Class Win32_Service -Filter "Name LIKE '%Hyper%'" -ComputerName $_.Name -Credential $cred }

My only issue with this is that then the Services line has quite a few multiple instances for some services like HyperV, is there a way to just parse if the service is present and then do a "YES" instead? I was also thinking that on VM's that are offline the column would be blank.

Any Ideas?

thanks!

CGM

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = Get-VM | where { $_.Guest.OSFullName -like "*micro*" } |

Select Name,PowerState,

    @{ N = "OS"; E = { $_.Guest.OSFullName } },

    @{ N = "HyperV Present"; E = {if(Get-WmiObject -Class Win32_Service -Filter "Name LIKE '%Hyper%'" -ComputerName $_.Name -Credential $cred){'Yes'}else{'No'}}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = Get-VM | where { $_.Guest.OSFullName -like "*micro*" } |

Select Name,PowerState,

    @{ N = "OS"; E = { $_.Guest.OSFullName } },

    @{ N = "HyperV Present"; E = {if(Get-WmiObject -Class Win32_Service -Filter "Name LIKE '%Hyper%'" -ComputerName $_.Name -Credential $cred){'Yes'}else{'No'}}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Hmm looked like it worked at 1st but im getting all "NO" on VM's that I know have the Service or are offline (cause they cant be queried)

0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

NM it looks fine now, recopied it and spaced it

thanks!

0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Okay PowerCli peeps,

So I worked some more on this script and added some of the changes LucD suggested, it looks like im getting some false positives on systems that are down or Access Denied like these errors

Invalid query "select * from Win32_Service where Name LIKE '%HYPER%'"

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

So would it be better to use Try / Catch loops and write the Yes / No / Error instead?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, I would definitely go for the Try/Catch option.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Okay I fixed it it up with a Catch / Try + If / Else loop works perfect , thanks for the input.

$report = Get-VM | where-object { $_.Guest.OSFullName -like "*micro*" } | Select-object Name,

PowerState,

@{ N = "OS"; E = { $_.Guest.OSFullName } },

@{ N = "Hyper-V Present"; E = {

   Try { if (

   Get-WmiObject -Class Win32_Service -Filter "Name LIKE '%Hyper%'" -ComputerName $_.Name -Credential $cred -ErrorAction Stop) {'Yes'} else {'No'}

  }

   catch {$_}

  }}​

0 Kudos