SMAAuthoringToolkitInner.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 |
$script:ConfigurationFileName = "SMAAuthoringToolkitConfig.json" $script:ConfigurationPath = "$env:USERPROFILE\$script:ConfigurationFileName" $script:LocalAssetsPath = "$PSScriptRoot\LocalAssets.json" $script:SecureLocalAssetsPath = "$PSScriptRoot\SecureLocalAssets.json" $script:StartProfileSnippetForPowerShellToLoadSMAISEAddOn = "# Start SMAISEAddOn snippet" $script:EndProfileSnippetForPowerShellToLoadSMAISEAddOn = "# End SMAISEAddOn snippet" $script:IseAddonPath = "\SMAISEAddon\Microsoft.SystemCenter.ServiceManagementAutomation.ISEAddOn.dll" $script:PowerShellToLoadSMAIseAddOnGeneric = @" `n # Start SMAISEAddOn snippet Import-Module SMAAuthoringToolkit # End SMAISEAddOn 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-SMAAuthoringToolkitConfiguration 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 "SMAAuthoringToolkit: Value is not encrypted. Returning raw value without decrypting" return $Value } elseif($Configuration.EncryptionCertificateThumbprint -eq "none") { Write-Verbose "SMAAuthoringToolkit: No encryption certificate specified. Returning raw value without decrypting" return $Value } else { $Thumbprint = $Configuration.EncryptionCertificateThumbprint Write-Verbose "SMAAuthoringToolkit: 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 "SMAAuthoringToolkit: 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-SMAAuthoringToolkitConfiguration if($Configuration.EncryptionCertificateThumbprint -eq "none") { Write-Verbose "SMAAuthoringToolkit: No encryption certificate specified. Not encrypting value" return $Value } else { $Thumbprint = $Configuration.EncryptionCertificateThumbprint Write-Verbose "SMAAuthoringToolkit: 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 SMAAuthoringToolkit to user's profile directory #> function Copy-ConfigFile { Write-Verbose "SMAAuthoringToolkit: 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 "SMAAuthoringToolkit: '$script:ConfigurationPath' already present. Not copying file." } } <# .SYNOPSIS Sets up the Service Management Automation ISE add-on for use in the PowerShell ISE. #> function Install-SMAIseAddOn { $IseProfilePath = Join-Path (Split-Path $Profile) $script:IseProfileFileName if(!(Test-Path $IseProfilePath)) { Write-Verbose "SMAAuthoringToolkit: '$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:StartProfileSnippetForPowerShellToLoadSMAISEAddOn) # add content to PS ISE profile to load ISe add on on start up Write-Verbose "SMAAuthoringToolkit: 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 $PowerShellToLoadSMAIseAddOnWithPath = $script:PowerShellToLoadSMAIseAddOnGeneric -f $IseAddOnModuleFolderPath, $script:IseAddonPath Add-Content $IseProfilePath $PowerShellToLoadSMAISEAddOnWithPath if($IsRunningInISE) { # load the ISE add-on into the PS ISE session already open Write-Verbose "SMAAuthoringToolkit: Loading ISE add-on into this PowerShell ISE session" Invoke-Expression $PowerShellToLoadSMAIseAddOnWithPath } } else { Write-Verbose "SMAAuthoringToolkit: Content already present. Not adding any content." } } <# .SYNOPSIS Removes the Service Management Automation ISE add-on from the PowerShell ISE. #> function Uninstall-SMAIseAddOn { $IseProfilePath = Join-Path (Split-Path $Profile) $script:IseProfileFileName $ProfileContent = Get-Content $IseProfilePath -Raw $StartProfileSnippetIndex = $ProfileContent.IndexOf($script:StartProfileSnippetForPowerShellToLoadSMAISEAddOn) $EndProfileSnippetIndex = $ProfileContent.IndexOf($script:EndProfileSnippetForPowerShellToLoadSMAISEAddOn) if($StartProfileSnippetIndex -gt -1 -and $EndProfileSnippetIndex -gt -1) { Write-Verbose "SMAAuthoringToolkit: 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:EndProfileSnippetForPowerShellToLoadSMAISEAddOn.Length) $NewProfileContent | Set-Content $IseProfilePath } } <# .SYNOPSIS Get a local certificate based on its thumbprint, as part of the Service Management Automation Authoring Toolkit. Not meant to be called directly. #> function Get-SMAAuthoringToolkitLocalCertificate { param( [Parameter(Mandatory=$True)] [string] $Name, [Parameter(Mandatory=$True)] [string] $Thumbprint ) Write-Verbose "SMAAuthoringToolkit: Looking for local certificate with thumbprint '$Thumbprint'" try { $Certificate = Get-Item ("Cert:\CurrentUser\My\" + $Thumbprint) -ErrorAction Stop Write-Output $Certificate } catch { Write-Error "SMAAuthoringToolkit: 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 Service Management Automation Authoring Toolkit. Not meant to be called directly. #> function Get-SMAAuthoringToolkitLocalAsset { param( [Parameter(Mandatory=$True)] [ValidateSet('Variable', 'Certificate', 'PSCredential', 'Connection')] [string] $Type, [Parameter(Mandatory=$True)] [string]$Name ) $Configuration = Get-SMAAuthoringToolkitConfiguration 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 = "SMAAuthoringToolkit: SMAAuthoringToolkit local assets defined in '$script:LocalAssetsPath' is incorrect. Make sure the file exists, and it contains valid JSON." $SecureLocalAssetsError = "SMAAuthoringToolkit: SMAAuthoringToolkit secure local assets defined in '$script:SecureLocalAssetsPath' is incorrect. Make sure the file exists, and it contains valid JSON." Write-Verbose "SMAAuthoringToolkit: 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 "SMAAuthoringToolkit: Found local value for $Type asset '$Name.'" $AssetWasInSecureLocalAssets = $False } else { $Asset = _findObjectByName -ObjectArray $SecureLocalAssets.$Type -Name $Name if($Asset) { Write-Verbose "SMAAuthoringToolkit: Found secure local value for $Type asset '$Name.'" $AssetWasInSecureLocalAssets = $True } } if($Asset) { if($Type -eq "Certificate") { $AssetValue = Get-SMAAuthoringToolkitLocalCertificate -Name $Name -Thumbprint $Asset.Thumbprint } elseif($Type -eq "Variable") { if($Asset.Value -eq $Null) { Write-Warning "SMAAuthoringToolkit: 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 ("SMAAuthoringToolkit: 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 "SMAAuthoringToolkit: 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 "SMAAuthoringToolkit: Local value for $Type asset '$Name' not found." Write-Warning "SMAAuthoringToolkit: Warning - Local value for $Type asset '$Name' not found." } } <# .SYNOPSIS Get the configuration for the Service Management Automation Authoring Toolkit. Not meant to be called directly. #> function Get-SMAAuthoringToolkitConfiguration { $ConfigurationError = "SMAAuthoringToolkit: SMAAuthoringToolkit configuration defined in '$script:ConfigurationPath' is incorrect. Make sure the file exists, contains valid JSON, and contains 'LocalAssetsPath', 'SecureLocalAssetsPath', and 'EncryptionCertificateThumbprint' settings." Write-Verbose "SMAAuthoringToolkit: Grabbing SMAAuthoringToolkit 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 Service Management Automation. Part of the Service Management Automation Authoring Toolkit to help author runbooks locally. #> function Get-AutomationVariable { [OutputType([Object])] param( [Parameter(Mandatory=$true)] [string] $Name ) Write-Verbose "SMAAuthoringToolkit: Looking for local variable asset with name '$Name'" $AssetValue = Get-SMAAuthoringToolkitLocalAsset -Type Variable -Name $Name Write-Output $AssetValue } <# .SYNOPSIS Get a connection asset from Service Management Automation. Part of the Service Management Automation Authoring Toolkit to help author runbooks locally. #> function Get-AutomationConnection { [OutputType([Hashtable])] param( [Parameter(Mandatory=$true)] [string] $Name ) Write-Verbose "SMAAuthoringToolkit: Looking for local connection asset with name '$Name'" $AssetValue = Get-SMAAuthoringToolkitLocalAsset -Type Connection -Name $Name Write-Output $AssetValue } <# .SYNOPSIS Set the value of a variable asset in Service Management Automation. Part of the Service Management Automation Authoring Toolkit to help author runbooks locally. #> function Set-AutomationVariable { param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter(Mandatory=$true)] [object] $Value ) Write-Verbose "SMAAuthoringToolkit: Looking for local variable asset with name '$Name'" $LocalAssetValue = Get-SMAAuthoringToolkitLocalAsset -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 "SMAAuthoringToolkit: 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 Service Management Automation. Part of the Service Management Automation Authoring Toolkit to help author runbooks locally. #> function Get-AutomationCertificate { [OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])] param( [Parameter(Mandatory=$true)] [string] $Name ) Write-Verbose "SMAAuthoringToolkit: Looking for local certificate asset with name '$Name'" $AssetValue = Get-SMAAuthoringToolkitLocalAsset -Type Certificate -Name $Name Write-Output $AssetValue } |