Skip to content

Commit 79ab770

Browse files
altered cim instance filter
1 parent c615956 commit 79ab770

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Get-DriveSize.ps1

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ function Get-DriveSize
5656
{
5757
try
5858
{
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]
6460
}
6561
catch
6662
{
@@ -73,16 +69,17 @@ function Get-DriveSize
7369
{
7470
try
7571
{
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]
7773
}
7874
catch
7975
{
8076
Write-Error -Message 'Failed to get Operating System'
8177
$_
78+
break
8279
}
8380
}
8481

85-
If($version -gt 6)
82+
If($version -ge 6)
8683
{
8784
$FreeGb = @{
8885
Name = 'FreeGB'
@@ -107,11 +104,11 @@ function Get-DriveSize
107104
try
108105
{
109106
$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{
111108
$_.DriveLetter -ne $null
112109
}
113110
$Return = $disks |
114-
Select-Object -Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
111+
Select-Object -Property PSComputerName, Name, Label , $TotalGB , $FreeGb , $FreePercent |
115112
Sort-Object -Property Name |
116113
Format-Table -AutoSize |
117114
Out-String
@@ -131,10 +128,10 @@ function Get-DriveSize
131128
try
132129
{
133130
$disks = (Get-CimInstance -ClassName win32_volume -ComputerName $Server -ErrorAction Stop).Where{
134-
$_.DriveLetter -ne $null
131+
$_.DriveType -eq 3 -and $_.DriveLetter -ne $null
135132
}
136133
$Return = $disks |
137-
Select-Object -Property Name, Label , $TotalGB , $FreeGb , $FreePercent |
134+
Select-Object -Property PSComputerName, Name, Label , $TotalGB , $FreeGb , $FreePercent |
138135
Sort-Object -Property Name |
139136
Format-Table -AutoSize |
140137
Out-String
@@ -161,13 +158,19 @@ function Get-DriveSize
161158
expression = {
162159
[math]::round(((($_.FreeSpace / 1Gb)/($_.Size / 1Gb)) * 100),0)
163160
}
161+
}
162+
$FreeGb = @{
163+
Name = 'FreeGB'
164+
Expression = {
165+
'{0:N2}' -f ($_.Freespace/1GB)
166+
}
164167
}
168+
165169
if($Credential)
166170
{
167171
Try
168172
{
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|
171174
Where-Object -FilterScript {
172175
$_.drivetype -eq 3
173176
}|
@@ -176,8 +179,6 @@ function Get-DriveSize
176179
Format-Table -AutoSize |
177180
Out-String
178181
return $Return
179-
}
180-
Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -Credential $Credential -ErrorAction Stop
181182
}
182183
Catch
183184
{
@@ -208,6 +209,5 @@ function Get-DriveSize
208209
}
209210
}
210211
}
211-
Write-Output -InputObject "Disk Space on $Server at $Date"
212212
return $Return
213213
}

0 commit comments

Comments
 (0)