Created
January 18, 2019 19:49
-
-
Save LawrenceHwang/acaf3e655c4ade7be4cb633f984f6a10 to your computer and use it in GitHub Desktop.
ConvertTo-SSMDocument.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-SSMDocument | |
{ | |
<# | |
.Synopsis | |
Convert a script into SSM Command document | |
.DESCRIPTION | |
This cmdlet convert a script into SSM Command document. | |
.EXAMPLE | |
PS:\ > ConvertTo-SSMDocument -Expression 'Get-Service' | |
{ | |
"schemaVersion": "2.2", | |
"description": "SSM Command Document", | |
"parameters": { | |
"Message": { | |
"type": "String", | |
"description": "SSM parameter description", | |
"default": "hello hello" | |
} | |
}, | |
"mainSteps": [ | |
{ | |
"action": "aws:runPowerShellScript", | |
"name": "example", | |
"inputs": { | |
"runCommand": [ | |
"Get-Service" | |
] | |
} | |
} | |
] | |
} | |
.EXAMPLE | |
$scriptpath = 'C:\temp\demo.ps1' | |
ConvertTo-SSMDocument -Path $scriptpath | |
{ | |
"schemaVersion": "2.2", | |
"description": "SSM Command Document", | |
"parameters": { | |
"Message": { | |
"type": "String", | |
"description": "SSM parameter description", | |
"default": "hello hello" | |
} | |
}, | |
"mainSteps": [ | |
{ | |
"action": "aws:runPowerShellScript", | |
"name": "example", | |
"inputs": { | |
"runCommand": [ | |
"if (-Not (Get-Module -name 'ActiveDirectory' -ErrorAction stop -ListAvailable)){", | |
" Add-WindowsFeature -Name \"RSAT-AD-PowerShell\" -IncludeAllSubFeature -verbose", | |
"}" | |
] | |
} | |
} | |
] | |
} | |
.NOTES | |
Author: Lawrence Hwang | |
#> | |
[cmdletbinding(DefaultParameterSetName = 'Expression')] | |
param( | |
# Convert an existing script to SSM document. | |
[parameter(Mandatory = $true, | |
ParameterSetName = 'File')] | |
[string]$Path, | |
# Convert a one liner to SSM document. | |
[parameter(Mandatory = $true, | |
ParameterSetName = 'Expression')] | |
[string]$Expression, | |
#SSM document description | |
[parameter(Mandatory = $false, | |
ParameterSetName = 'File')] | |
[parameter(Mandatory = $false, | |
ParameterSetName = 'Expression')] | |
[string]$SSMDocDescription = 'SSM Command Document', | |
# SSM Document type. Currently only support Command type. | |
[parameter(Mandatory = $false, | |
ParameterSetName = 'File')] | |
[parameter(Mandatory = $false, | |
ParameterSetName = 'Expression')] | |
[string]$DocumentType = 'Command' | |
) | |
switch ($PSCmdlet.ParameterSetName) | |
{ | |
'File' {$RawContent = Get-Content $Path} | |
'Expression' {$RawContent = $Expression} | |
} | |
$RawContent = $RawContent.replace('\', '\\').replace('"', '\"') | |
$powershellscript = [string]::Join("`",`n`"", $RawContent) | |
$powershellscript = '"' + $powershellscript + '"' | |
$ssmdoc = @" | |
{ | |
"schemaVersion": "2.2", | |
"description": "$SSMDocDescription", | |
"parameters": { | |
"Message": { | |
"type": "String", | |
"description": "SSM parameter description", | |
"default": "hello hello" | |
} | |
}, | |
"mainSteps": [ | |
{ | |
"action": "aws:runPowerShellScript", | |
"name": "example", | |
"inputs": { | |
"runCommand": [ | |
$powershellscript | |
] | |
} | |
} | |
] | |
} | |
"@ | |
$ssmdoc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment