function Test-WMI {
param(
[Parameter(Position = 0, Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[String]
$Namespace = "root"
,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$Class = "__Namespace"
,
[Parameter(Mandatory = $false)]
[switch]
$Quiet
)
begin {
[System.Environment]::ExitCode = -1
}
process {
$Properties = @{
Namespace = $Namespace
Class = $Class
Result = -1
}
try {
$CimInstance = (Get-CimInstance -ClassName $Class -Namespace $Namespace -ErrorAction Stop)
$Properties.Result = 0
}
catch {
$Properties.Result = $error[0].Exception.HResult
}
finally {
if ($CimInstance) {
$Properties.Result = 0
}
elseif (!$CimInstance -and $error[0].Exception.HResult -eq 0) {
$Properties.Result = 0x8004100E
}
else {
$Properties.Result = $error[0].Exception.HResult
}
}
}
end {
if ($Quiet) {
($Properties.Result -eq 0)
}
else {
$obj = New-Object -TypeName PSObject -Property $Properties
Write-Output -InputObject $obj
}
}
}