Tuesday, 14 August 2012

Drive Size - PowerShell Script

The below script is used to find the disk size for a set of servers,
 
1:  # Script to Find List of drives and Size of Drive in given set of server  
2:  # Created by - Vinoth N Manoharan  
3:  # Version 1.1  
4:  # Date - 15/09/2011  
5:  # Script Help :-  
6:  #---------------  
7:  # Parameter 1 :- "-s" to run powershell for Single server  
8:  # "-f" to give filename with list of servers  
9:  # Parameter 2 :- Server name(if -s is the parameter 1) or Filename with fully defined path(if -f is the parameter 1)  
10:  #  
11:  # Example1:- FindDiskSize.ps1 -s <servername>  
12:  # Example2:- FindDiskSize.ps1 -f <filename with fully define path like c:\test.txt>  
13:  Clear-Host  
14:  $List =@()  
15:  if($args.Length -ne 2)  
16:  {  
17:  Write-Host "Incorrect Paramenter Count use either -s or -f to specify the servername/Serverlist"  
18:  }  
19:  elseif(($args[0] -eq "-s") -or ($args[0] -eq "-S"))  
20:  {  
21:  $computer = $args[1]  
22:  $drives = @()  
23:  if($computer -ne $null)  
24:  {  
25:  $srt = "Server Name :- " + $computer  
26:  $srt  
27:  Echo "---------------------------"  
28:  # Properties for PowerShell logical disk object:  
29:  $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq3}  
30:  $drives | Add-Member -MemberType ScriptProperty -Name SizeGB -Value{[math]::Round(($this.Size/1GB),2)} -PassThru |  
31:  Add-Member -MemberType ScriptProperty -Name FreeSpaceGB -Value{[math]::Round(($this.FreeSpace/1GB),2)} -PassThru |  
32:  ft Name, SizeGB, FreeSpaceGB -AutoSize  
33:  }  
34:  }  
35:  elseif(($args[0] -eq "-f") -or ($args[0] -eq "-F"))  
36:  {  
37:  $filename = $args[1]  
38:  $computers = get-content $filename  
39:  foreach ($computer in $computers)  
40:  {  
41:  $drives = @()  
42:  if($computer -ne $null)  
43:  {  
44:  $srt = "Server Name :- " + $computer  
45:  $srt  
46:  Echo "---------------------------"  
47:  # Properties for PowerShell logical disk object:  
48:  $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq3}  
49:  $drives | Add-Member -MemberType ScriptProperty -Name SizeGB -Value{[math]::Round(($this.Size/1GB),2)} -PassThru |  
50:  Add-Member -MemberType ScriptProperty -Name FreeSpaceGB -Value{[math]::Round(($this.FreeSpace/1GB),2)} -PassThru |  
51:  ft Name, SizeGB, FreeSpaceGB -AutoSize  
52:  }  
53:  }  
54:  }  
55:  else  
56:  {  
57:  Write-Host "Incorrect Paramenter, use either -s or -f to specify the servername/Serverlist"  
58:  }  

Copyright © 2012 Vinoth N Manoharan.The information provided in this post is provided "as is" with no implied warranties or guarantees.

No comments:

Post a Comment