Jose Barreto blogged the details HERE.
His original script is fine and works.
I refactored his script this way. I prefer this style, it feels more readable. Using features of PowerShell v3 makes it easier to write scripts more concisely. Jose used PowerShell v2 techniques that still work but are more verbose and bulky.
What do you think?
Refactored Version
$CimSession = "." ForEach($item in Get-SmbShare -CimSession $CimSession | Where Volume) { $volume = Get-Volume -CimSession $CimSession -Id $item.Volume [PSCustomObject] @{ Share = $item.Name Path = $item.Path Size = $volume.Size Remaining = $volume.SizeRemaining } }
Original
Get-SmbShare -CimSession FST2-FS2 | ? Volume -ne $null | % { $R = "" | Select Share, Path, Size, Free $R.Share=$_.Name; $R.Path=$_.Path Get-Volume -CimSession FST2-FS2 -Id $_.Volume | % { $R.Size=$_.Size; $R.Free=$_.SizeRemaining; $R | FL } }





{ 4 comments… read them below or add one }
I like this but must offer a short script I have been using for awhile. I wish I could say who I learned it from.
function Get-Disk { $fso = new-Object -com Scripting.FileSystemObject; $fso.getdrive($args) }
Usage:
Get-Disk c:
Get-Disk \\server\share
will return an object like:
Path : C:
DriveLetter : C
ShareName :
DriveType : 2
RootFolder : System.__ComObject
AvailableSpace : 60581060608
FreeSpace : 60581060608
TotalSize : 255505461248
VolumeName :
FileSystem : NTFS
SerialNumber : 144432260
IsReady : True
Thanks for the PowerShell script. Great way to skin the cat.
Doug
You have a bug: $Target should be $CimSession
Also, you can improve this by removing duplicate volumes:
$CimSession = ‘.’
Foreach ($item in Get-SmbShare -CimSession $CimSession |
Where Volume |
Sort -Unique Volume
) {
$volume = Get-Volume -CimSession $CimSession -Id $item.Volume
[PSCustomObject] @{
Share = $item.Name
Path = $item.Path
Size = $volume.Size
Remaining = $volume.SizeRemaining
}
}
Thanks Chris for catching the bug and the optimization. I corrected the code.
Doug