Solved

VBScript to run as different user

Posted on 2011-02-28
4
1,021 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
[X]
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
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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