I was tasked with getting the versions and counts of PowerShell in our infrastructure. I decided to use a VBScript instead of trying to take into account which PoSh versions would run which commands, execution policies, etc.
The script I wrote was very simple and worked in that it returned the version information that I wanted. However, after getting the info and exiting the script (the CMD window closed), on some computers (not all, and none in test) there was an orphaned cscript
process that took up all of the remaining CPU time. Running the script a second time left a second cscript
process that now split the CPU time, but still pegged it at 100%, ad infinitum. This was most visible on SQL servers because of the high CPU usage they have to begin with.
I've fixed the issue at hand but what I'm looking for here is a possible reason that this happened so I don't do it again in the future.
Here's my script:
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
filTxt = "C:\psver.txt"
'This command calls powershell, runs the $PSVersion command and outputs to
'a text file
cmd = "powershell.exe -Command " & _
"[string]$PSVersionTable.PSVersion.Major +'.'+ " & _
"[string]$PSVersionTable.PSVersion.Minor > " & filTxt
'Delete the file C:\psver.txt if it exists
If objFSO.FileExists(filTxt) Then
objFSO.DeleteFile(filTxt)
End if
'This runs the command we set above
Set psv = objShell.Exec(cmd)
'Sleep for 6 seconds to allow the file to finish writing
WScript.Sleep 6000
'This section reads the new file
Set objFile = objFSO.GetFile("C:\psver.txt")
Set objFile = objFSO.OpenTextFile(filTxt, 1, False, -1)
Do Until objFile.AtEndOfStream
strVersion = objFile.ReadLine
Loop
objFile.Close
The runaway processes happened on Windows 2008 R2 through 2016. PowerShell versions 2.0 - 5.1.
I'm thinking that because I didn't exit PowerShell nicely it left the cscript
session open, but why did it consume so much resources? For that matter, is there a way to exit PowerShell nicely using command lines, or should I have created a .ps1 file and bundled it with my .vbs file?