From: urkec on
"Jeff Lynch" wrote:

> Been busy cleaning up some code and i noticed that one of my scripts
> wasn't using Option Explicit. Now i am no guru but i figure it's always
> a better option to use that setting. Anyway here is the Scripts
> Intention and the problem will follow:
> Script Purpose:
> Kill Hung service whether local or remote.
> Problem:
> When I enable 'Use Option Explicit the script breaks and will no longer
> kill a running service on the local computer.
>
> Any advise on why option explicit breaks the code would be appreciated
>
> Script:
>
> 'Option Explicit
> 'Dim strServerName,strComputerName,strServiceName,strWQL
> KillService "usa-jlynch","Google Updater Service"
> '#######################################################################
> #######
> ' KillService -Kills service by matching Service to ProcessID to then
> Terminate
> '
> 'example:
> ' KillProcess "usa-jlynch","Ad-Aware 2007 Service"
> ' **Problem with Option Explicit, if turned on Local Services will not
> be killed
> '#######################################################################
> #######
> Function KillService(strServername,strServiceName)
> Dim objItem, objService, objProcess,strServiceList,strProcessKill
> Dim
> colListOfServices,colProcessList,strQuery,strServiceProcessID,strProcess
> Name, intRC
> strQuery="SELECT * FROM Win32_Service WHERE DisplayName = '" &
> strServiceName & "'"
> On Error Resume Next
> Set colListOfServices= objWMI(strServerName,strQuery)
> If err.number=0 Then
> err.clear
> For Each objService in colListOfServices
> strServiceProcessID = objService.ProcessId
> Next
> Wscript.Echo "The Process ID for "&strServiceName&" is :
> "&strServiceProcessID
> If strServiceProcessID>0 Then
> 'now to do the termination of the service's matching process
> strQuery = "Select * from Win32_Process Where ProcessID= '" &
> strServiceProcessID & "'" 'define WMI query
> Set colProcessList=objWMI(strServerName, strQuery)'retrieve WMI
> collection by calling the objWMI function
> 'wscript.echo err.number
> If err.number=0 Then
> For Each objWMIProcess In colProcessList
> intRC = objWMIProcess.Terminate()
> wscript.echo err.number
> Next
> Select Case intRC
> Case 0 Wscript.Echo " Terminated"
> Case 2 Wscript.Echo " Access denied"
> Case 3 Wscript.Echo " Insufficient privilege"
> Case 9 Wscript.Echo " Path not found"
> Case 21 Wscript.Echo " Invalid parameter"
> Case Else Wscript.Echo " Unable to terminate for undetermined
> reason"
> End Select
> End If
> Else
> wscript.echo "Invalide Process ID or Process terminated without
> intervention"
> End If
> End If
> End Function
>
>
> Function objWMI(strComputer, strWQL)
> Dim wmiNS, objWMIService
> wmiNS = "\root\cimv2"
> On Error Resume Next
> Set objWMIService=GetObject("winmgmts:" _
> &"{impersonationLevel=impersonate}!\\" _
> & strComputer & wmiNS)
> objWMIService.Security_.ImpersonationLevel = 3
> objWMIService.Security_.privileges.addasstring "SeDebugPrivilege",
> True
> Select Case Err.Number
> Case 0
> ' Success
> Set objWMI= objWMIService.ExecQuery(strWQL)
> Case 70
> ' Run-time error '70': Permission denied
> wscript.echo "Access Denied connecting to "&strComputer
> 'Set objWMI="NA"
> Case 424
> ' The remote server machine does not exist or is unavailable
> wscript.echo "The server " &UCase(strComputer)& " is unavailable or
> does not exist!"
> Case 462
> ' The remote server machine does not exist or is unavailable
> wscript.echo "The server " &UCase(strComputer)& " is unavailable or
> does not exist!"
> Case Else
> wscript.echo "Unknown error connecting to server
> "&UCase(strComputer)
> End Select
> On Error GoTo 0
> Set objWMIService= Nothing
> End Function
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
>


Your objWMIProcess is undefined.


--
urkec



From: Joe Fawcett on


"urkec" <urkec(a)discussions.microsoft.com> wrote in message
news:F06C4182-EF4F-4716-9766-AF9036C72A81(a)microsoft.com...
> "Jeff Lynch" wrote:
>
>> Been busy cleaning up some code and i noticed that one of my scripts
>> wasn't using Option Explicit. Now i am no guru but i figure it's always
>> a better option to use that setting. Anyway here is the Scripts
>> Intention and the problem will follow:
>> Script Purpose:
>> Kill Hung service whether local or remote.
>> Problem:
>> When I enable 'Use Option Explicit the script breaks and will no longer
>> kill a running service on the local computer.
>>
>> Any advise on why option explicit breaks the code would be appreciated
>>
>> Script:
>>
>> 'Option Explicit
>> 'Dim strServerName,strComputerName,strServiceName,strWQL
>> KillService "usa-jlynch","Google Updater Service"
>> '#######################################################################
>> #######
>> ' KillService -Kills service by matching Service to ProcessID to then
>> Terminate
>> '
>> 'example:
>> ' KillProcess "usa-jlynch","Ad-Aware 2007 Service"
>> ' **Problem with Option Explicit, if turned on Local Services will not
>> be killed
>> '#######################################################################
>> #######
>> Function KillService(strServername,strServiceName)
>> Dim objItem, objService, objProcess,strServiceList,strProcessKill
>> Dim
>> colListOfServices,colProcessList,strQuery,strServiceProcessID,strProcess
>> Name, intRC
>> strQuery="SELECT * FROM Win32_Service WHERE DisplayName = '" &
>> strServiceName & "'"
>> On Error Resume Next
>> Set colListOfServices= objWMI(strServerName,strQuery)
>> If err.number=0 Then
>> err.clear
>> For Each objService in colListOfServices
>> strServiceProcessID = objService.ProcessId
>> Next
>> Wscript.Echo "The Process ID for "&strServiceName&" is :
>> "&strServiceProcessID
>> If strServiceProcessID>0 Then
>> 'now to do the termination of the service's matching process
>> strQuery = "Select * from Win32_Process Where ProcessID= '" &
>> strServiceProcessID & "'" 'define WMI query
>> Set colProcessList=objWMI(strServerName, strQuery)'retrieve WMI
>> collection by calling the objWMI function
>> 'wscript.echo err.number
>> If err.number=0 Then
>> For Each objWMIProcess In colProcessList
>> intRC = objWMIProcess.Terminate()
>> wscript.echo err.number
>> Next
>> Select Case intRC
>> Case 0 Wscript.Echo " Terminated"
>> Case 2 Wscript.Echo " Access denied"
>> Case 3 Wscript.Echo " Insufficient privilege"
>> Case 9 Wscript.Echo " Path not found"
>> Case 21 Wscript.Echo " Invalid parameter"
>> Case Else Wscript.Echo " Unable to terminate for undetermined
>> reason"
>> End Select
>> End If
>> Else
>> wscript.echo "Invalide Process ID or Process terminated without
>> intervention"
>> End If
>> End If
>> End Function
>>
>>
>> Function objWMI(strComputer, strWQL)
>> Dim wmiNS, objWMIService
>> wmiNS = "\root\cimv2"
>> On Error Resume Next
>> Set objWMIService=GetObject("winmgmts:" _
>> &"{impersonationLevel=impersonate}!\\" _
>> & strComputer & wmiNS)
>> objWMIService.Security_.ImpersonationLevel = 3
>> objWMIService.Security_.privileges.addasstring "SeDebugPrivilege",
>> True
>> Select Case Err.Number
>> Case 0
>> ' Success
>> Set objWMI= objWMIService.ExecQuery(strWQL)
>> Case 70
>> ' Run-time error '70': Permission denied
>> wscript.echo "Access Denied connecting to "&strComputer
>> 'Set objWMI="NA"
>> Case 424
>> ' The remote server machine does not exist or is unavailable
>> wscript.echo "The server " &UCase(strComputer)& " is unavailable or
>> does not exist!"
>> Case 462
>> ' The remote server machine does not exist or is unavailable
>> wscript.echo "The server " &UCase(strComputer)& " is unavailable or
>> does not exist!"
>> Case Else
>> wscript.echo "Unknown error connecting to server
>> "&UCase(strComputer)
>> End Select
>> On Error GoTo 0
>> Set objWMIService= Nothing
>> End Function
>>
>>
>>
>> *** Sent via Developersdex http://www.developersdex.com ***
>>
>
>
> Your objWMIProcess is undefined.
>
>
> --
> urkec
>
>
>
Yes, you really need to remove On Error Resume Next when debugging.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name