none
script to retrieve disk space from servers. RRS feed

  • Question

  • #function GetComputerDiskspace {
     param
     (
      [string]$listpath,
      [string]$logpath
     )

     # Get the list of machines for this script if none is set exits script
     if (!($listpath)) {".\logs\Ntregis_servhdd.txt"}
     $serverlist = @(get-content, $listpath)
     # Checks for $logpath if does not exist defaults to .\$listpath c:\logs\Ntregis_servhdd.txt
     if (!($logpath)) {$logpath = ".\"}

     # Gets date and reformats to be used in log filename, enabling automagic log creation
     $tempdate = (get-date).tostring("dd-MM-yyyy_HHmm.ss")
     $logfile = $logpath+"Available_Diskspace_"+$tempdate+".csv"

     # Encoding for output is set to utf8, otherwise excel will not open the .csv files correctly
     $exporttofile = "Servername,Drive Letter,Total Space(GB),Free Space(GB),Free Percentage"
     $exporttofile | out-file $logfile -append -encoding utf8

     # Main loop
     $count = $serverlist.count
     for ($j=0;$j -lt $count;$j++) {
      $tempvar = @()
      # Prepare variables and display progress of script
      write-output ($serverlist[$j],"*** Server",($j+1),"out of",$serverlist.count -join " ")

      # Loop only executed when ping is successful
      if (test-connection -computername $serverlist[$j] -count 1 -quiet) {
       [array]$tempvar = Get-WmiObject win32_logicaldisk -filter "drivetype = '3'" -computername $serverlist[$j] | Select systemname,deviceid,size,freespace
        for ($k=0;$k -lt $tempvar.count;$k++) {
         $tempoutput = $tempvar[$k]
         
         # Setup line to be written to file
         $freespace = "{0:N1}" -f ($tempoutput.freespace/$tempoutput.size*100)
         $exporttofile = $tempoutput.systemname+","+$tempoutput.deviceid+","+("{0:N1}" -f ($tempoutput.size/1GB))+","+("{0:N1}" -f ($tempoutput.freespace/1GB))+","+$freespace

         # Write to log, UTF8 encoding for .csv
         $exporttofile | out-file $logfile -append -encoding utf8
        }
      }
     }
    #}   Why do I have this error it seems to me that the syntax is fine. Let me know if you have the answer to the problem. One more thing it will save to csv file but no server information relating to the variables queried for. Thank.

    PS C:\logs> C:\logs\getServerDskspc.ps1
    .\logs\Ntregis_servhdd.txt
    Missing argument in parameter list.
    At C:\logs\getServerDskspc.ps1:41 char:30
    +     $serverlist = @(get-content, <<<<  $listpath)
        + CategoryInfo          : InvalidOperation: (,:String) [], RuntimeException
        + FullyQualifiedErrorId : MissingArgument

    Friday, March 15, 2013 4:29 PM