Home > Net >  How to handle an expression when input is taken from a csv
How to handle an expression when input is taken from a csv

Time:02-22

Beginner with powershell and looking for some help with the @{Label='ID'; expression={??} section please:-

$ID = @()
Import-Csv C:\computers2.csv | ForEach-Object {$ID  = $_.ID}
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ID | 
Select-Object -Property DeviceID, @{Label='ID'; expression= 
{$ID}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{Label='Free (Gb)'; expression= 
{($_.FreeSpace/1GB).ToString('F2')}},
@{label='Percent'; expression={[Math]::Round(($_.freespace / 
$_.size) * 100, 2)}} | Format-Table

The output I get is this:-

output

What I'm trying to achieve would be this:-

desired output

CodePudding user response:

Managed to work it out after following some guides and getting the correct variables

$CSV = Import-Csv C:\computers.csv
$SPACE = ForEach ($ID in $CSV) {
Get-WmiObject -Class Win32_LogicalDisk -ComputerName ($ID.ID) | 
Select-Object -Property DeviceID,
@{Label='ID'; expression={$_.SystemName}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{Label='Free (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='PercentFree'; expression={[Math]::Round(($_.freespace / $_.size) * 
100, 2)}}
} | FormatTable
  • Related