Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
You can search the UDF collection for fnWMIService, or try the code I'm working on. This code is 90% complete - only the CREATE SERVICE action is yet to be written.
With this code, you can use:
If WMISvcMgr('Query', 'W32Time', , $Computer)[7] = 'Running'
'Is Running!'
EndIf
It also illustrates how you can use WMI to query and manage services.
_________________________ Actually I am a Rocket Scientist!
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Here are a few examples of using my function:
Code:
; list all services on local computer
$a = WMISvcMgr('List')
@SERROR ?
For Each $ in $a
Left(Split($, ',')[0] + ' ', 40) Split($, ',')[2] ?
Next
; display current status
$a = WMISvcMgr('Query', 'Spooler', , $Computer)
UBound($a) + 1 ' records' ?
@SERROR ?
For Each $ in $a
$ ?
Next
; Stop the service
WMISvcMgr('Stop', 'Spooler', , $Computer) ?
@SERROR ?
Sleep 1
; show current status
$a = WMISvcMgr('Query', 'Spooler', , $Computer)
@SERROR ?
For Each $ in $a
$ ?
Next
; Stop the service (again) and handle special message
$Err = WMISvcMgr('Stop', 'Spooler', , $Computer)
@SERROR ?
WMISvcMgr($Err) ?
Sleep 1
; Start the service
WMISvcMgr('Start', 'Spooler', , $Computer) ?
@SERROR ?
Sleep 1
; show updated status
$a = WMISvcMgr('Query', 'Spooler', , $Computer) @SERROR ?
For Each $ in $a
$ ?
Next
_________________________ Actually I am a Rocket Scientist!
If you need to do this only locally, you may also do something like this:
Code:
shell '%ComSpec% /c net start |find /c "Print Spooler" >nul 2>nul'
if not @error
"Spooler Service is running"
else
"No printing today, try tomorrow :p"
endif
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Jochen: (aka "Old Fashioned")
I almost posted my SvcList and SvcCtl UDFs, which use XNET. I'm actually upset that XNet is no longer a part of the Kix distribution - it's one of my favorite tools. I'm really bummed that it fails on my Vista x64 system. That's the reason behind the format of the values returned from my new func - it (will) match the results generated by my UDFs that use XNET.
Both of the XNET-based UDFs can be downloaded from the Resources page of my web site.
Glenn
_________________________ Actually I am a Rocket Scientist!
Curious why you're writing a new one Glenn. The one written up by Chris S. is pretty darn full. What are you planning to have that fnWMIService doesn't have?
Not sure why but http://www.kixtart.org/udf does not appear to list that UDF (maybe due to missing the open/close parens) but here is a link to it.
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I need specific input and output to replace existing UDFs that use XNET. I haven't posted this in the UDF forum yet.
fnWMIService is great, especially if you're comfortable with WMI, and programming in general. This UDF is more direct, requiring less setup and programming, as well as being a drop-in replacement for my older UDFs.
I use a modified version of fnWMIService in many other scripts where I need the extra level of control and am willing to code for it. You can find the modified version on my web site. I modify all public WMI code to allow interacting with Sealeapord's WMIAuth UDF.
Glenn
_________________________ Actually I am a Rocket Scientist!
If You want to use the names from Services, You could try this
Code:
Break On
$Service = 'Print Spooler'
If ServiceRunning($Service)
$Service + ' is Running'
Else
$Service + ' is NOT Running'
EndIf
Get $x
Function ServiceRunning($ServiceName)
Dim $oWMI,$cItems,$oItem
$oWMI = GetObject("winmgmts:root\cimv2")
$cItems = $oWMI.ExecQuery("Select * From Win32_Service Where Caption='$ServiceName' And Started=-1",,48)
For Each $oItem In $cItems
$ServiceRunning = 1
Next
EndFunction
Well then that leads to PROPRIETARY UDF which is not a good thing for most users as your UDF mods are not published and maintained here on Korg which means that if they were to become publicly used there could easily become version issues as people would probably come here for assistance and most of the regulars would not have or see your code here to help them.
If current published UDFS don't handle what you're doing then perhaps you should post and ask the author for an update, or (as you've done with a few) post your own UDF so that it is maintained here on Korg and not on your site as the main source.
As an example you and I both worked on the WMIConfirm.udf - if that does not provide what you want or if Jens' does not provide what you want then maybe ask for an updated version.
I know you already know all this stuff Glenn and I'm not meaning to slam you on this (maybe I'm the one just reading you wrong) I just don't want off site UDFs to popup and then users come here asking for help with it.
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Yes, indeed it does. The WMI UDFs are modified to add an optional $_pAuth parameter at the end of the function. The first WMI line, which instantiates the object, is modified to become the Else part of an If/Else statement. If the $_pAuth value is passed, that is used instead of the one opened in the UDF. The remainder of the UDF is generally unchanged. I've PM'd a few regarding this change, and believe that only one was updated. Funny you bring up WMIConfirm, since you and I worked on updating some other code with WMIAuth integration and I suggested you add it to that UDF as well, but the published UDF still does not have the update.
Honestly, if you look at the library I now publish, I think you'll find that nearly every public UDF has been modified in some way, often simply "de-golfed" to add comments in the code. As sweet as some of Lonk's code can be, I would never be able to have it pass a code review at the organizations I work for. I've added 30 lines of comments to a 10 line UDF to get it through the review process, and often without a single "real" code change. I have strict, documented standards in my organization, especially now that there are several people coding and using the UDF libraries. For example:
If a function deals with WMI, the name must begin with "WMI" - no leading chars, and not "WMI" elsewhere in the name. It must accept the pAuth value as the last optional field.
If the file is a library of functions, the file name must end in "lib". Libraries must have a header that describes the library, and each function must have a full header.
In addition to explicit, NoVars and NoMacros type options, I also need to insure that variable names conform to certain standards - Globals & Macros in ALL CAPS, Function vars must separate the $ from name with a "_" so the variable map file that is generated allows quick visual determination if a var is from main or UDF code. Even variable names between functions should be similar ($_Target, not $_Computer or $_Server or $_System). Most of my UDFs meet this, but we don't usually change varnames in public UDFs. We're still discussing the use of strict Hungarian Notation for variables.
Individually DIMming variables or variable sets, with comments describing each var or set. (a "set" is something like Dim $X,$Y,$Z ; loop index vars)
I'm not going to enforce the coding standards of the organization I work for upon others, which is why I choose to modify most public UDFs for internal use. I keep track of changes to the public UDFs, updating the internal code to keep up with updates on a regular basis. And - when I find bugs, I test with the raw code and if the bug is still there, I post not only the finding, but often the solution as well. A recent example is Jens' DirList - found two bugs dealing with deep recursion (one's still outstanding [ Jens - you listening? ]) I actually use both DirList and DirPlus, but prefer DirList for large file manipulations due to the way it returns data to me. I regularly use it to synchronize files with 60-70 thousand files.
Finally, I published my library because I've been including it with the KixDev toolchest for over 2 years now. It's easier to make my library available to users of my kixtart development tools that way. If someone comes to KORG with questions A) great, that's more traffic here, and B) I'll prolly be here to stick my coins into the discussion. You comment sounds like you're saying we would not discuss UDFs created or adapted from MS or ScriptLogic, and I know that's not what you mean.
Oh - BTW - with all this talk about standards and such... there's one UDF on my site that's very specialized, difficult to read, and has no comments whatsoever. It flies in the face of standards! It only works if you use KGen, and is only useful if you don't tokenize your files. Lonk would be proud!
Glenn
_________________________ Actually I am a Rocket Scientist!