Solved

VBScript to run as different user

Posted on 2011-02-28
4
753 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now