A N

December 11, 2015

Powershell – Get Disc Report

Filed under: Tips — donrsh @ 3:33 pm
<#	
	 Created on:   	2015-09-09 
	 Created by:   	Arash Nabi
	 Email: 	    arash@nabi.nu 
     
     Get disk report. Export the result to csv file for further tuning in Excel.
	
#>
try
{
	$VerbosePreference = 'Continue'
	$diskObj = @()
	$Servers = Get-Content -Path "C:\Servers.txt"
	
	foreach ($server in $servers)
	{		
		# CIM stuff
		$objOption = New-CimSessionOption -Protocol Dcom -ErrorAction SilentlyContinue
		$objSession = New-CimSession -ComputerName $server -SessionOption $objOption -ErrorAction SilentlyContinue
		if ($objSession)
		{
			$params = @{
			'ServerName' = $server
			}
			$allDisks = Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_Volume -ErrorAction SilentlyContinue | Where-Object { $_.Capacity -gt 1 } |
			select driveletter, capacity, freespace
			foreach ($disk in $alldisks)
			{
				$DisksizeGB = [math]::round($disk.capacity/1GB)
				$diskLetter = $disk.Driveletter.Replace(':', '')
				$DiskUsed = [math]::round(($disk.capacity - $disk.freespace) /1GB)
				$DiskFree = [math]::round(($disk.freespace /1GB))
				$percentFree = "{0:P1}" -f ($DiskFree/$DisksizeGB)
				$params.Add($("$diskLetter Drive"), $("($percentFree Free) Total $disksizeGB GB, Used $DiskUsed GB, Free $DiskFree GB"))
			}
			# Create objects
			$objresult = New-Object psobject -Property $params
			# Add object to result
			$diskObj += $objresult
			Write-Verbose -Message $server
		}
		else
		{
			Write-Warning "No CimSession connection to $server. Aborting"
		}
	}
	$diskObj | select Servername, 'C Drive', 'D Drive', 'F Drive', 'E Drive', 'G Drive' | Export-Csv -Path c:\export.csv -NoTypeInformation
}
catch [System.Net.WebException] #, [System.Exception]
{
	$errorMessage = $_.Exception
	Write-Warning -Message $errorMessage
}
finally
{
	Write-Verbose -Message 'End of Script'
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: