functions/ConvertTo-PuppetResourceApi.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Function ConvertTo-PuppetResourceApi {
  <#
    .SYNOPSIS
      Collate the information about a DSC resource for building a Puppet resource_api type and provider
 
    .DESCRIPTION
      This function takes a DSC resource and returns the representation of that resource for the Puppet
      Resource API types and providers as a PowerShell object for further use.
 
    .PARAMETER DscResource
      The DscResourceInfo object to convert; can be passed via the pipeline, normally retrieved
      via calling Get-DscResource.
 
    .PARAMETER Name
      If not passing a full object, specify the name of the DSC Resource to retrieve and convert.
 
    .PARAMETER Module
      If not passing a full object, specify the module name of the the DSC Resource to retrieve and convert.
      Can be either a string or a hash containing the keys ModuleName and ModuleVersion.
 
    .EXAMPLE
      Get-DscResource -Name PSRepository | ConvertTo-PuppetResourceApi -OutVariable Foo
 
      Retrieve the representation of a Puppet Resource API type and provider from a DSC Resource object.
 
    .EXAMPLE
      ConvertTo-PuppetResourceApi -Name PSRepository
 
      Retrieve the representation of a Puppet Resource API type by searching for a DSC resource object via
      Get-DscResource. Will ONLY find the resource if it is in the PSModulePath.
 
    .NOTES
      This function currently takes EITHER:
 
      1. A DscResource Object, as passed by Get-DSCResource
      2. A combo of name/module to retrieve DSC Resources from
  #>

  [CmdletBinding(
    DefaultParameterSetName='ByObject'
  )]
  Param(
    [Parameter(
      ValueFromPipeline,
      ParameterSetName = 'ByObject'
    )]
    [Microsoft.PowerShell.DesiredStateConfiguration.DscResourceInfo[]]$DscResource,

    [Parameter(
      ValueFromPipelineByPropertyName,
      ParameterSetName = 'ByProperty'
    )]
    [string[]]$Name,

    [Parameter(
      ValueFromPipelineByPropertyName,
      ParameterSetName = 'ByProperty'
    )]
    [object]$Module
  )

  Begin{}

  Process {
    # Retrieve the DSC Resource information from the system unless passed directly
    If ($null -eq $DscResource) {
      if ($null -eq $Module) {
        $DscResourceToProcess = Get-DscResourceTypeInformation -Name $Name
      } else {
        $DscResourceToProcess = Get-DscResourceTypeInformation -Name $Name -Module $Module
      }
    } Else {
      $DscResourceToProcess = $DscResource
      If ($null -eq $DscResourceToProcess.ParameterInfo) { $DscResourceToProcess = Get-DscResourceTypeInformation -DscResource $DscResourceToProcess}
    }
    ForEach ($Resource in $DscResourceToProcess) {
      $PuppetizedName = "dsc_$($Resource.Name.ToLowerInvariant())"
      [PSCustomObject]@{
        Name         = $PuppetizedName
        RubyFileName = "$PuppetizedName.rb"
        Version      = [string]$Resource.Version
        Type         = Get-TypeContent -DscResource $Resource
        Provider     = Get-ProviderContent -DscResource $Resource
      }
    }
  }

  End {}
}