Hi,
I created a basic custom inventory for Disk usage by creating a Systems manager association. I am running a powershell script to get the disk information but unable to see the inventory in the console. Script runs fine and was written to the intended location in the target machine however the inventory doesn't show up in the console.
Here's my script.
function Get-EbsVolumes {[CmdletBinding()]
$Map = @{"0" = '/dev/sda1'}; for($x = 1; $x -le 26; $x++)
{$Map.add($x.ToString(), [String]::Format("xvd{0}",[char](97 + $x)))};
for($x = 78; $x -le 102; $x++)
{$Map.add($x.ToString(), [String]::Format("xvdc{0}",[char](19 + $x)))}
return Get-WmiObject -Class Win32_DiskDrive | % { $Drive = $; $SN = $Drive.SerialNumber -replace '^(vol)([^ ]+)(?: *.+)?$', '$1-$2';
Get-WmiObject -Class Win32_DiskDriveToDiskPartition | Where-Object {$.Antecedent -eq $Drive.Path.Path} | %{
$D2P = $; $Partition = Get-WmiObject -Class Win32_DiskPartition | Where-Object {$.Path.Path -eq $D2P.Dependent};
$Disk = Get-WmiObject -Class Win32_LogicalDisk`ToPartition | Where-Object {$.Antecedent -in $D2P.Dependent} | %{
$L2P = $; Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$.Path.Path -in $L2P.Dependent} | %{
$L2V = $; Get-WmiObject -Class Win32_Volume | Where-Object {$.SerialNumber -eq ([Convert]::ToInt64($L2V.VolumeSerialNumber, 16))}; } }
New-Object PSObject -Property @{ Device = $Map[$Drive.SCSITargetId.ToString()]; Disk = [Int]::Parse($Partition.Name.Split(",")[0].Replace("Disk #","")); Boot = $Partition.BootPartition; Partition = [Int]::Parse($Partition.Name.Split(",")[1].Replace(" Partition #","")); DriveLetter = If($Disk -eq $NULL) {"NA"} else {$Disk.DriveLetter}; VolumeDeviceID = If($Disk -eq $NULL) {"NA"} else {$Disk.DeviceID}; DriveType = If($Disk -eq $NULL) {"NA"} else {$Disk.DriveType}; IsSystemVolume = If($Disk -eq $NULL) {"NA"} else {$Disk.SystemVolume}; IsBootVolume = If($Disk -eq $NULL) {"NA"} else {$Disk.BootVolume.ToString()}; DriveLabel = If($Disk -eq $NULL) {"NA"} else {$Disk.Label}; BlockSize = $Disk.BlockSize; Capacity = ([math]::Round($Disk.Capacity /1GB,0)).ToString(); DiskType = $Partition.Type; FreeSpace = [math]::Round($Disk.FreeSpace /1GB,0); SerialNumber = $SN } } } | Where-Object {
$.DriveType -eq 3 -and $_.IsSystemVolume -eq $false} }
$data = Get-EbsVolumes | Select-Object @{n="Device";e={$_.Device}},@{n="Disk";e={$_.Disk.ToString()}},@{n="Boot";e={$_.Boot.ToString()}},@{n="DriveLetter";e={$_.DriveLetter.ToString()}},@{n="VolumeDeviceID";e={$_.VolumeDeviceID}},@{n="Partition";e={$_.Partition}},@{n="DriveType";e={$_.DriveType.ToString()}},@{n="IsSystemVolume";e={$_.IsSystemVolume.ToString()}},@{n="IsBootVolume";e={$_.IsBootVolume.ToString()}},@{n="DriveLabel";e={$_.DriveLabel.ToString()}},@{n="BlockSize";e={$_.BlockSize.ToString()}},@{n="Capacity";e={$_.Capacity.ToString()}},@{n="DiskType";e={$_.DiskType.ToString()}},@{n="FreeSpace";e={$_.FreeSpace.ToString()}},@{n="SerialNumber";e={$_.SerialNumber.ToString()}} | ConvertTo-Json -Compress
$content = "{"SchemaVersion
" : "1.0
", "TypeName
": "Custom:EbsVolumesInventory
", "Content
": $data}"
$instanceId = Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/instance-id
$filepath = "C:\ProgramData\Amazon\SSM\InstanceData" + $instanceId + "\inventory\custom\EbsVolumesInventory.json"
if (-NOT (Test-Path $filepath)) { New-Item $filepath -ItemType file}
Set-Content -Path $filepath -Value $content