Private/ISE-Snippets.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
$Title       = "1.1 LAB - Script-Folder"
$Description = "Build a folder with script"
$SnippetText = @'
Function Ensure-Folder{
     
    $Folder = "C:\Logging"
 
    # Test-Resource
    If(Test-Path $Folder){
       "Folder present :-)"
    }
 
    # Set-Resource
    Else{
       "No folder Present!"
        New-Item -Path $Folder -ItemType Directory
    }
}
 
Ensure-Folder
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "1.2 LAB - Look at Built-In DSC Resources"
$Description = "Native DSC Resources in Windows"
$SnippetText = @'
Get-DscResource -Module PSDesiredStateConfiguration
 
Get-DscResource -Name "File"
Get-DscResource -Name "File" -Syntax
Get-DscResource -Name "File" | Select-Object -ExpandProperty Properties
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "1.3 LAB - Test Built-In DSC Resources"
$Description = "Native DSC Resources in Windows verification"
$SnippetText = @'
$ResourceParameters = @{
    DestinationPath = 'C:\Logging';
    Type = 'Directory';
}
 
Invoke-DscResource -Name "File" -Method Test -Property $ResourceParameters -ModuleName PSDesiredStateConfiguration -Verbose
 
Invoke-DscResource -Name "File" -Method Set -Property $ResourceParameters -ModuleName PSDesiredStateConfiguration -Verbose
 
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "2.1 LAB - Local Configuration Manager (LCM)"
$Description = "Configuring the DSC LCM"
$SnippetText = @'
#Look at LCM Properties
Get-DscLocalConfigurationManager
Get-DscLocalConfigurationManager | Select *
 
# Create LCM Properties: https://docs.microsoft.com/en-us/powershell/dsc/metaconfig
[DSCLocalConfigurationManager()]
configuration LCM
{
    Settings
    {
        ConfigurationMode = "ApplyAndAutoCorrect"
        RefreshMode = 'Push'
        DebugMode = 'All'
        RebootNodeIfNeeded = $true
    }
}
 
# Create mof
LCM -OutputPath $env:TEMP
 
ise $env:TEMP\localhost.meta.mof
 
# Configure the DSCLocalConfigurationManager
Set-DSCLocalConfigurationManager -Path $env:TEMP –Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "3.1 LAB - DSC-Folder"
$Description = "Build a folder with DSC"
$SnippetText = @'
Configuration DSCFolder {
 
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    File LoggingFolder {
        Ensure = "Present"
        DestinationPath = "C:\Logging"
        Type = "Directory"
    }
}
 
DSCFolder -OutputPath $env:TEMP
 
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "3.2 LAB - DSC-Folder Add dependencies"
$Description = "Build a folder with file DSC"
$SnippetText = @'
Configuration DSCFolder
{
 
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    # Create a directory
    File LoggingFolder {
        Ensure = "Present"
        Type = "Directory"
        DestinationPath = "C:\Logging"
    }
 
    # Create a file
    File LoggingFile {
        Ensure = "Present"
        DestinationPath = "C:\Logging\Log.txt"
        Contents = "Hello World!"
        DependsOn = "[File]LoggingFolder"
    }
}
 
DSCFolder -OutputPath $env:TEMP
 
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "4.1 LAB - DSC diagnostics"
$Description = "Troubleshooting DSC"
$SnippetText = @'
#NOTE: https://docs.microsoft.com/en-us/powershell/dsc/troubleshooting
 
Get-DscConfigurationStatus | Show-Object
 
$Status = Get-DscConfigurationStatus
$Status.ResourcesInDesiredState
$status.MetaConfiguration
 
Get-DscConfiguration
 
Get-WinEvent -LogName "Microsoft-Windows-Dsc/Operational"
         
Install-Module xDscDiagnostics -Force -Verbose -RequiredVersion 2.6.0
 
Update-xDscEventLogStatus -Channel Debug -Status Enabled
 
Get-xDscOperation
 
Trace-xDscOperation -JobId <JobId>
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "5.1 LAB - Certificate for DSC encyprion"
$Description = "Build a Docusign Encryption certificate"
$SnippetText = @'
$Params = @{
    Subject = "CN=DscEncryption"
    DnsName = "DscEncryption"
    FriendlyName = 'DSC Encryption Certifificate'
    TextExtension = @('2.5.29.37={text}1.3.6.1.4.1.311.80.1')
    KeyUsage = @('KeyEncipherment', 'DataEncipherment')
    CertStoreLocation = "Cert:\LocalMachine\My"
    KeyLength = 2048
    KeyAlgorithm = 'RSA'
    HashAlgorithm = 'SHA256'
    NotBefore = (Get-Date)
    NotAfter = (Get-Date).AddYears(2)
}
 
$cert = New-SelfSignedCertificate @Params
$cert | Export-Certificate –FilePath 'C:\DSCEncrypt.cer'
Import-Certificate -FilePath 'C:\DSCEncrypt.cer' -CertStoreLocation Cert:\LocalMachine\Root
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "5.2 LAB - Set LCM to use certificate"
$Description = "Use a Docusign Encryption certificate in LCM"
$SnippetText = @'
[DSCLocalConfigurationManager()]
Configuration LCM
{
    Param(
        $CertificateThumbprint
    )
 
    Settings
    {
        CertificateId = $CertificateThumbprint
    }
}
 
LCM -OutputPath $env:TEMP -CertificateThumbprint $cert.Thumbprint
Set-DscLocalConfigurationManager -Path $env:TEMP
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params

$Title       = "5.3 LAB - Encrypt configuration and test"
$Description = "Create an encrypted configuration and test on Node"
$SnippetText = @'
Configuration EncryptedFile
{
    Param(
        [PsCredential]$credential
    )
    
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    Node $AllNodes.NodeName
    {
        File EncryptedInfo
        {
            DestinationPath = "C:\EncryptTest.txt"
            Contents = $credential.GetNetworkCredential().password
            Credential = $credential
        }
    }
 
}
 
$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName = "localhost"
            PSDscAllowDomainUser = $true
            CertificateFile = "C:\DSCEncrypt.cer"
        }
    )
}
 
# Create MOF job
$Cred = Get-Credential -UserName localhost\administrator -Message "Admin Credential"
EncryptedFile -OutputPath $env:TEMP -Credential $Cred -ConfigurationData $ConfigurationData
 
# Verify encrypted MOF
ise "$env:TEMP\localhost.mof"
 
# Run the MOF job
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
 
# Get content of created file
Get-Content "C:\EncryptTest.txt"
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params