Skip to content

Commit a79f182

Browse files
Added Get-DriveSize
1 parent 4c12096 commit a79f182

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

Get-DriveSize.ps1

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<#
2+
.Synopsis
3+
Function to return space details for drives and mount points on local and remote servers
4+
.DESCRIPTION
5+
Returns Drive or volume name, Label, Size in GB, Free Space in Gb and Percentage of Free Space
6+
for Drives and Mount points for local and remote servers
7+
.EXAMPLE
8+
Get-DriveSize
9+
10+
This will return the details of the drives on the local machine
11+
.EXAMPLE
12+
Get-DriveSize -Server SQLServer1
13+
14+
This will return the details of the drives on the Server SQLServer1
15+
.EXAMPLE
16+
Get-DriveSize -Server SQLServer1 -Credential $Credential
17+
18+
This will return the details of the drives on the Server SQLServer1 using the credential stored in the $Credential Variable
19+
.EXAMPLE
20+
Get-DriveSize -Server SQLServer1 -PromptForCredential
21+
22+
This will return the details of the drives on the Server SQLServer1 and prompt for a credential
23+
.NOTES
24+
AUTHOR : Rob Sewell http://sqldbawithabeard.com
25+
Initial Release 06/03/2014
26+
Aliased for Show-DriveSizes 25/10/2014
27+
Updated with remoting capabilities 10/05/2015
28+
#>
29+
#requires -Version 3 -Modules CimCmdlets
30+
function Get-DriveSize
31+
{
32+
[CmdletBinding()]
33+
[Alias('Show-DrivesSizes')] # For Rob who still uses the old function name!
34+
param (
35+
# Server Name - Defaults to $ENV:COMPUTERNAME
36+
[Parameter(Mandatory = $false,
37+
ValueFromPipeline = $true,
38+
ValueFromPipelineByPropertyName = $true,
39+
Position = 0)]
40+
[string]$Server = $Env:COMPUTERNAME,
41+
# Credential for connecting to server
42+
[Parameter(Mandatory = $false,
43+
ValueFromPipeline = $true,
44+
ValueFromPipelineByPropertyName = $true)]
45+
[System.Management.Automation.PSCredential]$Credential,
46+
# Prompts for credential
47+
[switch]$PromptForCredential
48+
)
49+
if($PromptForCredential)
50+
{
51+
$Credential = Get-Credential -Message "Credential with Permissions to $Server"
52+
}
53+
$Date = Get-Date
54+
## Get the operating system version to decide win32_volume or win32_logicaldisk
55+
if($Credential)
56+
{
57+
try
58+
{
59+
$ScriptBlock = {
60+
$version = [int](Get-CimInstance -ClassName Win32_OperatingSystem).Version.split('.')[0]
61+
return $version
62+
}
63+
Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -Credential $Credential -ErrorAction Stop
64+
}
65+
catch
66+
{
67+
Write-Error -Message 'Failed to get Operating System'
68+
$_
69+
break
70+
}
71+
}
72+
else
73+
{
74+
try
75+
{
76+
$version = [int](Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $Server -ErrorAction Stop).Version.split('.')[0]
77+
}
78+
catch
79+
{
80+
Write-Error -Message 'Failed to get Operating System'
81+
$_
82+
}
83+
}
84+
85+
If($version -gt 6)
86+
{
87+
$FreeGb = @{
88+
Name = 'FreeGB'
89+
Expression = {
90+
'{0:N2}' -f ($_.Freespace/1GB)
91+
}
92+
}
93+
$TotalGB = @{
94+
Name = 'SizeGB'
95+
expression = {
96+
[math]::round(($_.Capacity/1Gb),2)
97+
}
98+
}
99+
$FreePercent = @{
100+
Name = 'Free %'
101+
expression = {
102+
[math]::round(((($_.FreeSpace /1Gb)/($_.Capacity /1Gb)) * 100),0)
103+
}
104+
}
105+
if($Credential)
106+
{
107+
try
108+
{
109+
$ScriptBlock = {
110+
$disks = (Get-CimInstance -ClassName win32_volume -ComputerName $Server -ErrorAction Stop).Where{
111+
$_.DriveLetter -ne $null
112+
}
113+
$Return = $disks |
114+
Select-Object -Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
115+
Sort-Object -Property Name |
116+
Format-Table -AutoSize |
117+
Out-String
118+
return $Return
119+
}
120+
Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -Credential $Credential -ErrorAction Stop
121+
}
122+
catch
123+
{
124+
Write-Error -Message 'Failed to get Disks'
125+
$_
126+
break
127+
}
128+
}
129+
else
130+
{
131+
try
132+
{
133+
$disks = (Get-CimInstance -ClassName win32_volume -ComputerName $Server -ErrorAction Stop).Where{
134+
$_.DriveLetter -ne $null
135+
}
136+
$Return = $disks |
137+
Select-Object -Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
138+
Sort-Object -Property Name |
139+
Format-Table -AutoSize |
140+
Out-String
141+
return $Return
142+
}
143+
catch
144+
{
145+
Write-Error -Message 'Failed to get Disks'
146+
$_
147+
break
148+
}
149+
}
150+
}
151+
else
152+
{
153+
$TotalGB = @{
154+
Name = 'Size GB'
155+
expression = {
156+
[math]::round(($_.Size/ 1Gb),2)
157+
}
158+
}
159+
$FreePerc = @{
160+
Name = 'Free %'
161+
expression = {
162+
[math]::round(((($_.FreeSpace / 1Gb)/($_.Size / 1Gb)) * 100),0)
163+
}
164+
}
165+
if($Credential)
166+
{
167+
Try
168+
{
169+
$ScriptBlock = {
170+
$Return = Get-WmiObject -Class win32_logicaldisk -ComputerName $Server -ErrorAction Stop|
171+
Where-Object -FilterScript {
172+
$_.drivetype -eq 3
173+
}|
174+
Select-Object -Property Name, Label, $TotalGB, $FreeGb, $FreePerc |
175+
Sort-Object -Property Name |
176+
Format-Table -AutoSize |
177+
Out-String
178+
return $Return
179+
}
180+
Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -Credential $Credential -ErrorAction Stop
181+
}
182+
Catch
183+
{
184+
Write-Error -Message 'Failed to get Disks'
185+
$_
186+
break
187+
}
188+
}
189+
else
190+
{
191+
Try
192+
{
193+
$Return = Get-WmiObject -Class win32_logicaldisk -ComputerName $Server -ErrorAction Stop|
194+
Where-Object -FilterScript {
195+
$_.drivetype -eq 3
196+
}|
197+
Select-Object -Property Name, Label, $TotalGB, $FreeGb, $FreePerc |
198+
Sort-Object -Property Name |
199+
Format-Table -AutoSize |
200+
Out-String
201+
return $Return
202+
}
203+
Catch
204+
{
205+
Write-Error -Message 'Failed to get Disks'
206+
$_
207+
break
208+
}
209+
}
210+
}
211+
Write-Output -InputObject "Disk Space on $Server at $Date"
212+
return $Return
213+
}

0 commit comments

Comments
 (0)