Page 1 of 1 1
Topic Options
#198654 - 2010-05-18 11:01 PM WMIService() - Control NT services
Barrman Offline
Fresh Scripter

Registered: 2010-05-18
Posts: 5
Loc: Colorado
Hi guys,

I need some help with this function. I can not for the life of me get stop and start to work. Query works like a dream but start/stop don't do anything.

What I am trying to do is remove extra printers from several windows 7 Boxes. I have DelPrinterConnections (udf) working and it correctly removes the registry entries for my network mapped printers, however, windows 7 will not reflect these changes unless I start and stop the print spooler.

This is where WMIService() comes into play.

Here is my script (I know its full of debug code)

 Code:
;Created: 2-04-010
;Modified:


BREAK OFF

CLS

;This changes the default Windows Scripting Host to Cscript to be able to run vbs scripts directly from a command line
shell "CScript //H:CScript //S"

;*********************************************************
; Drive Mappings
;********************************************************

IF INGROUP("JAG Admin")
	USE J: "\\JAG-DC01\JAG Admin"
ENDIF

IF INGROUP("JAG Common Drives")
	USE H: "\\jag-dc01\Home"
	USE P: "\\jag-dc01\casetracking"
	USE O: "\\jag-dc02\Programs"
	USE S: "\\jag-dc01\peachtree"
ENDIF

IF INGROUP("Interns - Folder Access")
	USE S: "\\JAG-DC01\Scanned"
ENDIF


;****************************************************************
; Printer Mappings - These are only mapped if the OS is XP 
;*****************************************************************



If @ProductType = "Windows XP Professional" OR @ProductType = "Windows XP Professional Tablet PC"



	IF not PriMapState('\\JAG-DC01\BizHub Scanner Room')
		MESSAGEBOX("Old Printers Found, removing old printers and adding new ones.","Message From the Admin")
		DelPrinterConnections
		ADDPRINTERCONNECTION ("\\JAG-DC01\BizHub Scanner Room")
		ADDPRINTERCONNECTION ("\\JAG-DC01\HP2200 Judges Lounge")
		ADDPRINTERCONNECTION ("\\JAG-DC01\HP4050 Janeys Desk")
		ADDPRINTERCONNECTION ("\\JAG-DC01\HP4250 Rhondas Desk")
		ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Ellens Office")
		ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Lisas Desk")
		ADDPRINTERCONNECTION ("\\JAG-DC01\SHARP MX-5500N PCL6")
		
		SETDEFAULTPRINTER("\\JAG-DC01\HP4650 Lisas Desk")

		MESSAGEBOX("Your Default Printer Has Been Changed To HP4650 Lisas Desk","Message From the Admin")
	ENDIF





EndIf

If @ProductType = "Windows 7 Professional Edition"


    	If %PROCESSOR_ARCHITECTURE% = 'amd64' Or %PROCESSOR_ARCHITEW6432% = 'amd64'
		;MESSAGEBOX("Im 64bit","test")
		
		IF not PriMapState('\\JAG-DC01\BizHub Scanner Room')
			MESSAGEBOX("Old Printers Found, removing old printers and adding new ones.","Message From the Admin")
			DelPrinterConnections
			ADDPRINTERCONNECTION ("\\JAG-DC01\BizHub Scanner Room")
			ADDPRINTERCONNECTION ("\\JAG-DC01\HP2200 Judges Lounge")
			ADDPRINTERCONNECTION ("\\JAG-DC01\HP4050 Janeys Desk")
			ADDPRINTERCONNECTION ("\\JAG-DC01\HP4250 Rhondas Desk")
			ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Ellens Office")
			ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Lisas Desk")
			ADDPRINTERCONNECTION ("\\JAG-DC01\SHARP MX-5500N PCL6")
			
			SETDEFAULTPRINTER("\\JAG-DC01\HP4650 Lisas Desk")

			MESSAGEBOX("Your Default Printer Has Been Changed To HP4650 Lisas Desk","Message From the Admin")
		ENDIF
	




	EndIf
	
    	If %PROCESSOR_ARCHITECTURE% = 'x86' And %PROCESSOR_ARCHITEW6432% = ''
    		;MESSAGEBOX("Im 32bit","test")
		
		;IF not PriMapState('\\JAG-DC01\BizHub Scanner Room')
			MESSAGEBOX("Old Printers Found, removing old printers and adding new ones.","Message From the Admin")
			?WMIService("Spooler","QUERY")
			?WMIService("Spooler","STOP")
			?WMIService("Spooler","QUERY")
			;DelPrinterConnections
			;ADDPRINTERCONNECTION ("\\JAG-DC01\BizHub Scanner Room")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\HP2200 Judges Lounge")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\HP4050 Janeys Desk")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\HP4250 Rhondas Desk")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Ellens Office")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\HP4650 Lisas Desk")
			;ADDPRINTERCONNECTION ("\\JAG-DC01\SHARP MX-5500N PCL6")
			
			;SETDEFAULTPRINTER("\\JAG-DC01\HP4650 Lisas Desk")

			MESSAGEBOX("Your Default Printer Has Been Changed To HP4650 Lisas Desk","Message From the Admin")
		;ENDIF

    	EndIf
	
	

EndIf

IF INGROUP("No Internet Access")
	;debug on
	Shell "route delete 0.0.0.0"
	Shell "route add 0.0.0.0 mask 0.0.0.0 10.20.2.4"
End IF


function DelPrinterConnections()
 
	dim $bk,$conn 
	$bk="HKEY_CURRENT_USER\Printers\Connections" 
	do
	$conn=delkey($bk+"\"+enumkey($bk,0))
	until @error

endfunction

;FUNCTION	PriMapState 
; 
;AUTHOR		Lonkero (Jooel.Nieminen@gwspikval.com) 
; 
;ACTION		Checks for existent networkprinter connection 
; 
;VERSION	1.1.1 
; 
;CHANGES	1.1.1	- 01. november 2003 
;		 Fixed buggie descriped in: 
;		 http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=1;t=008079 
;		1.1	- 02. July 2002 
;		 added support for win9x 
;		1.0	- 01. July 2002 
;		 initial release 
; 
;SYNTAX		PriMapState(PRINTER) 
; 
;PARAMETERS	PRINTER 
;		 to be checked Printer's name 
; 
;RETURNS	1 if printer connected 
;		2 if printer is default 
;		nothing if not connected 
; 
;REMARKS	code for w9x adapted from BrianTX 
; 
;DEPENDENCIES	none 
; 
;EXAMPLE 
;		if not PriMapState('\\server\printer1') 
;		 "printer1 not connected!" 
;	        endif 
; 
;CODE 

function PriMapState($_Pri)
if @inwin=1
 if len(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_Pri))
  if split(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),",")[0]=$_Pri
   $PriMapState=2
  else
   $PriMapState=1
  endif
 endif
else
 dim $_Root,$_C,$_C2 $_Root="HKLM\System\CurrentControlSet\control\Print\Printers"
 for $_C=0 to 259
  $_C2=enumkey($_Root,$_C)
  If instr(READVALUE($_Root+"\"+$_C2,"Port"),$_Pri)
   If instr(READPROFILESTRING("%windir%\win.ini","windows","device"),$_Pri)
    $PriMapState = 2
   Else
    $PriMapState = 1
   Endif
  Endif
  if $_C2=259 $_C=$_C2 endif
 next
endif
endfunction


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


When it gets run I get "Running Auto" as the result of all three of my calls to WMIService.

Again this is running on a windows 7 32 bit machine
Any help would be greatly appreciated.

Thanks
Nick


Edited by Richard H. (2010-05-19 10:11 AM)
Edit Reason: Added code tags, moved to advanced forum

Top
#198671 - 2010-05-20 01:11 AM Re: WMIService() - Control NT services [Re: Barrman]
Barrman Offline
Fresh Scripter

Registered: 2010-05-18
Posts: 5
Loc: Colorado
Oh thank you Richard, that is much more readable. Sorry for putting it in the wrong thread.

So any help guys?

Top
#198672 - 2010-05-20 02:53 AM Re: WMIService() - Control NT services [Re: Barrman]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
I haven't used WMIService, however in the Addprinter() UDF I needed to restart the spooler service too, and the code below is how I did it.

 Code:
 
            $objservice=Getobject("WinNT://" + $remotepc + "/Spooler,Service")
            if @error
              exit -4
            endif
            $objservice.stop
            do
              sleep 0.2
            until $objservice.status=1   ;stopped   
            $objservice.start


You could wrap this up as a UDF pretty easily.

Top
#198676 - 2010-05-20 07:11 AM Re: WMIService() - Control NT services [Re: Allen]
Barrman Offline
Fresh Scripter

Registered: 2010-05-18
Posts: 5
Loc: Colorado
Thanks Allen I'll give it a try tomorrow when im in the office. Ill post back to let you know how it goes.


Best regards
Nick

Top
#198684 - 2010-05-20 06:26 PM Re: WMIService() - Control NT services [Re: Barrman]
Barrman Offline
Fresh Scripter

Registered: 2010-05-18
Posts: 5
Loc: Colorado
No luck unfortunately

I have added this function

 Code:
function STOPSPOOLER()
	DIM $remotepc,$objservice

	$remotepc=@wksta
	MESSAGEBOX($remotepc,"test")


	$objservice=Getobject("WinNT://" + $remotepc + "/Spooler,Service")
            if @error
              exit -4
            endif

            $objservice.stop
            do
              sleep 0.2
	      ?$objservice.status
            until $objservice.status=1   ;stopped
	    ?$objservice.status
	    
            ;$objservice.start



endfunction


And I get stuck in the do loop. Status reports back as 4. I assume this means running.

Any suggestions on where to go from here or if there is an easier way to start/stop the print spooler?

Thanks again guys
Nick


Edited by Mart (2010-05-21 01:16 AM)
Edit Reason: Please use code tags when posting code.

Top
#198689 - 2010-05-21 01:17 AM Re: WMIService() - Control NT services [Re: Barrman]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Permissions problems?

What happens when you drop to cmd shell and do NET STOP Spooler?

Top
#198694 - 2010-05-21 10:02 AM Re: WMIService() - Control NT services [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Add some error checking after $objservice.stop - it may provide a clue.
Top
#198700 - 2010-05-22 07:55 PM Re: WMIService() - Control NT services [Re: Richard H.]
Barrman Offline
Fresh Scripter

Registered: 2010-05-18
Posts: 5
Loc: Colorado
I think Allen might be onto something with permissions. For XP boxes shelling out and running net stop spooler runs fine. For the windows 7 boxes I get an access denied message. I have to explicitly run cmd as administrator in the windows 7 machines in order for it to successfully run net stop spooler. I was hoping that WMIService would not be subject to the same permission restrictions (based on impersonation level, and me being domain admin), but perhaps that was wishful thinking. Ill add some ?@error, and get back to you on Monday. I'm pretty sure @error was not 0, but I didn't bother to write down what it was. Silly me.
Top
#198705 - 2010-05-24 10:10 AM Re: WMIService() - Control NT services [Re: Barrman]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You may need to check the $objservice object for errors (if it has an error property) as well as @ERROR.

You could try disabling UAC and see how you get on.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 194 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.073 seconds in which 0.037 seconds were spent on a total of 13 queries. Zlib compression enabled.

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