[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Syntax error on Sub Routine??

Posted on 2007-07-26
6
Medium Priority
?
484 Views
Last Modified: 2012-05-05
I have a script that deletes a key and all the sub keys as well.  It is :

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = InputBox("Computer Here")

strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath

Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub






However I need the script to read from a list of machines and do the same thing for all the machines.  I came up with the following, but it is giving me a syntax error on the Sub line and I can't figure out why.

INPUT_FILE_NAME = InputBox("Computer List")

Const FOR_READING = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)

strComputers = objFile.ReadAll

objFile.Close

arrComputers = Split(strComputers, vbCrLf)

For Each strComputer In arrComputers

Const HKEY_LOCAL_MACHINE = &H80000002

strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath

Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath

End Sub
Next
0
Comment
Question by:sunnyd24
  • 3
  • 3
6 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 19578492

Subroutines don't sit in the normal flow of code. They allow you to break sections out of the normal flow. Either to make it reusable, or to make it look neater.

You also wouldn't really see much point in passing a global constant into the subroutine as a parameter, especially not with the same name.

With a quick rewrite we end up with:



Const FOR_READING = 1

Sub DeleteSubkeys(strKeyPath)
    Const HKEY_LOCAL_MACHINE = &H80000002

    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub

INPUT_FILE_NAME = InputBox("Computer List")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)

strComputers = objFile.ReadAll

objFile.Close

arrComputers = Split(strComputers, vbCrLf)

For Each strComputer In arrComputers
      strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution"

      Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
            strComputer & "\root\default:StdRegProv")

      DeleteSubkeys strKeyPath

      Set objReg = Nothing
Next
0
 

Author Comment

by:sunnyd24
ID: 19578534
Line: 10
Char: 13
Error: Wrong number of arguments or invalid property assignment: 'DeleteSubkeys'
Code: 800A01C2

Thats the error I get, but you did give me some ideas
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 19578548

Oops, sorry:


Const FOR_READING = 1

Sub DeleteSubkeys(strKeyPath)
    Const HKEY_LOCAL_MACHINE = &H80000002

    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub

INPUT_FILE_NAME = InputBox("Computer List")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)

strComputers = objFile.ReadAll

objFile.Close

arrComputers = Split(strComputers, vbCrLf)

For Each strComputer In arrComputers
      strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution"

      Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
            strComputer & "\root\default:StdRegProv")

      DeleteSubkeys strKeyPath

      Set objReg = Nothing
Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:sunnyd24
ID: 19578579
Line 31
Char 7
Error 0x80041021
Code 80041021
Source (null)
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 19578606

That's this bit:

      Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
            strComputer & "\root\default:StdRegProv")

Lets change it a little and pop some error control in. Could just be a problem connecting to the PC in question.


Const FOR_READING = 1

Sub DeleteSubkeys(strKeyPath)
    Const HKEY_LOCAL_MACHINE = &H80000002

    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub

INPUT_FILE_NAME = InputBox("Computer List")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)

strComputers = objFile.ReadAll

objFile.Close

arrComputers = Split(strComputers, vbCrLf)

For Each strComputer In arrComputers
      strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution"

      On Error Resume Next : Err.Clear
      Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
            strComputer & "\root\default:StdRegProv")
      If Err.Number = 0 Then
            ' No Error. Run the key deletion.
            On Error Goto 0

            DeleteSubkeys strKeyPath
      Else
            On Error Goto 0
            WScript.Echo "Error Connecting to " & strComputer & ": " & Err.Description
      End If

      Set objReg = Nothing
Next
0
 

Author Comment

by:sunnyd24
ID: 19581815
Thank you for being patient!  There was a carriage return after the last computer that was causing the error.  Much appreciated!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
The article covers five tools all IT professionals should know about, as they up productivity by a great deal!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question