Error code, 80041021 occurs when you execute a VBScript connecting to WMI
(Windows Management Instrumentation). A wild guess there is a missing
\\ in the winmgmts statement.
The Symptoms You Get
The script does not manipulate the WMI object as you had hoped, instead you get a Windows Script Host error message like this picture:
The Cause of Code 80041021
Case 1
Your VBScript contains a misspelled object name. Code 80041021 is an
unusual Error in that a number is returned rather than 'Syntax Error' or
other message. In my example the fault was in a WMI script, the cause
was a syntax error with Set objWMI = GetObject("winmgmts:"
Case 2
In another example, I had the reverse problem, I put: \\ machinename
in the string variable, whereas I should have used plain: machinename.
Case 3
I removed the impersonationLevel=impersonate statement from the VBScript.
Wrong Set objWMIService = GetObject("winmgmts:" _ &
strComputer & "\root\cimv2")
The Solutions
I solved this 80041021 error message by paying close attention to the line number. In
addition, I compared the scripts to other WMI examples. In one case it turned out to
be a missing \\ in Set objWMI = GetObject("winmgmts:\\"..... In another case it was a missing & "{impersonationLevel=impersonate}!\\" _
»
Example of Code Error 80041021 Script
Mistake in Line 38
Set objWMI = GetObject("winmgmts:"
Correction:
Set objWMI = GetObject("winmgmts:\\"
' EventID12.vbs
' Version 1.9
' Guy Thomas 1st August 2010
Option Explicit
Dim objFso, objFolder, objWMI, objEvent ' Objects
Dim strFile, strComputer, strFolder, strFileName, strPath ' Strings
Dim intEvent, intNumberID, intRecordNum, colLoggedEvents
' --------------------------------------------
' Set your variables
intNumberID = 12 ' Event ID Number
intEvent = 1
intRecordNum = 1
' ----------------------------------------
' Section to create folder to hold file.
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolder) Then
Set objFolder = objFSO.GetFolder(strFolder)
Else
Set objFolder = objFSO.CreateFolder(strFolder)
Wscript.Echo "Folder created " & strFolder
End If
Set strFile = objFso.CreateTextFile(strPath, True)
'--------------------------------------------
' Next section creates the file to store Events
' Then creates WMI connector to the Logs
Set objWMI = GetObject("winmgmts:" _ & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System'" )
Wscript.Echo " Press OK and Wait 30 seconds (ish)"
' -----------------------------------------
' Next section loops through ID properties
intEvent = 1
For Each objEvent in colLoggedEvents
If objEvent.EventCode = intNumberID Then
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created the
WMI Monitor so that you can examine these gems of
performance information for free. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance and availability statistics for any device
on your network.