Skip to content

Instantly share code, notes, and snippets.

@austoonz
Created January 25, 2019 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austoonz/0e8a4ff8c9b26c81d6a2b4ddc9b5f63f to your computer and use it in GitHub Desktop.
Save austoonz/0e8a4ff8c9b26c81d6a2b4ddc9b5f63f to your computer and use it in GitHub Desktop.
Sample code for writing an AWS Systems Manager Inventory record.
$powerShellModules = Get-Module -ListAvailable
$date = Get-Date -UFormat '+%Y-%m-%dT%H:%M:%SZ'
$inventoryItems = [System.Collections.Generic.List[Amazon.SimpleSystemsManagement.Model.InventoryItem]]::new()
$contentList = [System.Collections.Generic.List[System.Collections.Generic.Dictionary[System.String,System.String]]]::new()
foreach ($moduleName in ($powerShellModules | Select-Object -Property Name -Unique).Name)
{
$module = $powerShellModules.Where({$_.Name -eq $moduleName}) | Sort-Object -Property Version -Descending | Select-Object -First 1
$contentDictionary = [System.Collections.Generic.Dictionary[System.String,System.String]]::new()
$contentDictionary.Add('ModuleName', $moduleName)
$contentDictionary.Add('ModuleVersion', $module.Version)
$contentDictionary.Add('Path', $module.Path)
$null = $contentList.Add($contentDictionary)
}
$inventoryItem = [Amazon.SimpleSystemsManagement.Model.InventoryItem]::new()
$inventoryItem.CaptureTime = $date
$inventoryItem.Content = $contentList
$inventoryItem.SchemaVersion = '1.0'
$inventoryItem.TypeName = 'Custom:PowerShellModule'
$null = $inventoryItems.Add($inventoryItem)
Write-SSMInventory -InstanceId $managedInstanceId -Item $inventoryItems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment