Page 1 of 1 1
Topic Options
#147494 - 2005-09-11 06:20 AM WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
Can someone tell me, what I'm doing wrong?
Have tried changing the variable for $computer name -
it does work when an explict computer name is used,
But I would like to check 50 plus servers...
and not do it manually.

Thanks for your advice!

;---------------------------------------------
code
Break=on
$LogFile = '090805.log'
Call 'C:\KixScripts\UDF\NetView.udf'
IF RedirectOutput($LogFile,1) = 0
for each $computer in NetView
$xQuery = WMIService("SpntSvc","query","$computer")
$xMode = WMIService("SpntSvc","startmode","$computer")
$computer + $xQuery + $xMode ?
next
endif

function WMIService($service,optional $action,optional $startmode,optional $computer)
Dim $wmi,$svc,$services,$sm,$st

if not $computer $computer=@wksta endif
$wmi=getobject("winmgmts:{impersonationlevel=impersonate}!//$computer")
$services=$wmi.ExecQuery("Select * from Win32_Service where name = '$service'")
if not @error
for each $svc in $services
If instr("AUTOMATIC,MANUAL,DISABLED",$startmode)
$ = $svc.ChangeStartMode($startmode)
endif
if $svc.State <> $action
select
case $action = "START" $ = $svc.StartService()
case $action = "STOP" $ = $svc.StopService()
case $action = "PAUSE" $ = $svc.PauseService()
case $action = "RESUME" $ = $svc.ResumeService()
endselect
endif
next
$wmi=getobject("winmgmts:{impersonationlevel=impersonate}!//$computer")
$services=$wmi.ExecQuery("Select * from Win32_Service where name = '$service'")
for each $svc in $services
$sm=$svc.StartMode
$st=$svc.State
$WMIService = "$st $sm"
next
$wmi=0
else
exit 2
endif
endfunction

Top
#147495 - 2005-09-11 02:42 PM Re: WMI Service state
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Don't put vars in quotes!
Please use code tags when posting.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#147496 - 2005-09-11 06:21 PM Re: WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
Have tried without quotes, still not working...

------------------------------------------------
repostCode:


Break=on
$LogFile = '090805.log'
Call 'C:\KixScripts\UDF\NetView.udf'
IF RedirectOutput($LogFile,1) = 0
for each $computer in NetView
$xQuery = WMIService("SpntSvc","query","$computer")
$xMode = WMIService("SpntSvc","startmode","$computer")
$computer + $xQuery + $xMode ?
next
endif

;
;
function WMIService($service,optional $action,optional $startmode,optional $computer)
Dim $wmi,$svc,$services,$sm,$st

if not $computer $computer=@wksta endif
$wmi=getobject("winmgmts:{impersonationlevel=impersonate}!//$computer")
$services=$wmi.ExecQuery("Select * from Win32_Service where name = '$service'")
if not @error
for each $svc in $services
If instr("AUTOMATIC,MANUAL,DISABLED",$startmode)
$ = $svc.ChangeStartMode($startmode)
endif
if $svc.State <> $action
select
case $action = "START" $ = $svc.StartService()
case $action = "STOP" $ = $svc.StopService()
case $action = "PAUSE" $ = $svc.PauseService()
case $action = "RESUME" $ = $svc.ResumeService()
endselect
endif
next

$wmi=getobject("winmgmts:{impersonationlevel=impersonate}!//$computer")
$services=$wmi.ExecQuery("Select * from Win32_Service where name = '$service'")
for each $svc in $services
$sm=$svc.StartMode
$st=$svc.State
$WMIService = "$st $sm"
next
$wmi=0
else
exit 2
endif

endfunction


Top
#147497 - 2005-09-11 07:53 PM Re: WMI Service state
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Can you post the NetView UDF your using ?

-Shawn

Top
#147498 - 2005-09-11 08:27 PM Re: WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
Code:

function netview(optional $domain)
dim $array[255]
dim $redim $redim = 255
dim $i $i = 0 dim $j $j = 0
dim $tempfile $tempfile = "%temp%\netview.tmp"
if $domain
$domain = "/domain:$domain"
endif
if exist("$tempfile")
del "$tempfile"
endif
shell '%comspec% /c net view $domain >"$tempfile"'
if @error = 0
if open(10,$tempfile) = 0
for $j = 1 to 5
$line = readline(10) ; skip headings
next
while @error = 0
$array[$i] = rtrim(substr($line,3,instr($line," ")-1))
$i=$i+1
if $i = $redim
$redim=$redim*2
redim preserve $array[$redim]
endif
$line = readline(10)
loop
$=close(10)
del "$tempfile"
if $i
redim preserve $array[$i-4]
$netview = $array
exit @error
endif
endif
endif
$netview = 0
exit @error
endfunction


Top
#147499 - 2005-09-11 08:48 PM Re: WMI Service state
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Between these two lines:

Code:

IF RedirectOutput($LogFile,1) = 0
for each $computer in NetView



put this

Code:

IF RedirectOutput($LogFile,1) = 0

$computers = NetView()
?"Count=" UBound($computers)

for each $computer in $computers



-Shawn

Top
#147500 - 2005-09-11 09:27 PM Re: WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
Sorry, maybe I didn't explain the problem...

I'm getting a list of computers but not the
information about the service (state or mode)

Top
#147501 - 2005-09-11 09:32 PM Re: WMI Service state
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ok, try this then - between these two lines:

Code:

for each $computer in NetView
$xQuery = WMIService("SpntSvc","query","$computer")



put this:

Code:

for each $computer in NetView
$computer = substr($computer,3)
$xQuery = WMIService("SpntSvc","query","$computer")



This strips-off those double whack-whacks from the hostname ...

-Shawn

Top
#147502 - 2005-09-11 09:35 PM Re: WMI Service state
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
plus, take off those double-quotes before the no-vars-in-string police haul your butt over ;0)

Code:

$xQuery = WMIService("SpntSvc","query",$computer)


Top
#147503 - 2005-09-11 10:07 PM Re: WMI Service state
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You (the script) need(s) to have local admin rights on the remote computers in order to check for the remote computer's service status.
_________________________
There are two types of vessels, submarines and targets.

Top
#147504 - 2005-09-11 11:48 PM Re: WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
oops "OUTPUT empty", the VAR quotes are gone and Admin Rights to all PC's.
Could it be the "//" in the UDF, before $computer...
Quote:


$wmi=getobject("winmgmts:{impersonationlevel=impersonate}!//$computer")




Code:

$xQuery = WMIService("SpntSvc","query",$computer)
$xMode = WMIService("SpntSvc","startmode",$computer)



Edited by RickB (2005-09-12 04:40 AM)

Top
#147505 - 2005-09-12 06:03 AM Re: WMI Service state
RickB Offline
Fresh Scripter

Registered: 2005-04-21
Posts: 11
Loc: Atlanta, Ga.
located another UDF for the Service status...
"fnWMIService"
Thanks for all the input!

Code:

http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=83246


Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2375 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.063 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org