Solved

VBScript to run as different user

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specific format 21 182
Using VB6 to write Excel Spreadsheets - 5 40
How can I save all open docs into a given folder 12 86
Randomize in Owl Carousel v1.3.2 6 20
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

13 Experts available now in Live!

Get 1:1 Help Now