Using Choices In a VBScript

Author Message
mushroomwolf

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 1/26/2012
  • Status: offline
Using Choices In a VBScript Friday, January 27, 2012 3:02 AM (permalink)
0
Hello, this is my first post on your website, but I've been a lurker for awhile now while trying to learn VB to automate a lot of the work I do on a daily basis, and I'm having trouble getting a code of mine to work;

Basically, I need to rename computer based on the company they're going to (9 different naming conventions), a dash, and then the Dell service tag.

What I want it to do:
When I click on it, I want it to be an input box asking for a number correlating to the business unit (1 is this unit, 2 is this unit, etc.), then pull the dell service tag, then rename the computer combining the two.

I'm running into a problem where, if you select "1" from the options menu, it works fine, but 2 through 9 always throw an error stating that "err = objComputer.Rename(name)" has incorrect syntax.

Can you help?

Script:


strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSN = objItem.SerialNumber
If strSN <> "" Then exit For
Next
Dim Input
Input = InputBox("Enter your choice:" & vbCrLF & "1: ASC" & vbCrLF & "2: ATI" & vbCrLF & "3: BRC" & vbCrLF & "4: CLK" & vbCrLF & "5: EFX" & vbCrLF & "6: JBL" & vbCrLF & "7: NASM" & vbCrLF & "8: NHA" & vbCrLF & "9: LOANER")
If Input = "1" then Name = "ASC"&"-"&StrSN
If Input = "2" then Name = "ATI"&"-"&StrSN
If Input = "3" then Name = "BRC"&"-"&StrSN
If Input = "4" then Name = "CLK"&"-"&StrSN
If Input = "5" then Name = "EFX"&"-"&StrSN
If Input = "6" then Name = "JBL"&"-"&StrSN
If Input = "7" then Name = "NASM"&"-"&StrSN
If Input = "8" then Name = "NHA"&"-"&StrSN
If Input = "9" then Name = "ASCLNR-"&StrSN
else
MsgBox ("Invalid Input")
End If
End If
End If
End If
End If
End If
End If
End If
End If
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers err = objComputer.Rename(name)
if err <> 0 then wscript.echo "There was an error renaming the machine. Please restart, and try again."
else wscript.echo "Machine successfully renamed: " & Name
end if
Next


Thanks!
 
#1
    dm_4ever

    • Total Posts : 3687
    • Scores: 82
    • Reward points : 0
    • Joined: 6/29/2006
    • Location: Orange County, California
    • Status: offline
    Re:Using Choices In a VBScript Friday, January 27, 2012 3:37 AM (permalink)
    0
    I think the problem is the way you built your If statement...a Case statement may work better in this case.
     
     Dim Input : Input = InputBox("Enter your choice:" & vbCrLf & Join(Array("1: ASC", "2: ATI", "3: BRC", _
     "4: CLK", "5: EFX", "6: JBL", "7: NASM", "8: NHA", "9: LOANER"), vbCrLf))
    
    Select Case Input
     Case "1" Name = "ASC" & "-" & strSN
     Case "2" Name = "ATI" & "-" & strSN
     Case "3" Name = "BRC" & "-" & strSN
     Case "4" Name = "CLK" & "-" & strSN
     Case "5" Name = "EFX" & "-" & strSN
     Case "6" Name = "JBL" & "-" & strSN
     Case "7" Name = "NASM" & "-" & strSN
     Case "8" Name = "NHA" & "-" & strSN
     Case "9" Name = "ASCLNR-" & "-" & strSN
     Case Else
     MsgBox "Invalid Input"
    End Select
    
    MsgBox Name 

    dm_4ever

    My philosophy: K.I.S.S - Keep It Simple Stupid
    Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
    Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      mushroomwolf

      • Total Posts : 3
      • Scores: 0
      • Reward points : 0
      • Joined: 1/26/2012
      • Status: offline
      Re:Using Choices In a VBScript Friday, January 27, 2012 3:53 AM (permalink)
      0
      Excellent!
       
      I'm having another issue you may be able to help with; I've combined this (now working) script with another one to join a domain, I've made sure that all variables are set before they're needed, and the script to join the domain works if I run it by itself, however when I run the full rename and join the domain script, I have it reboot at the end.  After the reboot, generally only the name change has stuck, and I have to join it to the domain and reboot again, which doesn't particularly save time and defeats the purpose of the script.  Any ideas?  I can post the script if nothing jumps to mind.  I also put a 20 second delay between renaming the machine, and joining the domain, because I read that having it go straight into joining after renaming can cause this issue.
       
      #3
        mushroomwolf

        • Total Posts : 3
        • Scores: 0
        • Reward points : 0
        • Joined: 1/26/2012
        • Status: offline
        Re:Using Choices In a VBScript Friday, January 27, 2012 4:29 AM (permalink)
        0
        Script:


        CONST JOIN_DOMAIN = 1
        CONST ACCT_CREATE = 2
        CONST ACCT_DELETE = 4
        CONST WIN9X_UPGRADE = 16
        CONST DOMAIN_JOIN_IF_JOINED = 32
        CONST JOIN_UNSECURE = 64
        CONST MACHINE_PASSWORD_PASSED = 128
        CONST DEFERRED_SPN_SET = 256
        CONST INSTALL_INVOCATION = 262144

        strDomain = ""
        strPassword = ""
        strUser = ""
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
        For Each objItem In colItemsstrSN = objItem.SerialNumber
        If strSN <> "" Then exit For
        Next
        Dim Input : Input = InputBox("Enter your choice:" & vbCrLf & Join(Array("1: ASC", "2: ATI", "3: BRC", "4: CLK", "5: EFX", "6: JBL", "7: NASM", "8: NHA", "9: LOANER"), vbCrLf))
        Select Case Input
        Case "1" Name = "ASC" & "-" & strSN
        Case "2" Name = "ATI" & "-" & strSN
        Case "3" Name = "BRC" & "-" & strSN
        Case "4" Name = "CLK" & "-" & strSN
        Case "5" Name = "EFX" & "-" & strSN
        Case "6" Name = "JBL" & "-" & strSN
        Case "7" Name = "NASM" & "-" & strSN
        Case "8" Name = "NHA" & "-" & strSN
        Case "9" Name = "ASC-LNR-" & strSN
        Case else
        MsgBox ("Invalid Input")
        Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
        For Each objComputer in colComputers
        err = objComputer.Rename(name)
        if err <> 0 then
        wscript.echo "There was an error renaming the machine. Please restart, and try again."
        Wscript.Quit
        else
        wscript.echo "Machine successfully renamed: " & Name
        end if
        Next
        WScript.Sleep 20000
        SET objNetwork = CREATEOBJECT("WScript.Network")strComputer = objNetwork.ComputerName
        SET objComputer = GETOBJECT("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
        Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
        For Each objOperatingSystem in colOperatingSystems
        ObjOperatingSystem.Reboot()
        Next


        except the domain, username, and password are all filled in on mine 
         
        It's giving me an "expected 'end'" message on line 61, which is the very last Next statement, after Reboot.
        <message edited by mushroomwolf on Friday, January 27, 2012 4:31 AM>
         
        #4
          dm_4ever

          • Total Posts : 3687
          • Scores: 82
          • Reward points : 0
          • Joined: 6/29/2006
          • Location: Orange County, California
          • Status: offline
          Re:Using Choices In a VBScript Friday, January 27, 2012 4:30 AM (permalink)
          0
          You can post your code in case someone has some insight or search the forum since I've seen several examples throughout the years of people renaming their computers and joining to the domain.
          dm_4ever

          My philosophy: K.I.S.S - Keep It Simple Stupid
          Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
          Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9