Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBScript to run as different user

Posted on 2011-02-28
4
838 Views
Last Modified: 2012-05-11
I am working on a script to check an array of computers and then check an array of services to see if they are started. The problem I am having is I need this script to run as a different user and when right clicking on the script itself it does not give me that option. I believe our GPO does not allow it. How would I run this as a different user to check the status of the services running?


ArrComputer = Array("Computer1, Computer2, Computer3")
ArrServices = Array("Service1", "Service2", "Service3", "Service4")

For Each strComputer In ArrComputer
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
        For Each objItem in colItems
            For Each Service In ArrServices
                If Service = objItem.DisplayName Then
                    Wscript.echo "DisplayName: "  & objItem.DisplayName
                    Wscript.echo "State: "  & objItem.State & VbCrLf
                End If
            Next
       Next
Next

Open in new window

0
Comment
Question by:EndUserTech
  • 2
4 Comments
 
LVL 4

Expert Comment

by:loki_loki
ID: 34997616
This may help.  

http://msdn.microsoft.com/en-us/library/aa389290%28v=vs.85%29.aspx

If you need to code it into the script then you will have to leave the password in clear text, which is not ideal.

strComputer = computer name
strNamespace = "root\cimv2"
strUser = "username"
strPassword = passwordSet objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objwbemLocator.ConnectServer _
(strComputer, strNamespace, strUser, strPassword)
0
 
LVL 28

Accepted Solution

by:
omgang earned 250 total points
ID: 34997630
Give this a try.  It's a second script that calls your current script using a RunAs command.  You'll need to chagne the username and password obviously.  I set this up to use the local machine Administrator account.  If you want to use a domain account it'll need to be tweaked a bit.  Let me know.
OM Gang





                intSleep = 250   'milliseconds to wait until we send the password
                strPass = "password"     'admin acount password
                Set objNet = CreateObject("WScript.Network")
      Set wshShell = CreateObject("WScript.Shell")
      strThisComputer = objNet.ComputerName
           
            'build RunAs command so we can execute msg command as priveleged user
      strRunAsCmd = "runas /noprofile /user:" & strThisComputer & "\administrator "

            'build command to send network message
      strNetCmd = "ChangeIPAddress.bat"         '<------- change to path and name of your vbs script

      strFullCmd = strRunAsCmd & Chr(34) & strNetCmd & Chr(34)
      wshShell.Run strFullCmd
      WScript.Sleep intSleep
      wshShell.SendKeys strPass & "~"
0
 

Author Comment

by:EndUserTech
ID: 34997762
Sage,

I think your solution will work, but yes I do need to run as a domain account.
0
 
LVL 28

Expert Comment

by:omgang
ID: 34998241
So instead of obtaining the local machine we simply specify the domain for strThisComputer.  Note that to call your script we have to actually call the CScript program and specify the script to be run.  I've tested and this works.
OM Gang


               intSleep = 250   'milliseconds to wait until we send the password
                strPass = "domainuserpassword"     'acount password
                Set objNet = CreateObject("WScript.Network")
      Set wshShell = CreateObject("WScript.Shell")
      'strThisComputer = objNet.ComputerName
      strThisComputer = "domain"
           
            'build RunAs command so we can execute msg command as domain user
      strRunAsCmd = "runas /noprofile /user:" & strThisComputer & "\domainuserID "

            'build command to send network message
      strNetCmd = "cscript c:\temp\nameofscript.vbs"        '<------- change to path and name of your vbs script

      strFullCmd = strRunAsCmd & Chr(34) & strNetCmd & Chr(34)
      wshShell.Run strFullCmd
      WScript.Sleep intSleep
      wshShell.SendKeys strPass & "~"
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

860 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