One of the major errors I see PowerShell newcomers performing is copying script structures and syntax of VBScript into PowerShell. Let me give you an example
This piece of VBScript is borrowed from the Windows 2000 scripting guide
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
Wscript.Echo "OS Name: " & objOperatingSystem.Name
Wscript.Echo "Version: " & objOperatingSystem.Version
Wscript.Echo "Service Pack: " & _
objOperatingSystem.ServicePackMajorVersion _
& "." & objOperatingSystem.ServicePackMinorVersion
Wscript.Echo "OS Manufacturer: " & objOperatingSystem.Manufacturer
Wscript.Echo "Windows Directory: " & _
objOperatingSystem.WindowsDirectory
Wscript.Echo "Locale: " & objOperatingSystem.Locale
Wscript.Echo "Available Physical Memory: " & _
objOperatingSystem.FreePhysicalMemory
Wscript.Echo "Total Virtual Memory: " & _
objOperatingSystem.TotalVirtualMemorySize
Wscript.Echo "Available Virtual Memory: " & _
objOperatingSystem.FreeVirtualMemory
Wscript.Echo "OS Name: " & objOperatingSystem.SizeStoredInPagingFiles
Next
It uses the Win32_OperatingSystem class to display information about the computers OS. As you can see the bulk of the script is concerned with formatting. Note the error in the script regarding OS Name
What often happens is that this will be translated directly, line by line, into PowerShell – which gives us something like this
$strComputer = "." $colSettings = Get-WmiObject -Query "SELECT * FROM Win32_OperatingSystem" -ComputerName $strComputer foreach ($objOperatingSystem in $colSettings){ Write-Host "OS Name: " $objOperatingSystem.Name Write-Host "Version: " $objOperatingSystem.Version Write-Host "Service Pack: " $objOperatingSystem.ServicePackMajorVersion "." $objOperatingSystem.ServicePackMinorVersion Write-Host "OS Manufacturer: " $objOperatingSystem.Manufacturer Write-Host "Windows Directory: " $objOperatingSystem.WindowsDirectory Write-Host "Locale: " $objOperatingSystem.Locale Write-Host "Available Physical Memory: " $objOperatingSystem.FreePhysicalMemory Write-Host "Total Virtual Memory: " $objOperatingSystem.TotalVirtualMemorySize Write-Host "Available Virtual Memory: " $objOperatingSystem.FreeVirtualMemory }
This is less code but still seems like a lot of work
lets put it into PowerShell as if we had written it from scratch
$strComputer = "." Get-WmiObject -Class Win32_OperatingSystem -ComputerName $strComputer | select Name, Version, ServicePackMajorVersion, ServicePackMinorVersion, Manufacturer, WindowsDirectory, Locale, FreePhysicalMemory, TotalVirtualMemorySize, FreeVirtualMemory
Now that is much easier. Quicker to code and easier to understand.
By all means use VBscripts as references for using WMI classes (I do) but re-write into PowerShell its much, much easier.
This isn’t a contradiction of my previous post as I advocated leaving legacy VBScript alone until you needed to modify it