Public/CimObject.ps1

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
<#
.SYNOPSIS
    Test the value of a CimObject Property.
.DESCRIPTION
    Test the value of a CimObject Property. The Class can be provided with the Namespace. See Example.
.PARAMETER Target
    Specifies the name of the CIM class for which to retrieve the CIM instances. Can be just the ClassName
    in the default namespace or in the form of namespace/className to access other namespaces.
.PARAMETER Property
    Specifies an instance property to retrieve.
.PARAMETER Should
    A Script Block defining a Pester Assertion.
.EXAMPLE
    CimObject Win32_OperatingSystem SystemDirectory { Should Be C:\WINDOWS\system32 }
.EXAMPLE
    CimObject root/StandardCimv2/MSFT_NetOffloadGlobalSetting ReceiveSideScaling { Should Be Enabled }
.NOTES
    Assertions: Be, BeExactly, Match, MatchExactly
#>

function CimObject {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position=1)]
        [Alias("ClassName")]
        [string]$Target,

        [Parameter(Mandatory, Position=2)]
        [string]$Property,

        [Parameter(Mandatory, Position=3)]
        [scriptblock]$Should
    )


    $expression = $null
    $class = $null
    $namespace = $null
    if ($Target -match '/')
    {
        $lastIndexOfSlash = $Target.LastIndexOf('/')

        $class = $Target.Substring($lastIndexOfSlash + 1)
        $namespace = $Target.Substring(0,$lastIndexOfSlash)

        $PSBoundParameters["Target"] = $class
        $PSBoundParameters.Add("Qualifier", $namespace)

        $expression = {Get-CimInstance -ClassName $Target -Namespace $Qualifier}
    }
    else
    {
        $expression = {Get-CimInstance -ClassName $Target}
    }

    if ($Property -like '*.*' -or $Property -like '*(*' -or $Property -like '*)*') {
      $PSBoundParameters.Remove("Property")
      $PSBoundParameters.Add("PropertyExpression", $Property)
      $params = Get-PoshspecParam -TestName CimObject -TestExpression $expression @PSBoundParameters
    }

    else
    {
      $params = Get-PoshspecParam -TestName CimObject -TestExpression $expression @PSBoundParameters
    }


    Invoke-PoshspecExpression @params
}