Using Arrays to create a custom PS object

I am attempting to create a custom PS object to then export to an Excel document. I think I’m overcomplicating the task. Here is the current script that just prints the information I need:

$Server = Get-WmiObject -ComputerName Fileserver01 win32_logicaldisk -Filter DriveType=3
$date = date

Write-Host $date
Write-Host "Free Space on FileServer01 Drives:"
Foreach($drive in $Server){
        Write-Host $drive.Name ($drive.Freespace / 1GB)
}

My initial idea was to feed each drive into a different array, then for each object in the array feed it into the PS object:

$Server = Get-WmiObject -ComputerName Fileserver01 win32_logicaldisk -Filter DriveType=3
$DriveSpace = @()
$date = date

Write-Host $date
Write-Host "Free Space on FileServer01 Drives:"
Foreach($drive in $Server){
        $drive.Freespace += $DriveSpace
}

The above code just doesn’t add anything to the arrays, and I’m not sure what I’m doing wrong. Overall I have pretty much no experience exporting data, so if there’s an easier way of doing this, I will take any recommendations. My idea for the PS object would look like this:

$TimeStamp = [PSCustomObject]@{
        Time = $date
        C Drive = $drive[0].Freespace
        D Drive = $drive[1].Freespace
}

$DriveSpace += $drive.Freespace

Wow yep that’ll do it thank you. Finished Script:

$Server = Get-WmiObject -ComputerName Fileserver01 win32_logicaldisk -Filter DriveType=3
Foreach($Drive in $DriveSpace){
        $RAWFreeSpace = ($Drive.FreeSpace / 1GB)
        $RoundedFreeSpace = [Math]::($RAWFreeSpace, 2)
        $DriveStorage += $RoundedFreeSpace
}

$TimeStamp = [PSCustomObject]@{
        Time = date
        "C Drive" = $DriveStorage[0]
        "D Drive" = $DriveStorage[1]
}

$TimeStamp

I was panicking because I thought I overcomplicated it. In other forums I overlooked the fact that I was making a syntax error.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.