powershell 编写Systems Manager库存记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 编写Systems Manager库存记录相关的知识,希望对你有一定的参考价值。

$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

以上是关于powershell 编写Systems Manager库存记录的主要内容,如果未能解决你的问题,请参考以下文章