CimPSDrive.psm1

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using namespace Microsoft.PowerShell.SHiPS

[SHiPSProvider()]
class CMRoot : SHiPSDirectory
{
    # static member to keep track of CIM sessions
    static [System.Collections.Generic.List``1[Microsoft.Management.Infrastructure.CimSession]] $sessions
    
    # Default constructor
    CMRoot([string]$name):base($name)
    {
    }

    [object[]] GetChildItem()
    {
        $obj = @()

        # If sessions are present, create machine object for those sessions
        if([CMRoot]::sessions){
            [CMRoot]::sessions | ForEach-Object {
                $obj += [CMMachine]::new($_.ComputerName, $_)
            }
        }
        # Else create a default session for localhost
        else{
            $obj += [CMMachine]::new('localhost')
        }
        return $obj
    }
}

[SHiPSProvider()]
class CMMachine : SHiPSDirectory
{
    [Microsoft.Management.Infrastructure.CimSession]$Cimsession = $null

    # Given a machine name, create a cimsession and add to static member
    CMMachine([string]$name):base($name)
    {
        $this.CimSession = New-CimSession -ComputerName $name
        [CMRoot]::Sessions += $this.CimSession
    }

    # Given a cimsession, add to static member
    CMMachine([string]$name, [Microsoft.Management.Infrastructure.CimSession]$cimsession):base($name)
    {
        $this.CimSession = $cimsession
    }

    [object[]] GetChildItem()
    {
        $obj = @()
        
        # Find all the namespace under root namespace
        $namespaces = (Get-CimInstance -Namespace root -ClassName __namespace -CimSession $this.CimSession).Name | Sort-Object
        foreach ($namespace in $namespaces) {
            $obj += [CMNamespace]::new('root',$namespace,$this.CimSession)
        }
        return $obj
    }
}

[SHiPSProvider()]
class CMNamespace : SHiPSDirectory
{
    [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null
    [string]$Namespace = 'root'
    [string]$Type = 'Namespace'

    CMNamespace([string]$parent, [string]$name, [Microsoft.Management.Infrastructure.CimSession]$cimsession):base($name)
    {
        $this.Namespace = Join-Path $parent $name
        $this.CimSession = $cimsession
    }
     
    [object[]] GetChildItem()
    {
        $obj = @()

        # Find all the child namespaces
        $namespaces = (Get-CimInstance -Namespace $this.Namespace -ClassName __namespace -CimSession $this.CimSession).Name | Sort-Object
        $namespacesToSkip = @('ms_409')
        foreach ($namespace in $namespaces) {
            if($namespacesToSkip -notcontains $namespace){
                $obj += [CMNamespace]::new($this.Namespace, $namespace, $this.CimSession)
            }
        }
        
        # Find all the classes in the given namespace
        $classNamesToSkip = @('ms_409'
                              'CIM_Indication'
                              'CIM_ClassIndication'
                              'CIM_ClassDeletion'
                              'CIM_ClassCreation'
                              'CIM_ClassModification'
                              'CIM_InstIndication'
                              'CIM_InstCreation'
                              'CIM_InstModification'
                              'CIM_InstDeletion'
                              'CIM_Error'
                              'MSFT_WmiError'
                              'MSFT_ExtendedStatus'
                              'WMI_Extension'
                            )

        $classnames = (Get-CimClass -Namespace $this.Namespace -CimSession $this.CimSession).CimClassName | Sort-Object

        foreach($classname in $classnames){
            if(-not ($classname.StartsWith('__')) -and ($classNamesToSkip -notcontains $classname)){
                $obj += [CMClass]::new($this.Namespace, $classname, $this.CimSession)
            }
        }

        return $obj
    }
}

[SHiPSProvider()]
class CMClass : SHiPSDirectory
{
    [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null
    [string]$Namespace = $null
    [string]$Type = 'Class'

    CMClass([string]$namespace, [string]$name, [Microsoft.Management.Infrastructure.CimSession]$cimsession):base($name)
    {
        $this.Namespace = $namespace
        $this.CimSession = $cimsession
    }
     
    [object[]] GetChildItem()
    {
        try{
            return Get-CimInstance -ClassName $this.name -Namespace $this.Namespace -CimSession $this.Cimsession -ErrorAction Stop
        }

        catch [Microsoft.Management.Infrastructure.CimException]{
            if($_.FullyQualifiedErrorId -like 'HRESULT 0x80338102*'){
                Write-Verbose -Verbose -Message 'No instances for this class'
                return $null  
            }
            else{
                throw $_
            }
        } 
    }    
}

#region Cmdlets
function Get-CMSession{
    param(
        [Parameter(Mandatory)]
        [string]$ComputerName
    )    
    [CMRoot]::Sessions | Where-Object {$_.ComputerName -eq $ComputerName}
}

function Connect-CIM{
    param(
        [Parameter(Mandatory)]
        [string]$ComputerName,
        [pscredential]$Credential
    )
    if(Get-CMSession -ComputerName $ComputerName){
        Write-Verbose -Verbose -Message "Already connected to $ComputerName. Skipping ..."
    }
    else{
        ([CMRoot]::Sessions).Add((New-CimSession -ComputerName $ComputerName -Credential $Credential))        
    }
}

function Disconnect-CIM{
    param(
        [Parameter(Mandatory)]
        [string]$ComputerName
    )
    $sessionToRemove = Get-CMSession -ComputerName $ComputerName

    if($sessionToRemove){
        if(([CMRoot]::Sessions).Remove($sessionToRemove)){
            Remove-CimSession -CimSession $sessionToRemove -ErrorAction Stop
        }
    }
    else{
        Write-Verbose -Verbose -Message "No connection to $ComputerName. Skipping ..."        
    }
}

Export-ModuleMember -Function Connect-CIM,Disconnect-CIM
#endregion