Modules/M365DSCStubsUtility.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
192
193
194
195
196
197
198
199
200
201
202
203
204
function New-M365DSCStubFiles
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $DestinationFilePath,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $GlobalAdminAccount
    )

    if ($null -eq $GlobalAdminAccount)
    {
        $GlobalAdminAccount = Get-Credential
        $PSBoundParameters.Add("GlobalAdminAccount", $GlobalAdminAccount)
    }

    if (Test-Path $DestinationFilePath)
    {
        $answer = $null
        do
        {
            $answer = Read-Host "A file already exists at the specified location. Remove it? (y/n)"
        } while ($answer -ne 'y' -and $answer -ne 'n')

        if ($answer -eq 'y')
        {
            Remove-Item -Path $DestinationFilePath -Confirm:$false
        }
    }

    $Modules = @(
        @{
            Platform     = 'AzureAD'
            ModuleName   = 'AzureADPreview'
            RandomCmdlet = 'Get-AzureADDirectorySetting'
        },
        @{
            Platform     = 'ExchangeOnline'
            ModuleName   = $null
            RandomCmdlet = 'Add-AvailabilityAddressSpace'
        },
        @{
            Platform   = 'Intune'
            ModuleName = "Microsoft.Graph.Intune"
        },
        @{
            Platform   = 'MicrosoftTeams'
            ModuleName = 'MicrosoftTeams'
        },
        @{
            Platform   = 'PnP'
            ModuleName = 'SharePointPnPPowerShellOnline'
        },
        @{
            Platform   = 'PowerPlatforms'
            ModuleName = 'Microsoft.PowerApps.Administration.PowerShell'
        },
        @{
            Platform     = 'SecurityComplianceCenter'
            ModuleName   = $null
            RandomCmdlet = 'Add-ComplianceCaseMember'
        },
        @{
            Platform     = 'SkypeForBusiness'
            ModuleName   = $null
            RandomCmdlet = 'Clear-CsOnlineTelephoneNumberReservation'
        }
    )
    $Content = ''
    foreach ($Module in $Modules)
    {
        Write-Host "Generating Stubs for {$($Module.Platform)}..."
        $CurrentModuleName = $Module.ModuleName
        if ($null -eq $CurrentModuleName)
        {
            $ConnectionMode = New-M365DSCConnection -Platform $Module.Platform `
                -InboundParameters $PSBoundParameters
            $foundModule = Get-Module | Where-Object -FilterScript { $_.ExportedCommands.Values.Name -ccontains $Module.RandomCmdlet }
            $CurrentModuleName = $foundModule.Name
        }
        else
        {
            Import-Module $CurrentModuleName -Force -ErrorAction SilentlyContinue
            $ConnectionMode = New-M365DSCConnection -Platform $Module.Platform `
                -InboundParameters $PSBoundParameters
        }

        $cmdlets = Get-Command -CommandType 'Cmdlet' | Where-Object -FilterScript { $_.Source -eq $CurrentModuleName }
        if ($null -eq $cmdlets)
        {
            $cmdlets = Get-Command -CommandType 'Function' | Where-Object -FilterScript { $_.Source -eq $CurrentModuleName }
        }

        try
        {
            $aliases = Get-Command -CommandType 'Alias' | Where-Object -FilterScript { $_.Source -eq $CurrentModuleName }
            $cmdlets += $aliases
            $cmdlets = $cmdlets | Select-Object -Unique
        }
        catch
        {
            Write-Verbose -Message $_
        }
        $StubContent = ''
        $i = 1
        foreach ($cmdlet in $cmdlets)
        {
            Write-Host $cmdlet
            Write-Progress -Activity "Generating Stubs" -Status $cmdlet.Name -PercentComplete (($i / $cmdlets.Length) * 100)

            try
            {
                $metadata = New-Object -TypeName System.Management.Automation.CommandMetaData -ArgumentList $cmdlet
                $parameters = $metadata.Parameters
            }
            catch
            {
                Write-Verbose -Message $_
            }

            $invalidParameters = @("ErrorVariable", `
                    "ErrorAction", `
                    "InformationVariable", `
                    "InformationAction", `
                    "WarningVariable", `
                    "WarningAction", `
                    "OutVariable", `
                    "OutBuffer", `
                    "PipelineVariable", `
                    "Verbose", `
                    "WhatIf", `
                    "Debug")

            $additionalParameters = (Get-Command $cmdlet.Name).Parameters

            foreach ($additionalParam in $additionalParameters.Keys)
            {
                if (-not $parameters.ContainsKey($additionalParam) -and `
                        -not $invalidParameters.Contains($additionalParameter))
                {
                    $parameters += @{$additionalParam = $additionalParameters.$additionalParam }
                }
            }
            $StubContent += "function $($cmdlet.Name)`n{`r`n [CmdletBinding()]`r`n param(`r`n"
            $invalidTypes = @("ActionPreference")

            $foundParamNames = @()
            foreach ($param in $parameters.Values)
            {
                if ($foundParamNames -notcontains $param.Name)
                {
                    $foundParamNames += $param.Name
                    if ($param.ParameterType.Name -notin $invalidTypes -and `
                            $param.Name -notin $invalidParameters -and `
                            -not [System.String]::IsNullOrEmpty($param.Name))
                    {
                        $StubContent += " [Parameter()]`r`n"
                        $ParamType = $param.ParameterType.ToString()
                        if ($ParamType -eq 'System.Collections.Generic.List`1[System.String]')
                        {
                            $ParamType = "System.String[]"
                        }
                        elseif ($ParamType -eq 'System.Nullable`1[System.Boolean]')
                        {
                            $ParamType = "System.Boolean"
                        }
                        $StubContent += " [$ParamType]`r`n"
                        $StubContent += " `$$($param.Name),`r`n`r`n"
                    }
                }
            }
            if ($parameters.Values.Count -gt 0)
            {
                $endOfString = $StubContent.SubString($StubContent.Length - 5, 5)
                if ($endOfString -eq ",`r`n`r`n")
                {
                    $StubContent = $StubContent.Remove($StubContent.Length - 5, 5)
                }
            }
            $StubContent += "`r`n )`r`n}`n"
            $i ++
        }
        Write-Progress -Activity "Generating Stubs" -Completed

        $Content += "#region $($Module.Platform)`r`n"

        $TypesToConvert = @('Microsoft.Online.SharePoint.PowerShell.SpoHubSitePipeBind', `
                'Microsoft.Online.SharePoint.PowerShell.SpoSitePipeBind'
        )

        foreach ($type in $TypesToConvert)
        {
            $StubContent = $StubContent.Replace($type, 'Object')
        }
        $Content += $StubContent
        $Content += "#endregion`r`n"
        Write-Host "Done" -ForegroundColor Green
    }
    $Content | Out-File $DestinationFilePath -Encoding utf8
}