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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell | No Comment | 2,328 views | 15/07/2012 07:29

Function to test Wmi Objects

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function Test-WmiObject {
 
<#
    .SYNOPSIS
 
        Function to test Wmi Objects
 
    .EXAMPLE
 
        Test-WmiObject -NameSpace "root\virtualization" -WmiHost "hyperv01.yusufozturk.info"
 
#>
 
[CmdletBinding(SupportsShouldProcess = $true)]
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,
 
	[Parameter(
        Mandatory = $false,
        HelpMessage = 'XML output')]
    [switch]$OutXML = $false
)
	$ErrorActionPreference = "silentlycontinue"
 
	if (!$WmiHost)
	{
		$CheckWmiObject = Get-WmiObject -Computer "." -Namespace "$NameSpace" -List -EA SilentlyContinue
		if (!$CheckWmiObject)
		{
			$ResultCode = "-1"
			$ResultMessage = "Could not contact with Wmi Provider."
		}
		else
		{
			$ResultCode = "1"
			$ResultMessage = "Wmi Provider is available."
		}
	}
	else
	{
		$CheckWmiObject = Get-WmiObject -Computer "$WmiHost" -Namespace "$NameSpace" -List -EA SilentlyContinue
		if (!$CheckWmiObject)
		{
			$ResultCode = "-1"
			$ResultMessage = "Could not contact with Wmi Provider."
		}
		else
		{
			$ResultCode = "1"
			$ResultMessage = "Wmi Provider is available."
		}
	}
 
	if ($OutXML)
	{
		New-XML -ResultCode $ResultCode -ResultMessage $ResultMessage
	}
	else
	{
		$Properties = New-Object Psobject
		$Properties | Add-Member Noteproperty ResultCode $ResultCode
		$Properties | Add-Member Noteproperty ResultMessage $ResultMessage
		Write-Output $Properties
	}
}

Example Usage: Test-WmiObject -NameSpace “root\virtualization” -WmiHost “hyperv01.yusufozturk.info”