WMIC Odd Behavior - hang

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

WMIC Odd Behavior - hang

Post by MigrationUser »

11 Jan 2012 14:50
RG

I am seeing some odd WMIC behavior on 2003 Server only (tried on SP1 and SP2). If I have the following code embedded in a rather large bat file it will hang forever. Yet I can put this code in another bat file and it executes just fine. Although it will fail if I call that small bat file from my large one. If it so happens that WMIC is not installed I see the prompt to that effect, WMIC gets installed, the prompt goes away and it hangs. I do not have this problem on Vista or Windows 7.
Any ideas? Thanks!

Code: Select all

@ECHO OFF
SET WebUser=UniverseWeb
ECHO.Please wait while WMIC sets password for %WebUser% to never expire...
WMIC /TRACE:ON USERACCOUNT WHERE "Name='%WebUser%'" SET PasswordExpires=FALSE
Windows Shell Scripting and InstallShield

----------------------------

#2 12 Jan 2012 12:08
bluesxman


Sorry not had that problem (though I don't mess with WMIC very often).

What it you replace it with some other WMIC command, does it still hang?

Strange that it fails in the context of your large script but not the smaller one when standing alone.

What happens if you use start /w "" cmd /c "wmicScript.cmd" or start /w "" wmic [...] to launch it in another window?

If it's specific to setting password expiry, you could always try to workaround the issue with a little VBScript.

cmd | *sh | ruby | chef

----------------------------

#3 12 Jan 2012 21:39
RG


bluesman,
Yes, it hangs on other WMIC commands too (2003 Server only).
Good suggestion! This works:

Code: Select all

SET WebUser=UniverseWeb
ECHO.Please wait while WMIC sets password for %WebUser% to never expire...
START "" /W CMD /C WMIC /RECORD:"SomeFile.txt" USERACCOUNT WHERE "Name='%WebUser%'" SET PasswordExpires=FALSE
Notes: I also made these changes, but they were not causing any problem:
1. Removed /TRACE:ON because that is verbose and distracting.
2. Added /RECORD:"SomeFile.txt" to log the result.
Thanks for getting me going :)

Windows Shell Scripting and InstallShield

----------------------------

#4 13 Jan 2012 11:31
bluesxman


Splendid, glad to be of service :)

cmd | *sh | ruby | chef

----------------------------

#5 04 Jan 2013 19:49
lawson23


I would like to know if anyone actually has figured out "why" this happens. I use this during deployment. I have used it successfully for like 5 years deploying XP SP3 desktops. I just moved from one deployment solution to another and now I'm having this problem from those newly deployed systems.

If I open a command window and run a wmic command it works if I put it in a batch it hangs unless I use something like Start /W.

Any ideas?

----------------------------

#6 04 Jan 2013 20:16
Simon Sheppard


If WMI is taking a few seconds to initialise and return the results, then possibly the batch script is completing and terminating the session before WMIC has completed.

To test this you could try putting a PAUSE command immediately after the WMIC command.

----------------------------

#7 04 Jan 2013 21:20
lawson23


wmic hangs indefinitely. There is no few seconds or anything. It hangs at that process I can watch it using echo statements and batch logging. I first run:
WMIC EXIT
to get wmic installed before running my commands. Then my commands execute. It hangs right at this process. So if I add start /w before the command then when it goes to run the other commands it hangs there.

If you search: wmic hang batch
you will find other results but no one actually knowing why this is happening. Again the exact same os same batch files same deployment process but one fails the other has worked for 5 years.

----------------------------

#8 04 Jan 2013 21:25
Ed Dyreen
lawson23 wrote:

I would like to know if anyone actually has figured out "why" this happens. I use this during deployment. I have used it successfully for like 5 years deploying XP SP3 desktops. I just moved from one deployment solution to another and now I'm having this problem from those newly deployed systems.

If I open a command window and run a wmic command it works if I put it in a batch it hangs unless I use something like Start /W.

Any ideas?
Sorry, haven't figured out "why" yet, you are not the only one though, Heads Up, I know some more issues with WMIC...:)

-WMIC fails to return control to caller batch and hangs;
Start an additional shell that kills WMIC after some minutes, 'start /wait' WMIC, after processing kill the additional shell.
-An additional Carriage Return is added at the end of each line;
Strip with a 'for' Or redirect to file and use 'type "file"'
-Programs may fail or fail to start depending on how much memory is used by the batch;
unSet variables And Or use 'start /i'

----------------------------

#9 09 Jan 2013 18:58
lawson23


My problem with start /w is that I need to run wmic commands and get return values from them and based on a FIND of those values do something. When using start /w I always just end up with an errorlevel of 1.

----------------------------

#10 09 Jan 2013 23:47
Ed Dyreen

lawson23 wrote:

My problem with start /w is that I need to run wmic commands and get return values from them and based on a FIND of those values do something. When using start /w I always just end up with an errorlevel of 1.

Hmm, if this don't work wmic may be corrupted somehow.

Code: Select all

@echo off

wmic path win32_process

start /wait "wmic" cmd /k wmic.EXE path win32_process 1^> "succes.TXT" 2^> "failed.TXT"
echo.errorLevel:%errorLevel%_

pause
exit
Have you tried a repair ? https://web.archive.org/web/20150821045 ... airwmi.htm

----------------------------

#11 10 Jan 2013 15:11
lawson23


I have located the issue. The issue for me is the Altiris Agent installed at the start of deployment. Once the Altiris Agent is installed I can no longer run a wmic command in a batch file. I can run a single command in a command window but not in a batch file. I also believe this just started with the new 7.1 sp2 mp1 version of the agent but I can't be 100% on this. I understand this is now out of the scope of the post but hopefully it can help others in locating the culprit. As it is something other than the OS.

Ed thank you for the information but can you explain a bit of this command as wouldn't this still run in a different window than my batch? I can return results in the second window but I need them returned to the batch window. Just trying to understand the command an example of what I run is:

Code: Select all

echo **********************************************************************
echo *                      Run Workstation Specific Drivers              *
echo **********************************************************************
echo %errorlevel%
echo %date%,%time%,errorlevel: %errorlevel%,Installing wmic: Begin >> c:\RISlog\Deployment.log
WMIC EXIT >> c:\RISlog\Deployment.log 2>&1
echo %date%,%time%,errorlevel: %errorlevel%,Installing wmic: End >> c:\RISlog\Deployment.log
echo **********************************************************************
echo *                      CHECK WORKSTATION MODEL                       *
echo **********************************************************************
echo %date%,%time%,errorlevel: %errorlevel%,Print Computer Model: Begin >> c:\RISlog\Deployment.log
wmic ComputerSystem Get model >> c:\RISlog\Deployment.log
echo. >> c:\RISlog\Deployment.log
echo %date%,%time%,errorlevel: %errorlevel%,Print Computer Model: End >> c:\RISlog\Deployment.log
wmic ComputerSystem Get model | findstr /i "6400"
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 GOTO :6x00
wmic ComputerSystem Get model | findstr /i "6410"
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 GOTO :6x10
wmic ComputerSystem Get model | findstr /i "6510"
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 GOTO :6x10
wmic ComputerSystem Get model | findstr /i "6420"
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 GOTO :6x20
GOTO :end 
----------------------------

#12 10 Jan 2013 17:05
Ed Dyreen


You can return results from a child cmd but not from the parent ??, That's even more weird !!!
Why would the parent be blocked from accessing WMI but not a child cmd ?
This doesn't makes sense.

Normally you don't require the start but...

Code: Select all

@echo off

start /wait "wmic" cmd /c wmic.EXE path win32_process 1^> "succes.TXT" 2^> "failed.TXT"
echo. &echo.wmic returned errorLevel:%errorLevel%_
echo. &echo.succes.TXT:
for /f delims^=^ eol^= %%? in ( "succes.TXT" ) do echo.%%?_
echo. &echo.failed.TXT:
for /f delims^=^ eol^= %%? in ( "failed.TXT" ) do echo.%%?_

pause
exit
Returns wmic's exitCode and stdOut to "succes.TXT" and stdErr to "failed.TXT" in the current directory.

Last edited by Ed Dyreen (10 Jan 2013 17:06)

----------------------------

#13 14 Jan 2013 20:18
lawson23


ok I take back what I said it is not the agent itself but a line of code I ran before the wmic commands.

This query fails the wmic commands later on:
SC query AeXNSClient | FIND "STATE" | FIND "RUNNING" >> c:\RISlog\Deployment.log 2>&1

When I Uninstalled the agent a precheck for the agent prevents the above command from running. Running the command against a different service produces the same hanging result. So what we found is it is due to the logging of that command.

remove "2>&1" from the SC command and wmic does not hang later.

So here is test code that fails:

Code: Select all

SC query SharedAccess | FIND "STATE" | FIND "RUNNING" >> c:\RISlog\Deployment.log 2>&1
WMIC EXIT >> c:\RISlog\Deployment.log 2>&1
wmic ComputerSystem Get model >> c:\RISlog\Deployment.log
end
This commands works:

Code: Select all

SC query SharedAccess | FIND "STATE" | FIND "RUNNING" >> c:\RISlog\Deployment.log
WMIC EXIT >> c:\RISlog\Deployment.log 2>&1
wmic ComputerSystem Get model >> c:\RISlog\Deployment.log
end 

Hopefully this can help someone else!!! If anyone has an explanation I would sure be interested!
Post Reply