Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk <JorisvanderStruijk@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 01:40:04 -0800
Thx Alex. It realy gave me some better views about things.
About your article in Windows IT Pro, maby it's a good thing to post it (or
some parts of it) if that's possible and your ok with it. It could give me,
and others a better inside view of scripting for a lot of computers.
Grx,
Joris
"Alex K. Angelopoulos [MVP]" wrote:
.
"Joris van der Struijk" <JorisvanderStruijk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:BE341A65-F1A4-4684-B894-A94A61FF9FBA@xxxxxxxxxxxxxxxx
Never mind the remark about locked machines. I made an error in a previous
script, it now works with the script i provided.
What i do find annoying is those errors, these keep halting and messing up
my script. For most of them i have no idea whats cousing them.
I wrote a fairly detailed article with an accompanying mass-shutdown script
for Windows systems. If you happen to be a Windows IT Pro subscriber, the
article can be found here:
http://www.windowsitpro.com/Windows/Article/ArticleID/44710/44710.html
If not, the following may help - unfortunately done from memory since I
don't have the article handy. Some of the comments below are NOT in the
article, in any case. Furthermore, my code was structured differently from
yours to make it convenient for list processing, so it may be difficult to
compare precisely to yours. :)
The fundamental issue is that if you perform a task that works on multiple
remote machines, you can guarantee that it will have a high failure rate.
The solution is to neither halt on all errors nor to suppress them; you need
to track the computers where it fails and what the reason was. For that
reason, I would suggest that your script should always run from cscript;
this lets you emit the error and (if you want) success information in a
console window for further use.
(1) There are situations where you can get other weird output from a ping
attempt that makes hash of the IsConnectible function you're using, as you
have apparently found out; it is also complicated by the fact that machine
firewalls may dump ping attempts even on a LAN. XP has a fast failure on
remote WMI calls, so you may find it best to just dump the IsConnectible and
go straight to a WMI connection attempt.
(2) Your first failure point is going to be connecting to the remote
machine. Since it could happen due to authentication errors, the machine
being down, DCOM being too tight, the firewall not letting in the traffic,
or a host of other minor reasons, I suggest momentarily suppressing errors
and then testing for a non-zero error. You want to emit the computer name,
the error numeric, and the error description, then turn error suppression
off and jump to the next machine in the list.
(3) If the connection succeeded, then you have other failure conditions when
you try to shut down the system. Again, you need to control errors here and
capture the result for redisplay.
Here's an example rewrite of your loop implementing this approach. It will
only work with cscript as the host. This is "air code" so it may have a
problem I missed, but the general concept is that it does what I outlined
above - direct connect, suppress errors, and echo both error and success
information to stderr and stdout.
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name")
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
If Err.Number = 0 Then
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
If Err.Number = 0 Then
For Each objOperatingSystem In colOperatingSystems
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next
End If
End If
If Err.Number <> 0 Then
WScript.StdErr.WriteLine "error:" & strComputer & ":" _
& Err.Number & ":" & Err.Description
Else
WScript.StdOut.WriteLine "success:" & strComputer
End If
On Error Goto 0
objRecordSet.MoveNext
Loop
"Jason Gurtz" wrote:
Joris van der Struijk wrote:
Addon:
Also we would like that a workstation that's locked is also shutdown by
this
script. But i have no idea how to accomplice this.
[...]
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Looking at
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_operatingsystem.asp>
I don't see how you could be any more forceful about shutting a machine
down. Maybe test out the shutdown.exe command and see if it can
shutdown a machine even if it's locked (check out the -f flag) You
could then call that from your script instead of the Win32Shutdown
method. I suspect that it probably calls similar methods under the
covers anyway so YMMV.
~Jason
--
- References:
- Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk
- RE: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk
- Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Jason Gurtz
- Finetuning: Remote Shutdown with WMI, some errors occur.
- Prev by Date: inventory all software from computers on a domain
- Next by Date: RE: [MSH] bug? function returns string-array with one element as s
- Previous by thread: Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- Next by thread: Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- Index(es):
Relevant Pages
|
Loading