@@ -56,11 +56,7 @@ function Get-DriveSize
56
56
{
57
57
try
58
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
59
+ $version = [int ](Get-WMIObject - ClassName Win32_OperatingSystem - Credential $Credential ).Version.split(' .' )[0 ]
64
60
}
65
61
catch
66
62
{
@@ -73,16 +69,17 @@ function Get-DriveSize
73
69
{
74
70
try
75
71
{
76
- $version = [int ](Get-CimInstance - ClassName Win32_OperatingSystem - ComputerName $Server - ErrorAction Stop).Version.split(' .' )[0 ]
72
+ $version = [int ](Get-WMIObject - ClassName Win32_OperatingSystem - ComputerName $Server - ErrorAction Stop).Version.split(' .' )[0 ]
77
73
}
78
74
catch
79
75
{
80
76
Write-Error - Message ' Failed to get Operating System'
81
77
$_
78
+ break
82
79
}
83
80
}
84
81
85
- If ($version -gt 6 )
82
+ If ($version -ge 6 )
86
83
{
87
84
$FreeGb = @ {
88
85
Name = ' FreeGB'
@@ -107,11 +104,11 @@ function Get-DriveSize
107
104
try
108
105
{
109
106
$ScriptBlock = {
110
- $disks = (Get-CimInstance - ClassName win32_volume - ComputerName $Server - ErrorAction Stop).Where {
107
+ $disks = (Get-CimInstance - ClassName win32_volume - ComputerName $Server - ErrorAction Stop ).Where {
111
108
$_.DriveLetter -ne $null
112
109
}
113
110
$Return = $disks |
114
- Select-Object - Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
111
+ Select-Object - Property PSComputerName , Name, Label , $TotalGB , $FreeGb , $FreePercent |
115
112
Sort-Object - Property Name |
116
113
Format-Table - AutoSize |
117
114
Out-String
@@ -131,10 +128,10 @@ function Get-DriveSize
131
128
try
132
129
{
133
130
$disks = (Get-CimInstance - ClassName win32_volume - ComputerName $Server - ErrorAction Stop).Where {
134
- $_.DriveLetter -ne $null
131
+ $_.DriveType -eq 3 -and $_ . DriveLetter -ne $null
135
132
}
136
133
$Return = $disks |
137
- Select-Object - Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
134
+ Select-Object - Property PSComputerName , Name, Label , $TotalGB , $FreeGb , $FreePercent |
138
135
Sort-Object - Property Name |
139
136
Format-Table - AutoSize |
140
137
Out-String
@@ -161,13 +158,19 @@ function Get-DriveSize
161
158
expression = {
162
159
[math ]::round(((($_.FreeSpace / 1 Gb )/ ($_.Size / 1 Gb )) * 100 ), 0 )
163
160
}
161
+ }
162
+ $FreeGb = @ {
163
+ Name = ' FreeGB'
164
+ Expression = {
165
+ ' {0:N2}' -f ($_.Freespace / 1 GB )
166
+ }
164
167
}
168
+
165
169
if ($Credential )
166
170
{
167
171
Try
168
172
{
169
- $ScriptBlock = {
170
- $Return = Get-WmiObject - Class win32_logicaldisk - ComputerName $Server - ErrorAction Stop|
173
+ $Return = Get-WmiObject - Class win32_logicaldisk - ComputerName $Server - Credential $Credential - ErrorAction Stop|
171
174
Where-Object - FilterScript {
172
175
$_.drivetype -eq 3
173
176
}|
@@ -176,8 +179,6 @@ function Get-DriveSize
176
179
Format-Table - AutoSize |
177
180
Out-String
178
181
return $Return
179
- }
180
- Invoke-Command - ComputerName $Server - ScriptBlock $ScriptBlock - Credential $Credential - ErrorAction Stop
181
182
}
182
183
Catch
183
184
{
@@ -208,6 +209,5 @@ function Get-DriveSize
208
209
}
209
210
}
210
211
}
211
- Write-Output - InputObject " Disk Space on $Server at $Date "
212
212
return $Return
213
213
}
0 commit comments