internal/functions/Initialize-DscResourceCimClass.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
Function Initialize-DscResourceCimClass {
  <#
    .SYNOPSIS
      Invoke a DSC Resource once to load its CIM class
    .DESCRIPTION
      Invoke a DSC Resource once to load its CIM class into the CIM namespace
      for DSC, allowing other commands to introspect that CIM class.
    .EXAMPLE
      Initialize-DscResourceCimClass -Name PSRepository -ModuleName PowerShellGet -ModuleVersion 2.1.3
      This will assemble the minimum parameters required to invoke the PSRepository resource with the
      Get method, ignoring any/all errors and then do so. Once the invocation has completed, the CIM
      Class is then loaded for other commands to introspect.
  #>

  [cmdletbinding()]
  param(
    [string]$Name,
    [string]$ModuleName,
    [string]$ModuleVersion
  )
  $InvokeParams = @{
    Name       = $Name
    Method     = 'Get'
    Property   = @{foo = 1}
    ModuleName = @{
      ModuleName    = $ModuleName
      ModuleVersion = $ModuleVersion
    }
    ErrorAction = 'Stop'
  }
  Try {
    Invoke-DscResource @InvokeParams
  } Catch {
    # We only care if the resource can't be found, not if it fails while executing
    if ($_.Exception.Message -match '(Resource \w+ was not found|The PowerShell DSC resource .+ does not exist at the PowerShell module path nor is it registered as a WMI DSC resource)') {
      $PSCmdlet.ThrowTerminatingError($_)
    }
  }
}