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
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
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
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)
NM it looks fine now, recopied it and spaced it
thanks!
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?
Yes, I would definitely go for the Try/Catch option.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Okay I fixed it it up with a Catch / Try + If / Else loop works perfect , thanks for the input.
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 {$_}
}}