I’ve got a cleanup script that I’m intending to use to clean hundreds of virtual servers through the use of active directory. In the past I would create a simple .txt file that would display the following:
-Amount of disk space that existed before the script was run
-How much space after it was run
-Total space cleared
In the past this worked great, however it was intended to be used on a single server at a time rather than hundreds. Since I’m wanting to change towards running this script on hundreds of scripts at once, I’d like to change this to a spreadsheet which would display the same data as well as show the name of each server that the script was ran against.
How could I manage to create this type of output in a spreadsheet format and display that? Here’s my current code (the .txt method):
$logFilePath = "C:\logfile.txt"
$disks = Get-WMIObject -Computer $server -Class Win32_LogicalDisk -Filter "DeviceID like '%C%'"
$beforeFreeSpace = $disks.FreeSpace
$beforeFreeSpaceMB = [math]::truncate($beforeFreeSpace / 1MB)
$preCleanupMessage = "Space available before cleanup ran (MB): "
$preCleanupMessage += $beforeFreeSpaceMB
$preCleanupMessage | out-file -filePath $logFilePath -Append
$afterFreeSpace = $disks.FreeSpace
$afterFreeSpaceMB = [math]::truncate($afterFreeSpace / 1MB)
$freedSpace = "Freed up space after cleanup (MB): "
$freedSpace += $afterFreeSpaceMB - $beforeFreeSpaceMB
$freedSpace | out-file -filePath $logFilePath -Append
$message = "Free space remaining after cleanup (in MB): "
$message += [math]::truncate($afterFreeSpace / 1MB)
$message | out-file -filePath $logFilePath -Append
Thanks in advance!
I’d do something like this:
Also, keep in mind that the default gwmi cmdlet can time out and you may have to work around that. Good luck. Also, run this manually and if you have to abort the script at any point, simply run the last line to get the output up to that point.