convert .vbs to Visual Basic - take two

Author Message
itismike

  • Total Posts : 42
  • Scores: 0
  • Reward points : 0
  • Joined: 7/7/2007
  • Status: offline
convert .vbs to Visual Basic - take two Saturday, July 07, 2007 12:17 PM (permalink)
0
Hi Scripters!

I pieced together a script from many online examples which takes care of several obstacles that I encounter on my employer's domain when attempting to offer Unsolicited Remote Assistance, and I'd like to make it a lot prettier. I am interested in displaying a GUI to the technician that has check marks for each of the steps. I have the front-end working fine (a simple window with an input box and 4 checkmarks), but I was disappointed to learn that .vbs code isn't compatible with VB.NET. I'm looking to convert a few of the following functions into valid VB.NET code:

This portion queries the user to enter a computer hostname, and then runs psexec to add a localgroup called techSupportGroup to the Administrator's group:
' Explain psexec and need for admin password:
 prompt=msgBox("The next script attempts to add tech" _
 & "SupportGroup to the Administrator group.  Please enter the " _
 & "administrator password for the target computer when " _
 & "prompted.  Type exit when complete.",4096,"Admin Password")
 
 'Add "techSupportGroup" to the Administrator's group:
 
 objShell.Run("%COMSPEC% /K psexec \\" & strComputer & " -u " _
 & "" & strComputer & "\administrator net localgroup " _
 & "administrators ""domainName\techSupportGroup"" /add"),1,TRUE

I'm sure there exists a better method to add a name to a localgroup on a remote PC rather than psexec, but I don't know where to look to find the syntax.

Another branch of the code stops a service, performs a command-line execution, then resumes the service:
 '***********************************
 '* * * restart service * * *
 '***********************************
 '
 '
 '* * * ADDED FOR DEBUGGING ONLY * * *
 strComputer = "."
 '* * * END DEBUGGING * * *
 '
 ' ReStartService.vbs
 ' Sample script to Stop or Start a Service
 ' www.computerperformance.co.uk/
 ' Created by Guy Thomas December 2005 Version 2.4
 ' -------------------------------------------------------'
 
 ' Explain psexec and need for admin password:
 prompt=msgBox("The next script attempts to restart the " _
 & "sessmgr service.  Please enter the administrator " _
 & "password for the target computer when " _
 & "prompted.  Type exit when complete.",4096,"Admin Password")
 
 'Option Explicit
 Dim objWMIService, objItem, objService
 Dim colListOfServices, strService, intSleep 'strComputer
 'strComputer = "."
 intSleep = 5000
 WScript.Echo " Click OK, then wait " _
 & intSleep/1000 & " seconds"
 
 'On Error Resume Next
 ' NB strService is case sensitive.
 strService = " 'rdsessmgr' "
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" _
 & strComputer & "\root\cimv2")
 Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where Name ="_
 & strService & " ")
 For Each objService in colListOfServices
 	objService.StopService()
 	WSCript.Sleep intSleep
 
 	objShell.Run("%COMSPEC% /K psexec \\" & strComputer & " -u " _
 	& strComputer & "\administrator %systemroot%\system32\" _
 	& "sessmgr.exe -service"),1,TRUE
 
 	WSCript.Sleep intSleep
 	objService.StartService()
 Next
 WScript.Echo "Your "& strService & " service has Started"
 'WScript.Quit
 ' End of Example WMI script to Start / Stop services

Again, I am attempting to replace the psexec portion of the code with standard VB.NET functions.

If anyone has some ideas, I'd be grateful to being pointed in the right direction. I'm also eager to share the completed code (after I add some error-checking) in the code forum.

Thanks,
Mike
 
#1
    Rischip

    • Total Posts : 519
    • Scores: 2
    • Reward points : 0
    • Joined: 3/26/2007
    • Status: offline
    RE: convert .vbs to Visual Basic - take two Monday, July 09, 2007 7:42 AM (permalink)
    0
    There is no need for VB.NET unless that is what you want in the end.
    To do a purely vbscript WMI implementation to add the group. You would do this....
    Using WMI to do remote execution is completely silent on the remote box. (You cannot make it visible)
     
      
     strComputer = "REMOTE_PC_NAME" 'get the pc name however you wish
      
     Set locator = CreateObject("WBEMScripting.SWBEMLocator")
      
     Set objsvc = locator.ConnectServer(strComputer, "root/cimv2", "domain\username", "password") 'probably want to create a prompt as to not embed this in the script
      
     objsvc.Security_.ImpersonationLevel = 3
     objsvc.Security_.AuthenticationLevel = 6
      
     Set objWMIService = Objsvc.Get("Win32_Process")
     
     Set objInParam = objWMIService.Methods_("Create").inParameters.SpawnInstance_()
     
     ' Add the input parameters.
     objInParam.Properties_.item("CurrentDirectory") = NULL
     objInParam.Properties_.item("ProcessStartupInformation") = NULL
     objInParam.Properties_.item("CommandLine") = "net localgroup administrators domainName\techSupportGroup /add"
      
     Set objOutParams = objWMIService.ExecMethod_("Create", objInParam)
     
     

     
    #2
      itismike

      • Total Posts : 42
      • Scores: 0
      • Reward points : 0
      • Joined: 7/7/2007
      • Status: offline
      RE: convert .vbs to Visual Basic - take two Monday, July 09, 2007 5:20 PM (permalink)
      0
      Hi Rischip,
      Thanks for the informative reply!

      True, I don't need it to be VB.NET - I was just trying to add more options to the script. Is there a method in .VBS to add checkmarks to an input box?

      Secondly, I was wondering if you could give me some guidance/direction to understand some of the WMI codes here. Would you mind inserting some comments in the code or pointing me to a good resource that will help me understand some of the techniques you used in this example?

      Thanks very much,
      Mike
       
      #3
        Rischip

        • Total Posts : 519
        • Scores: 2
        • Reward points : 0
        • Joined: 3/26/2007
        • Status: offline
        RE: convert .vbs to Visual Basic - take two Tuesday, July 10, 2007 2:58 AM (permalink)
        0
        This should help

        http://msdn2.microsoft.com/En-US/library/aa389763.aspx

        http://www.computerperformance.co.uk/vbscript/wmi_process_start.htm


        http://www.google.com/search?q=remote+execute+wmi&hl=en&safe=off
         
         
        Check marks in an input box ?? I'm not sure what you mean, but I would initially say no.
        If you want anything more than straightup text entry or the use of standard buttons, then you need to move to .HTA or create activeX InternetExplorer.Application and read back the forms control values.
        <message edited by Rischip on Tuesday, July 10, 2007 3:06 AM>
         
        #4
          itismike

          • Total Posts : 42
          • Scores: 0
          • Reward points : 0
          • Joined: 7/7/2007
          • Status: offline
          RE: convert .vbs to Visual Basic - take two Wednesday, July 11, 2007 2:51 AM (permalink)
          0
          Ok, I can live without the checkboxes.  I'm still trying to decode the rest of your suggestion.  Sorry - I'm a bit of a newbie with WMI commands.

          Your suggestion about prompting for a password is what I'm working on next.  Does a method exist that would conceal the password as it was typed?  I'm of the firm belief that I should NEVER see my admin password in plain-text during routine use.
           
          #5
            Rischip

            • Total Posts : 519
            • Scores: 2
            • Reward points : 0
            • Joined: 3/26/2007
            • Status: offline
            RE: convert .vbs to Visual Basic - take two Wednesday, July 11, 2007 4:23 PM (permalink)
            0
            If you are going to run the script using cscript (console) you can use
             
             WScript.Echo "Please enter your password and press [Enter]"
             Set oPW = CreateObject("ScriptPW.Password")
             pswd = oPW.GetPassword()
             

             
            Otherwise you can use a password box in an HTA if you desire to create a gui for the app.
            Enter Password<input type="password"></input>
             
            #6
              itismike

              • Total Posts : 42
              • Scores: 0
              • Reward points : 0
              • Joined: 7/7/2007
              • Status: offline
              RE: convert .vbs to Visual Basic - take two Thursday, July 12, 2007 12:50 AM (permalink)
              0
              I think this is exactly what I'm looking for (or close enough :)  )

              I did some searching for "ScriptPW.Password" and found a more detailed explaination of it's use on "The Scripting Guy's" site.   As you hinted at, I cannot do this in an Input box, so I'll need to open a console first.  I'm looking at other code and it looks like this is what opens a console:

               objShell.Run("%COMSPEC% /C [commands go here]")
               

              where /C will close the console when the command is complete.  So how would I piece these code segments togther to get the desired results?

              Desired results:
              A console opens and says: "Please enter the administrator password for the target computer and press [Enter]"
              The user enters a string, presses enter, and the console closes, while pswd contains the value of the password for use in the next code segment.
               
              #7
                itismike

                • Total Posts : 42
                • Scores: 0
                • Reward points : 0
                • Joined: 7/7/2007
                • Status: offline
                RE: convert .vbs to Visual Basic - take two Friday, July 13, 2007 1:09 AM (permalink)
                0
                Still trying to insert this into my script.  The rest of the script is running from a .vbs file that is executed from a double-click, so it's running from the WScript service - not CScript.  When I try to impliment the code above into the script, it generates object errors:
                 dim cmdline                    'command line arguments
                 dim oPW                        'objectPwd
                 dim pswd                    'pwd
                 '
                 dim objShell : Set objShell = CreateObject("WScript.Shell")
                 '
                 cmdline = "Please enter admin password for target PC and press [Enter]"
                 'get password through VBS without displaying value to shoulder-surfers
                 objShell.Run("%COMSPEC% /C echo " & cmdline),1,TRUE
                 Set oPW = CreateObject("ScriptPW.Password")
                 pswd = oPW.GetPassword()
                 '
                 '
                 WScript.Echo "your password is " & pswd
                 

                 
                #8
                  itismike

                  • Total Posts : 42
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 7/7/2007
                  • Status: offline
                  RE: convert .vbs to Visual Basic - take two Friday, July 13, 2007 4:52 AM (permalink)
                  0
                  OK i am now using the 'Force script to run in CScript' from the FAQ page.  It runs correctly (after adding some Chr(34) around the pathname) and is an adequate solution.

                  Last difficulty is restarting the sessmgr service, and running the following command on the remote PC:

                  %systemroot%\system32\sessmgr.exe -service

                  I was using psexec to accomplish this, but if I can do it without psec, the script will be MUCH more user-friendly.

                  Thanks very much for your help so far!
                  -Miike
                  <message edited by itismike on Friday, July 13, 2007 5:39 AM>
                   
                  #9
                    Rischip

                    • Total Posts : 519
                    • Scores: 2
                    • Reward points : 0
                    • Joined: 3/26/2007
                    • Status: offline
                    RE: convert .vbs to Visual Basic - take two Saturday, July 14, 2007 2:01 PM (permalink)
                    0
                    well you should be able to run any command remotely using the method in the first post. It will however be invisible.
                    If you are just looking to start a service, use the SC command, should be native in XP, might need a resource kit for 2K or below.
                     
                     
                    #10
                      itismike

                      • Total Posts : 42
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 7/7/2007
                      • Status: offline
                      RE: convert .vbs to Visual Basic - take two Saturday, July 14, 2007 2:33 PM (permalink)
                      0
                      Thanks for the reply. I've learned much in the past few days, and am nearly ready to post the complete code on the forum. I ended up breaking one very long script into about 12 individual subroutines, and hope the modularity will help me and others reuse this code to create more useful scripts.

                      One thing I am unclear about and am still researching is how subroutines or functions within a .vbs script handle returning a variable. It looks like the only difference between a subroutine and a function is that a function can be declared as a certain type, and through this naming convention it will return a value:
                       str return_variable = function_name(argument1, argument2, …, argumentn)
                       
                       
                       function function_name (arg1, arg2,...argn)
                           function does it's thing
                           function_name = result
                       end function
                       


                      So the above will set the value of 'return_variable' to the result of the function. Is this how it would be implemented?:
                       function AskForAndMaskAdminPassword (strComputer, strAdminAcct)
                       
                       strPassword = AskForAndMaskAdminPassword
                       
                       +++++++++++++++ FUNCTIONS AND SUBROUTINES ++++++++++++++++++++++++++++++++
                         Function AskForAndMaskAdminPassword (ComputerName, AdminAcct)
                       
                       '  GET PASSWORD FROM USER INPUT (MASKED)
                       
                           dim oPW					'objectPwd
                       
                           WScript.Echo Chr(13) & Chr(10) & "Enter password for the " & AdminAcct _
                             & " account on " & ComputerName & " and press [Enter]." _
                             & Chr(13) & Chr(10) & "  (or press [Enter] to quit)"
                       
                           Set oPW = CreateObject("ScriptPW.Password")
                       
                           AskForAndMaskAdminPassword = oPW.GetPassword()
                       
                           If AskForAndMaskAdminPassword = "" Then WScript.Quit
                       
                           WScript.Echo Chr(13) & Chr(10) & "password stored..."
                       
                         End Function
                       +++++++++++++++ END FUNCTIONS AND SUBROUTINES ++++++++++++++++++++++++++++
                       


                      Do I need to declare a variable type or anything?
                       
                      #11

                        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