Reputation: 85
I wrote a simple script which should get a list of computers from an OU and report on disk size and free space amount on each reachable machine.
$computers = Get-ADComputer -SearchBase "ou=Test,ou=test,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
Write-host $item
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
Write-host $disk_size
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
Write-Host $disk_freespace
}
$output | Export-Csv "networkpath\free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
The output looks good on screen but the exported CSV file is empty. I guess that I could change foreach
to ForEach-Object
but I don't think that it's good practice to put every script to one liner and in case I need to add more actions then it will be painful.
Here is my updated code based on the comments:
$computers = Get-ADComputer -SearchBase "ou=t,ou=test,ou=Production,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
$output = [PSCustomObject]@{
Name = $item
Size = $disk_size
FreeSpace = $disk_freespace
}
}
$output | Export-Csv "networkpath\$((Get-Date).ToString("yyyyMMdd_HHmmss"))_free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
But now I'm getting only one record in my CSV file. What am I doing wrong?
Upvotes: 0
Views: 958
Reputation: 7479
here's what i was trying to convey about building a PSCustomObject
& sending that out to a collection. [grin]
what it does ...
#region/#endregion
block and use Get-Content
OR use Get-ADComputer
. splat
for readability instead of a long line of parameters & values. $NoResponse
PSCO
$Results
collection the code ...
#region >>> fake reading in a text file
# in real life, use Get-Content
$ComputerList = @'
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a CSV file
$TimeStamp = (Get-Date).ToString('yyyy-MM-dd')
$ReportPath = $env:TEMP
$ReportFile = 'Dmitry Dorofeev_-_DiskSpaceReport_-_{0}.csv' -f $TimeStamp
$FullReportFile = Join-Path -Path $ReportPath -ChildPath $ReportFile
$NoResponse = '__n/a__'
$Results = foreach ($CL_Item in $ComputerList)
{
if (Test-Connection -ComputerName $CL_Item -Count 2 -Quiet)
{
$GWO_Params = @{
Class = 'Win32_LogicalDisk'
ComputerName = $CL_Item
Filter = "DeviceID = 'c:'"
}
$DiskInfo = Get-WmiObject @GWO_Params
$Size_GB = [math]::Round($DiskInfo.Size / 1gb, 2)
$FreeSpace_GB = [math]::Round($DiskInfo.FreeSpace / 1gb, 2)
}
else
{
$Size_GB = $FreeSpace_GB = $NoResponse
}
[PSCustomObject]@{
ComputerName = $CL_Item
Size_GB = $Size_GB
FreeSpace_GB = $FreeSpace_GB
}
}
# display on screen
$Results
# send to a csv file
$Results |
Export-Csv -LiteralPath $FullReportFile -NoTypeInformation
on screen output ...
ComputerName Size_GB FreeSpace_GB
------------ ------- ------------
BetterNotBeThere __n/a__ __n/a__
LocalHost 931.41 730.79
10.0.0.1 __n/a__ __n/a__
127.0.0.1 931.41 730.79
csv file [C:\Temp\Dmitry Dorofeev_-_DiskSpaceReport_-_2020-05-22.csv
] content ...
"ComputerName","Size_GB","FreeSpace_GB"
"BetterNotBeThere","__n/a__","__n/a__"
"LocalHost","931.41","730.79"
"10.0.0.1","__n/a__","__n/a__"
"127.0.0.1","931.41","730.79"
Upvotes: 2