Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
January 20, 2022

The Value of Objects

I expect I will write several articles about PowerShell and its relationship with objects. I know that this is the biggest hurdle for PowerShell beginners to overcome. But once they grasp that PowerShell is about working with objects in the pipeline, they recognize the value and begin finding it easier to write PowerShell code and use it interactively at a console prompt.

Don’t forget that I am approaching PowerShell from the role of an IT pro. Many of these people are coming to PowerShell with other automation and scripting skills. It might be old-fashioned batch files. It might be extensive experience with VBScript. That was my PowerShell journey. Like many of you, my initial PowerShell experiences were colored by my past. I kept trying to make PowerShell write text as I did with VBScript.

Here’s an old VBScript example.

Dim objSet, wshell
On Error Resume Next

Set wshell=CreateObject("Wscript.Shell")

If wscript.arguments.count=0 Then
 strSrv=InputBox("What server do you want to check?  You must have admin rights on it.  Do NOT use \\ " & _
 "before the servername.","Disk Check","SERVERNAME")
 If strSrv="" Then
   wshell.popup "Nothing entered or you cancelled",4,"Disk Check",0+48
   wscript.quit
 End If
Else
 strSrv=Trim(wscript.arguments(0))
End If

strQuery = "Select * from win32_logicaldisk where drivetype=3"
Set objSet=GetObject("winmgmts:\\" & strSrv).ExecQuery(strQuery)
if err.number<>0 then
 wshell.popup "Oops! Error connecting to " & UCase(strSrv) & vbCrlf & "make sure you are using valid " & _
 "credentials." & vbCrlf & "Error: " & err.number & " - " & err.description,5,"Disk Check Error",0+48
 wscript.quit
end if

For Each item In objSet
   PerFree=FormatPercent(item.FreeSpace/item.Size,2)
   o=o & item.DeviceID & "\" & VBTAB
   o=o & FormatNumber(item.Size/1048576,0) & Vbtab & FormatNumber(item.FreeSpace/1048576,0) & Vbtab & PerFree & Vbcrlf
next
WScript.Echo "Drive" & Vbtab & "Size (MB) Free (MB) %Free" & VbCrLf & o
set objSet=Nothing
set wshell=Nothing

wscript.quit
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.