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