Forum Discussion

Joe Schmitz's avatar
Joe Schmitz
Copper Contributor
Feb 01, 2018

Data usage report

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

  • Let'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 } }
  • Let'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 } }
    • Joe Schmitz's avatar
      Joe Schmitz
      Copper Contributor

      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

Resources