AzureAutomationAuthoringToolkitInner.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<#
        Learn more here: http://aka.ms/azureautomationauthoringtoolkit
#>


$script:ConfigurationFileName = "AzureAutomationAuthoringToolkitConfig.json"
$script:ConfigurationPath = "$env:USERPROFILE\$script:ConfigurationFileName"
$script:LocalAssetsPath = "$PSScriptRoot\LocalAssets.json"
$script:SecureLocalAssetsPath = "$PSScriptRoot\SecureLocalAssets.json"

$script:StartProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn = "# Start AzureAutomationISEAddOn snippet"
$script:EndProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn = "# End AzureAutomationISEAddOn snippet"

$script:IseAddonPath = "\ISEaddon\AutomationISE.dll"

$script:PowerShellToLoadAzureAutomationIseAddOnGeneric = @"
`n
# Start AzureAutomationISEAddOn snippet
Import-Module AzureAutomationAuthoringToolkit
# End AzureAutomationISEAddOn snippet
"@


$script:IseProfileFileName = "Microsoft.PowerShellISE_profile.ps1"

function _findObjectByName {
    param(
        [object] $ObjectArray,
        [string] $Name
    )
    
    $ObjectArray | Where-Object {
        return $_.Name -eq $Name
    }
}

function _DecryptValue {
    param(
        [object] $Value,
        [switch] $SupressCouldNotDecryptWarning
    )

    $Configuration = Get-AzureAutomationAuthoringToolkitConfiguration

    if($Value -isnot [string]) {
        ## the local assets files store all encrypted values as strings, so if value is not a string, it is not an encrypted value.
        ## in this case, return the raw value
    
        Write-Verbose "AzureAutomationAuthoringToolkit: Value is not encrypted. Returning raw value without decrypting"
        return $Value
    }
    elseif($Configuration.EncryptionCertificateThumbprint -eq "none") {
        Write-Verbose "AzureAutomationAuthoringToolkit: No encryption certificate specified. Returning raw value without decrypting"
        return $Value
    }
    else {
        $Thumbprint = $Configuration.EncryptionCertificateThumbprint
        
        Write-Verbose "AzureAutomationAuthoringToolkit: Decrypting encrypted value '$Value' using encryption certificate with thumbprint '$Thumbprint'"

        if (Test-Path -Path Cert:\CurrentUser\My\$Thumbprint) {
            $Cert = Get-Item -Path Cert:\CurrentUser\My\$Thumbprint
            
            try {
                $Encrypted = [Convert]::FromBase64String($Value)
                $Bytes = $Cert.PrivateKey.Decrypt($Encrypted, $True)
                $EncryptedValue = [Text.Encoding]::UTF8.GetString($Bytes)

                # the encrypted value is a JSON string (so that we can encrypt non-string types), so convert it back to a proper object
                $EncryptedValue = ConvertFrom-Json -InputObject $EncryptedValue

                return $EncryptedValue
            }
            catch {
                
                if(!$SupressCouldNotDecryptWarning.IsPresent) {
                    Write-Warning "AzureAutomationAuthoringToolkit: Warning - Could not decrypt value '$Value' using encryption certificate with thumbprint '$Thumbprint'.
                    Returning raw value instead. Are you sure the value was encrypted with this certificate?"

                }

                return $Value 
            }
        }
        else { 
            throw "Encryption certificate with thumbprint '$Thumbprint' is not installed in the user cert store"
        }
    }
}

function _EncryptValue {
    param(
        [object] $Value
    )

    $Configuration = Get-AzureAutomationAuthoringToolkitConfiguration

    if($Configuration.EncryptionCertificateThumbprint -eq "none") {
        Write-Verbose "AzureAutomationAuthoringToolkit: No encryption certificate specified. Not encrypting value"
        return $Value
    }
    else {
        $Thumbprint = $Configuration.EncryptionCertificateThumbprint
        
        Write-Verbose "AzureAutomationAuthoringToolkit: Encrypting value using encryption certificate with thumbprint '$Thumbprint'"

        if (Test-Path -Path Cert:\CurrentUser\My\$Thumbprint) {
                       
            # convert the value to a JSON string so that we don't lost type info when decrypting
            $JsonValue = ConvertTo-Json -InputObject $Value -Depth 999

            $Cert = Get-Item -Path Cert:\CurrentUser\My\$Thumbprint
            $Bytes = [Text.Encoding]::UTF8.GetBytes($JsonValue)
            $Encrypted = $Cert.PublicKey.Key.Encrypt($Bytes, $True)
            $Value = [Convert]::ToBase64String($Encrypted)

            return $Value
        }
        else { 
            throw "Encryption certificate with thumbprint '$Thumbprint' is not installed in the user cert store"
        }
    }
}

<#
    .SYNOPSIS
    Copies configuration file for AzureAutomationAuthoringToolkit to user's profile directory
#>

function Copy-ConfigFile
{
    Write-Verbose "AzureAutomationAuthoringToolkit: Copying '$script:ConfigurationFileName' configuration file to '$script:ConfigurationPath'"

    $ConfigurationFile = Get-Item -Path $script:ConfigurationPath -ErrorAction SilentlyContinue

    if(!$ConfigurationFile) {
        Copy-Item -Path (Join-Path $PSScriptRoot $script:ConfigurationFileName) -Destination $script:ConfigurationPath
    }
    else {
        Write-Verbose "AzureAutomationAuthoringToolkit: '$script:ConfigurationPath' already present. Not copying file."
    }
}

<#
        .SYNOPSIS
        Sets up the Azure Automation ISE add-on for use in the PowerShell ISE.
#>

function Install-AzureAutomationIseAddOn {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    
    $IseProfilePath = Join-Path (Split-Path $Profile) $script:IseProfileFileName

    if(!(Test-Path $IseProfilePath)) {
        Write-Verbose "AzureAutomationAuthoringToolkit: '$IseProfilePath' does not exist, creating it"
        New-Item -Path $IseProfilePath -ItemType File -Force | Out-Null
    }

    $ProfileContent = Get-Content $IseProfilePath -Raw
    if(!$ProfileContent) { $ProfileContent = "" }
    $StartProfileSnippetIndex = $ProfileContent.IndexOf($script:StartProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn)

    # add content to PS ISE profile to load ISe add on on start up
    Write-Verbose "AzureAutomationAuthoringToolkit: Adding content to '$IseProfilePath' to cause ISE add-on to load on PowerShell ISE start up"

    if($StartProfileSnippetIndex -eq -1) {
        $IsRunningInISE = (Split-Path $Profile -Leaf) -eq $script:IseProfileFileName
    
        # add loading of the ISE add-on into the PS ISE Profile so it is automatically loaded each time the ISE is opened
        $IseAddOnModuleFolderPath = Split-Path $PSScriptRoot -Parent
    
        $PowerShellToLoadAzureAutomationIseAddOnWithPath = $script:PowerShellToLoadAzureAutomationIseAddOnGeneric -f $IseAddOnModuleFolderPath, $script:IseAddonPath
    
        Add-Content $IseProfilePath $PowerShellToLoadAzureAutomationISEAddOnWithPath

        if($IsRunningInISE) {
            # load the ISE add-on into the PS ISE session already open
            Write-Verbose "AzureAutomationAuthoringToolkit: Loading ISE add-on into this PowerShell ISE session"
            Invoke-Expression $PowerShellToLoadAzureAutomationIseAddOnWithPath
        }
    }
    else {
         Write-Verbose "AzureAutomationAuthoringToolkit: Content already present. Not adding any content."
    }
}

<#
        .SYNOPSIS
        Removes the Azure Automation ISE add-on from the PowerShell ISE.
#>

function Uninstall-AzureAutomationIseAddOn {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    
    $IseProfilePath = Join-Path (Split-Path $Profile) $script:IseProfileFileName
    $ProfileContent = Get-Content $IseProfilePath -Raw
    
    $StartProfileSnippetIndex = $ProfileContent.IndexOf($script:StartProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn)
    $EndProfileSnippetIndex = $ProfileContent.IndexOf($script:EndProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn)

    if($StartProfileSnippetIndex -gt -1 -and $EndProfileSnippetIndex -gt -1) {
        Write-Verbose "AzureAutomationAuthoringToolkit: Removing content from '$IseProfilePath' to cause ISE add-on to no longer load on PowerShell ISE start up"
        
        $NewProfileContent = $ProfileContent.Substring(0, $StartProfileSnippetIndex)
        $NewProfileContent += $ProfileContent.Substring($EndProfileSnippetIndex + $script:EndProfileSnippetForPowerShellToLoadAzureAutomationISEAddOn.Length)

        $NewProfileContent | Set-Content $IseProfilePath
    }
}

<#
        .SYNOPSIS
        Get a local certificate based on its thumbprint, as part of the Azure Automation Authoring Toolkit.
        Not meant to be called directly.
#>

function Get-AzureAutomationAuthoringToolkitLocalCertificate {
    param(
        [Parameter(Mandatory=$True)]
        [string] $Name,
        
        [Parameter(Mandatory=$True)]
        [string] $Thumbprint
    )
    
    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local certificate with thumbprint '$Thumbprint'"
            
    try {
        $Certificate = Get-Item ("Cert:\CurrentUser\My\" + $Thumbprint) -ErrorAction Stop
        Write-Output $Certificate
    }
    catch {
        Write-Error "AzureAutomationAuthoringToolkit: Certificate asset '$Name' referenced certificate with thumbprint
        '$Thumbprint' but no certificate with that thumbprint exist on the local system."

                
        throw $_
    }
}

<#
        .SYNOPSIS
        Get local assets defined for the Azure Automation Authoring Toolkit. Not meant to be called directly.
#>

function Get-AzureAutomationAuthoringToolkitLocalAsset {
    param(
        [Parameter(Mandatory=$True)]
        [ValidateSet('Variable', 'Certificate', 'PSCredential', 'Connection')]
        [string] $Type,

        [Parameter(Mandatory=$True)]
        [string]$Name
    )

    $Configuration = Get-AzureAutomationAuthoringToolkitConfiguration

    if($Configuration.LocalAssetsPath -eq "default") {
        Write-Verbose "Grabbing local assets from default location '$script:LocalAssetsPath'"
    }
    else {
        $script:LocalAssetsPath = $Configuration.LocalAssetsPath
        Write-Verbose "Grabbing local assets from user-specified location '$script:LocalAssetsPath'"
    }

    if($Configuration.SecureLocalAssetsPath -eq "default") {
        Write-Verbose "Grabbing secure local assets from default location '$script:SecureLocalAssetsPath'"
    }
    else {
        $script:SecureLocalAssetsPath = $Configuration.SecureLocalAssetsPath
        Write-Verbose "Grabbing secure local assets from user-specified location '$script:SecureLocalAssetsPath'"
    }
    
    $LocalAssetsError = "AzureAutomationAuthoringToolkit: AzureAutomationAuthoringToolkit local assets defined in
    '$script:LocalAssetsPath' is incorrect. Make sure the file exists, and it contains valid JSON."


    $SecureLocalAssetsError = "AzureAutomationAuthoringToolkit: AzureAutomationAuthoringToolkit secure local assets defined in
    '$script:SecureLocalAssetsPath' is incorrect. Make sure the file exists, and it contains valid JSON."

    
    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local value for $Type asset '$Name.'"
      
    try {
        $LocalAssets = Get-Content $script:LocalAssetsPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
    }
    catch {
        Write-Error $LocalAssetsError
        throw $_
    }

    try {
        $SecureLocalAssets = Get-Content $script:SecureLocalAssetsPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
    }
    catch {
        Write-Error $SecureLocalAssetsError
        throw $_
    }

    $Asset = _findObjectByName -ObjectArray $LocalAssets.$Type -Name $Name
    $AssetWasInSecureLocalAssets = $False

    if($Asset) {
        Write-Verbose "AzureAutomationAuthoringToolkit: Found local value for $Type asset '$Name.'"
        $AssetWasInSecureLocalAssets = $False
    }
    else {
        $Asset = _findObjectByName -ObjectArray $SecureLocalAssets.$Type -Name $Name

        if($Asset) {
            Write-Verbose "AzureAutomationAuthoringToolkit: Found secure local value for $Type asset '$Name.'"
            $AssetWasInSecureLocalAssets = $True
        }
    }

    if($Asset) {
        if($Type -eq "Certificate") {
            $AssetValue = Get-AzureAutomationAuthoringToolkitLocalCertificate -Name $Name -Thumbprint $Asset.Thumbprint
        }
        elseif($Type -eq "Variable") {
            
            if($Asset.Value -eq $Null) {
                Write-Warning "AzureAutomationAuthoringToolkit: Warning - Local Variable asset '$Name' has a value of null.
                If this was not intended, update its value in your local assets. "

            }
            
            if($AssetWasInSecureLocalAssets) {
                $AssetValue = (_DecryptValue -Value $Asset.Value)
            }
            else {
                $AssetValue = $Asset.Value
            }

        }
        elseif($Type -eq "Connection") {
            # Convert PSCustomObject to Hashtable
            $Temp = @{}

            $Asset.ValueFields.psobject.properties | ForEach-Object {
                
                if($_.Value -eq $Null) {
                    Write-Warning ("AzureAutomationAuthoringToolkit: Warning - Local Connection asset '$Name' has a null value for field '" + $_.Name + "'.
                    If this was not intended, update this connection field value in your local assets."
)
                }
                
                # even though all connection fields may not be encrypted, try to decrypt them all since we don't know which are encrypted and which are not.
                # if decryption fails (because field was not encrypted), supress the warning that decryption failed, since could be perfectly fine in this case.
                # when decryption fails, _DecryptValue returns the raw value, which is what we want
                $Temp."$($_.Name)" = (_DecryptValue -Value $_.Value -SupressCouldNotDecryptWarning)
            }

            $AssetValue = $Temp
        }
        elseif($Type -eq "PSCredential") {
            
            if($Asset.Password -eq $Null) {
                Write-Warning "AzureAutomationAuthoringToolkit: Warning - Local PSCredential asset '$Name' has a password value of null.
                If this was not intended, update its password value in your local assets. "

            }
            
            $AssetValue = @{
                Username = $Asset.Username
                Password = (_DecryptValue -Value $Asset.Password)
            }

        }

        Write-Output $AssetValue
    }
    else {
        Write-Verbose "AzureAutomationAuthoringToolkit: Local value for $Type asset '$Name' not found." 
        Write-Warning "AzureAutomationAuthoringToolkit: Warning - Local value for $Type asset '$Name' not found."
    }
}

<#
        .SYNOPSIS
        Get the configuration for the Azure Automation Authoring Toolkit. Not meant to be called directly.
#>

function Get-AzureAutomationAuthoringToolkitConfiguration {       
    $ConfigurationError = "AzureAutomationAuthoringToolkit: AzureAutomationAuthoringToolkit configuration defined in
        '$script:ConfigurationPath' is incorrect. Make sure the file exists, contains valid JSON, and contains 'LocalAssetsPath',
    'SecureLocalAssetsPath', and 'EncryptionCertificateThumbprint' settings."


    Write-Verbose "AzureAutomationAuthoringToolkit: Grabbing AzureAutomationAuthoringToolkit configuration from '$script:ConfigurationPath'"

    try {
        $ConfigurationTemp = Get-Content $script:ConfigurationPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
    }
    catch {
        Write-Error $ConfigurationError
        throw $_
    }

    $Configuration = @{}
    $ConfigurationTemp | ForEach-Object {
        $Key = $_.Name
        $Value = $_.Value

        $Configuration.$Key = $Value
    }

    if(!($Configuration.LocalAssetsPath -and $Configuration.SecureLocalAssetsPath -and $Configuration.EncryptionCertificateThumbprint)) {
        throw $ConfigurationError
    }

    Write-Output $Configuration
}

<#
        .SYNOPSIS
        Get a variable asset from Azure Automation.
        Part of the Azure Automation Authoring Toolkit to help author runbooks locally.
#>

function Get-AutomationVariable {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    [OutputType([Object])]
    
    param(
        [Parameter(Mandatory=$true)]
        [string] $Name
    )

    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local variable asset with name '$Name'"

    $AssetValue = Get-AzureAutomationAuthoringToolkitLocalAsset -Type Variable -Name $Name

    Write-Output $AssetValue
}

<#
        .SYNOPSIS
        Get a connection asset from Azure Automation.
        Part of the Azure Automation Authoring Toolkit to help author runbooks locally.
#>

function Get-AutomationConnection {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    [OutputType([Hashtable])]
    
    param(
        [Parameter(Mandatory=$true)]
        [string] $Name
    )

    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local connection asset with name '$Name'"

    $AssetValue = Get-AzureAutomationAuthoringToolkitLocalAsset -Type Connection -Name $Name

    Write-Output $AssetValue
}

<#
        .SYNOPSIS
        Set the value of a variable asset in Azure Automation.
        Part of the Azure Automation Authoring Toolkit to help author runbooks locally.
#>

function Set-AutomationVariable {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    
    param(
        [Parameter(Mandatory=$true)]
        [string] $Name,

        [Parameter(Mandatory=$true)]
        [object] $Value
    )

    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local variable asset with name '$Name'"

    $LocalAssetValue = Get-AzureAutomationAuthoringToolkitLocalAsset -Type Variable -Name $Name

    if($LocalAssetValue) {
        $LocalAssets = Get-Content $script:LocalAssetsPath -Raw | ConvertFrom-Json
        $SecureLocalAssets = Get-Content $script:SecureLocalAssetsPath -Raw | ConvertFrom-Json

        $LocalAssets.Variable | ForEach-Object {
            if($_.Name -eq $Name) {
                $_.Value = $Value
                $_.LastModified = Get-Date -Format u
            }
        }

        $SecureLocalAssets.Variable | ForEach-Object {
            if($_.Name -eq $Name) {
                $_.Value = (_EncryptValue -Value $Value)
                $_.LastModified = Get-Date -Format u
            }
        }

        Write-Verbose "AzureAutomationAuthoringToolkit: Setting value of local variable asset with name '$Name'"

        Set-Content $script:LocalAssetsPath -Value (ConvertTo-Json -InputObject $LocalAssets -Depth 999)
        Set-Content $script:SecureLocalAssetsPath -Value (ConvertTo-Json -InputObject $SecureLocalAssets -Depth 999)
    }
    else {
        throw "Variable '$Name' not found for account 'AuthoringToolkit'"
    }
}

<#
        .SYNOPSIS
        Get a certificate asset from Azure Automation.
        Part of the Azure Automation Authoring Toolkit to help author runbooks locally.
#>

function Get-AutomationCertificate {
    [CmdletBinding(HelpUri='http://aka.ms/azureautomationauthoringtoolkit')]
    [OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])]
    
    param(
        [Parameter(Mandatory=$true)]
        [string] $Name
    )

    Write-Verbose "AzureAutomationAuthoringToolkit: Looking for local certificate asset with name '$Name'"

    $AssetValue = Get-AzureAutomationAuthoringToolkitLocalAsset -Type Certificate -Name $Name

    Write-Output $AssetValue
}