Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript to run as different user

Posted on 2011-02-28
4
Medium Priority
?
1,270 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

972 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