Feb 01 2018 12:46 PM
New to Powershell.
Looking to see if there's a way to generate a report in Powershell that'll output the amount of data stored on each server's hard drives..
Thanks.
Joe
Feb 03 2018 12:22 AM - edited Feb 03 2018 12:49 AM
SolutionLet's say you have the list of Servers in C:\Servers.txt
ForEach ($Server in Get-Content "C:\Servers.txt") { ForEach ($Disk in get-WmiObject win32_logicaldisk -Computername $Server) { $output = @() $Computer = $Disk.PSComputerName $Drive = $Disk.DeviceID
$Size = "$($Disk.Size/1GB) GB" $FreeSpace = "$($Disk.FreeSpace/1GB) GB"
$Used = "$(($Disk.Size - $Disk.FreeSpace)/1GB) GB" $Properties = @{ Computer = $Computer Drive = $Drive
Size = $Size FreeSpace = $FreeSpace
Used = $Used } $PSobject = New-Object -TypeName psobject -Property $Properties $output += $PSobject $output | Export-Csv -Append -Path C:\ServerDevices.CSV" -NoTypeInformation } }
Feb 05 2018 07:48 AM
Hi Ortiz,
Thank you for your response. I ran into some issues with syntax errors when I plugged the code into ISE so I cleaned it up a little bit, as shown below.
ForEach ($Server in Get-Content "C:\Servers.txt") { ForEach ($Disk in get-WmiObject win32_logicaldisk -Computername $Server) { $output = @() $Computer = $Disk.PSComputerName $Drive = $Disk.DeviceID $Size = "$($Disk.Size/1GB) GB" $FreeSpace = "$($Disk.FreeSpace/1GB) GB" $Used = "$(($Disk.Size - $Disk.FreeSpace)/1GB) GB"} $Properties = @{ Computer = $Computer Drive = $Drive Size = $Size FreeSpace = $FreeSpace Used = $Used } $PSobject = New-Object -TypeName psobject -Property $Properties $output += $PSobject} $output | Export-Csv -Append -Path "C:\Users\jschmitz\Downloads\ServerDevices.CSV" -NoTypeInformation
After running the code I ran into a problem:
get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At line:3 char:21 + ForEach ($Disk in get-WmiObject win32_logicaldisk -Computername $Server) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
I'm only have to get one server to show up on the report, which is the second to last one on the list. Any ideas on what could be the cause?
Thank you!
Joe
Feb 05 2018 08:03 AM
that's connection errors, please check the following link:
Feb 05 2018 08:26 AM
Appreciate the help, but due to time I've found another solution. The script has been documented in my system.
Joe
Feb 03 2018 12:22 AM - edited Feb 03 2018 12:49 AM
SolutionLet's say you have the list of Servers in C:\Servers.txt
ForEach ($Server in Get-Content "C:\Servers.txt") { ForEach ($Disk in get-WmiObject win32_logicaldisk -Computername $Server) { $output = @() $Computer = $Disk.PSComputerName $Drive = $Disk.DeviceID
$Size = "$($Disk.Size/1GB) GB" $FreeSpace = "$($Disk.FreeSpace/1GB) GB"
$Used = "$(($Disk.Size - $Disk.FreeSpace)/1GB) GB" $Properties = @{ Computer = $Computer Drive = $Drive
Size = $Size FreeSpace = $FreeSpace
Used = $Used } $PSobject = New-Object -TypeName psobject -Property $Properties $output += $PSobject $output | Export-Csv -Append -Path C:\ServerDevices.CSV" -NoTypeInformation } }