0

I am writing a script where Block Devices attached to a EC2 instance are matched and then a corresponding tag is set.

I have following PSOObject and visualization in the console

 New-Object PSObject -Property @{
    Disk          = $Disk;
    Partitions    = $Partitions;
    DriveLetter   = If ($DriveLetter -eq $null) { "N/A" } Else { $DriveLetter };
    EbsVolumeId   = If ($EbsVolumeID -eq $null) { "N/A" } Else { $EbsVolumeID };
    Device        = If ($BlockDeviceName -eq $null) { "N/A" } Else { $BlockDeviceName };
    VirtualDevice = If ($VirtualDevice -eq $null) { "N/A" } Else { $VirtualDevice };
    VolumeName    = If ($VolumeName -eq $null) { "N/A" } Else { $VolumeName };
  }
} | Sort-Object Disk | Format-Table -AutoSize -Property Disk, Partitions, DriveLetter, EbsVolumeId, Device, VirtualDevice, VolumeName

Now I have following simple New-EC2tag command tagging the instance.

New-EC2Tag -Resource $InstanceId -Tags @{ Key = $DriveLetter; Value = $BlockDeviceName}

Basically it just writes the C: Drive and I believe I have to iterate through the PSObject to basically also set the corresponding drives.

I would bge grateful for any help.

ghraz0r
  • 31
  • 5
  • You can access the properties of that PSObject. If that object is stored as variable `$obj`, for example, you can use `$obj.DriveLetter` and `$obj.Device` to access the values. – AdminOfThings Apr 10 '19 at 14:40
  • but how do i select the specific enumeration over the variable...like for each n in $ob.Driveletter do this.... – ghraz0r Apr 10 '19 at 15:04
  • If $obj.DriveLetter is an array, you can do Foreach ($letter in $obj.DriveLetter) { $letter } – AdminOfThings Apr 10 '19 at 15:43
  • so basically it would work like for each ($letter in $obj.Drivelietter) (New-EC2Tag -Resource $InstanceId -Tags @{ Key = $letter; Value = $BlockDeviceName}? Could I also do $obj | Get-Member -MemberType Property,NoteProperty | ForEach-Object { New-EC2Tag -Resource $InstanceId -Tags @{ Key = $_.Driveletter; Value = $BlockDeviceName} }? Thanks for any tip! – ghraz0r Apr 10 '19 at 19:51
  • Your first question is the way I would do it. It is simpler and more efficient. – AdminOfThings Apr 10 '19 at 19:59

0 Answers0