Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Script all server disk space

This question is not answered
Hello everybody,

Looking for
a script to any server checks the size of disks, and the results database (SQL Server) record.

Forumozo
All Replies
  • <#
    Get-AllServerDisks.ps1

    Assumptions:   You run it as a user who has admin rights on all servers

    #>
    Clear-Host
    # Define Global Timeouts for WMI Query
    $WMI = [wmi]""
    $WMI.psbase.options.timeout = "0:0:2"
    $searcher = [WMISearcher]""
    $searcher.options.timeout = "0:0:2"

    # Define Temporary Working Directory
    $WorkingDir = "D:\Reports\AllServerDisks"

    # Define Credential XML Location
    $PathToCredXML = "$env:USERPROFILE\Scripts\PowerShell\Credentials.xml"

    # Build an LDAP Filter that only returns systems with the name "server" identified
    $LdapFilter = "(&(&(sAMAccountType=805306369)(objectCategory=computer)(objectClass=computer)(operatingSystem=*Server*)))"
    $Domain = New-Object System.DirectoryServices.DirectoryEntry
    $ADSearch = New-Object System.DirectoryServices.DirectorySearcher
    $ADSearch.SearchRoot = $Domain
    $ADSearch.PageSize = 1000
    $ADSearch.Filter = $LdapFilter
    $ADSearch.SearchScope = "Subtree"
    $ADSearch.PropertiesToLoad.Add("Name") | Out-Null

    Write-Host "Performing AD Search for Servers"
    $ADSearchResults = $ADSearch.FindAll()

    # Build Empty Collection
    Write-Host "Building Server Object"
    $ServerList = @()
    ForEach ( $ADSearchResult in $ADSearchResults )
    {
     # Add Just the server name to the new object
     $ServerList += ( $ADSearchResult.Properties ).name
    }

    $ServerList = $ServerList | Sort-Object

    Write-Host "Cleaning up variables"
    # Delete the Pre-existing Active Directory Objects - no longer needed
    Remove-Variable Domain, ADSearch, ADSearchResults, LdapFilter

    Write-Host "Checking for Credential XML File"
    if ( -not ( Test-Path $PathToCredXML ) )
    {
     Write-Host "Building a new Admin Credential XML File"
     $Credential = Get-Credential
     $Password = $credential.Password | ConvertFrom-SecureString
     $Username = $Credential.UserName
     $Store = "" | Select Username, Password
     $Store.Username = $Username
     $Store.Password = $Password
     $Store | Export-Clixml $PathToCredXML
    }
    else
    {
     Write-Host "Loading Admin Credentials from XML"
     $CredentialToLoad = Import-Clixml $PathToCredXML
     $Credential = New-Object System.Management.Automation.PsCredential($CredentialToLoad.Username,($CredentialToLoad.Password | ConvertTo-SecureString))
    }

    # Build an empty collection for the Disk Information
    $DiskInfo = @()
    Write-Host "Getting Disk Information For Servers"
    $i = 1
    $ServerCount = $ServerList.Count
    ForEach ( $Server in $ServerList )
    {
     # Every 200 Iterations, run the garbage collector
     if ( ($i % 200) -eq 1 )
     {
      Write-Host "Running Garbage Collection" -ForegroundColor Yellow
      [System.GC]::Collect()
     }
     
     Write-Host ( "[" + $i + "/" + $ServerCount + "] Server: " + $Server )
     if ( Test-Connection $Server -Count 1 -ErrorAction SilentlyContinue)
     {
      $Error.Clear()
      $ServerDiskCollection = New-Object PSObject
      Write-Host ( "`tGetting Disk Information")
      $LocalDiskQuery = "SELECT SystemName, DriveLetter, Name, Label, DriveType, Capacity, FreeSpace FROM Win32_Volume WHERE DriveType=3"
      $ServerDiskCollection = Get-WmiObject -ComputerName $Server -Query $LocalDiskQuery -Credential $Credential -ErrorAction SilentlyContinue
      if ( -not $Error )
      {
       $ServerDiskCollection = $ServerDiskCollection | Select-Object SystemName, DriveLetter, Name, Label, DriveType, Capacity, FreeSpace
       $ServerDiskCollection | Add-Member -MemberType ScriptProperty -Name "UsedSpace" -Value { $this.Capacity - $this.FreeSpace }
       $ServerDiskCollection | Add-Member -MemberType ScriptProperty -Name "MountPoint" -Value { ( $this.Name -like "?:\*\" ) }
       Write-Host ( "Writing " + $WorkingDir + "\" + $Server + ".csv ..." ) -NoNewline
       $ServerDiskCollection | Export-Csv -Path ( $WorkingDir + "\" + $Server + ".csv" ) -NoTypeInformation
       Write-Host (" [Completed]")
      }
      Else
      {
       Write-Host "`tInsufficient Permissions to Enumerate Disks" -ForegroundColor Red
      }
     }
     Else
     {
      Write-Host ( "`tServer Offline" ) -ForegroundColor Red
     }
     $i++
    }

    # Clear Out Unneeded Variables
    Remove-Variable ServerDiskCollection, WMICollection, Server, ServerList

    $CompleteReport = @()
    $CSVFiles = Get-ChildItem -Path ( $WorkingDir + "\*.csv" )
    $CSVFiles | ForEach-Object -Process { $CompleteReport += Import-Csv $_.FullName }
    $CompleteReport | Export-Csv -Path ( $WorkingDir + "\CompleteReport.csv" ) -NoTypeInformation
    $CSVFiles | ForEach-Object { Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue }


  • http://wp.shlidor.com/2012/02/26/get-disk-space-report-and-send-once-a-weekly-to-email/
  • Dear College,

    thank you very much in the script that is used within a few days .. We do not work. :-)
  • where is the complete report. I looked for it in c and could not find it?

  •  ERRROR


    Writing c:\Reports\AllServerDisks\PRD-SOLON.csv ...Export-Csv : Could not find a part of the path 'C:\Reports\AllServerDisks\PRD-
    SOLON.csv'.
    At C:\Reports\AllServersDisks\Frihddsize.ps1:97 char:38
    +    $ServerDiskCollection | Export-Csv <<<<  -Path ( $WorkingDir + "\" + $Server + ".csv" ) -NoTypeInformation
        + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
        + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
  • I got this script working but I notice quite a few things. The script is very robust in any case. I was thing if the separate excel sheets could have been combined in one. I also noticed that the size for the disk are in bytes. Do you think you can fix some of these. Especiaally the bytes to GB. Thanks a lot.