search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
How to check Wmi Object with PowerShell
Posted in Windows Powershell, Windows Server | No Comment | 5,090 views | 30/01/2012 01:19

Check-WmiObject allows you to check Wmi provider/interface on given server.
I’m using this function in SetLinuxVM 3.0 to decide Hyper-V Manager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Check WMI Object
# Author: Yusuf Ozturk
# http://www.yusufozturk.info
 
function Check-WmiObject
{
param (
    [Parameter(
        Mandatory = $true,
        HelpMessage = 'Wmi NameSpace. Example: root\virtualization')]
    [string]$NameSpace,
 
    [Parameter(
        Mandatory = $false,
        HelpMessage = 'Name of the Wmi Host. Example: Server01')]
    [string]$WMIHost
)
 
	$Success = "1";
	if (!$WMIHost)
	{
		$CheckWmiObject = Get-WmiObject -Computer "." -Namespace "$NameSpace" -List -EA SilentlyContinue
		if (!$CheckWmiObject)
		{
			Write-Debug "Could not contact with Wmi Provider."
		}
		else
		{
			Write-Debug "Wmi Provider is available."
			$Success
		}
	}
	else
	{
		$CheckWmiObject = Get-WmiObject -Computer "$WMIHost" -Namespace "$NameSpace" -List -EA SilentlyContinue
		if (!$CheckWmiObject)
		{
			Write-Debug "Could not contact with Wmi Provider."
		}
		else
		{
			Write-Debug "Wmi Provider is available."
			$Success
		}
	}
}

You can check Wmi Provider like this:

Check-WmiObject -NameSpace "root\virtualization"

If you want to query different wmi host:

Check-WmiObject -NameSpace "root\virtualization" -WmiHost "Server01"

NameSpace is mandatory but you don’t need to type WmiHost if you query localhost.

If you want to use it in a script, you can check it like this:

1
2
3
4
5
$Test = Check-WmiObject -NameSpace "root\virtualization"
if (!$Test)
{
	Write-Warning "Wmi Object is not available."
}

If checking is successful then you see “1” as output.



Leave a Reply