Script that retrieves disk information from a list of computer and output to csv

This script reads a list of servers or computer from a plaintext file and read the disk information using WMI. This information will be written to a comma-separated file which automatically appends the current date & time so this script can be run as a scheduled task.

 
 
 
 
 
(0)
1,877 times
Add to favorites
Storage
3/23/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • where do I append...
    2 Posts | Last post March 16, 2013
    • I read through the lines of codes without figuring out what line the .txt file goes into. I am asking if you can point out exactly where in your script to attach the path to the .txt file moving forward in the script execution for the CSV output?
    • If you would like it to append you can change line 48 to this:
      $logfile = $logpath+"Available_Diskspace.csv"
      
      And change line 52 to this:
      if (!(Test-Path $logfile)) {$exporttofile | out-file $logfile -append -encoding utf8}
      
      That will allow you to append to the file, because the filename will not be autogenerated based on the current date/time.
  • What I did...
    1 Posts | Last post March 14, 2013
    • #function GetComputerDiskspace {
      	param
      	(
      		[string]$listpath,
      		[string]$logpath
      	)
      
      	# Get the list of machines for this script if none is set exits script
      	if (!($listpath)) {"No list of computers specified, exiting";return}
      	$serverlist = @(get-content c:\Ntregis_servhdd.txt $listpath)
      
      	# Checks for $logpath if does not exist defaults to .\
      	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 
      				}
      		}
      	}
      #}