Use vbs to determine which account the script is running

Source: Internet
Author: User

Q:
Hello, script expert! How to determine which account is the script running?
-- KW
A:
Hello, KW. You know, it has been a while since we opened this column with a variety of tokens, and it is not easy for us: after all, searching for tokens is a masterpiece of our script experts. With this in mind, let's start with our favorite remark: the script we will introduce to you is only valid on Windows XP and Windows Server 2003. We will introduce you to methods that make the script equally effective on Windows 2000, but the latter is definitely better than the former.
Oh, yes: Now I think this method is good.
Okay, no excuse me (at least now ). Let's discuss the script. The script is as follows: Copy codeThe Code is as follows: strComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colProcessList = obw.miservice. ExecQuery ("Select * from Win32_Process Where "&_
"Name = 'cscript.exe 'or Name = 'wscript.exe '")
For Each objProcess in colProcessList
If InStr (objProcess. CommandLine, "test. vbs") Then
ColProperties = objProcess. GetOwner (strNameOfUser, strUserDomain)
Wscript. Echo "This script is running under the account belonging "_
& StrUserDomain & "\" & strNameOfUser &"."
End If
Next

As you can see, although this script can be easily run on a remote computer, we connect to the WMI Service on the local computer first. (Yes, we did say this many times. However, this is not an excuse, but a fact: almost all WMI scripts run on remote computers as well as on local computers. We often talk about some substantive content !)
Next we met the following line of code:Copy codeThe Code is as follows: Set colProcessList = obw.miservice. ExecQuery ("Select * from Win32_Process Where "&_
"Name = 'cscript.exe 'or Name = 'wscript.exe '")

You may have guessed that we need to use the Win32_Process class to execute our tasks, because Win32_Process is the WMI class used to track all processes currently running on the computer. Of course, we do not care about all processes running on the computer, but about scripts. Because of this, we have added a Where clause, which will only return information about the instances of the following two Windows Script hosts: Cscript.exe and Wscript.exe.
Note: Yes, we can write this script in a slightly different way, maybe it will save one or two lines of code in the process. We chose this method because it is more similar to running this task on Windows 2000.
After the query is sent, we create a For Each loop to traverse the returned set. In this example, we try to determine the owner of the script named Test. vbs. Therefore, we need to check each script to see if its name is Test. vbs. How can we do this? Use the following line of code:
If InStr (objProcess. CommandLine, "test. vbs") Then
What we need to do here is to use the InStr function to determine whether the test. vbs string can be found somewhere in the CommandLine attribute. What is the CommandLine attribute? Simply put, it is the command string required to start the script from the command prompt. For example, CommandLine may contain the following content:
C: \ Scripts \ Test. vbs
We assume that there is no script named MyTest. vbs, so we will check test. vbs. If you are concerned about this type of name conflict, we can only use InStr and test strings similar to \ test. vbs. This is a question you must decide.
If we can find our target string in the CommandLine value, we will call the GetOwner method to find out the "owner" of the process (that is, the name of the account under which the script runs ):
ObjProcess. GetOwner (strNameOfUser, strUserDomain)
We need to use GetOwner to pass a pair of "Output Parameters ". The output parameter is the variable that the method will fill with a value (We will name the variable ourselves ). Here, we will pass the variables named strNameOfUser and strUserDomain. In turn, GetOwner assigns the user name and the domain of the user who owns the process to these two variables.
In this case, all we need to do is to echo the returned information:
Wscript. Echo "This script is running under the account belonging "_
& StrUserDomain & "\" & strNameOfUser &"."
So, why cannot we run this script on Windows 2000? In fact, there are good reasons to explain this: only the CommandLine attribute is available on Windows XP and Windows Server 2003. In other versions of Windows, we cannot identify each script. The best way is to return the owner information for all instances of Cscript.exe and Wscript.exe that happen to be running. If only one script is running, there is no problem: a single instance of CScript.exe or Wscript.exe must be this single script. In other words, this means that the owner of the Script Host process is also the owner of the script process. If multiple scripts are run, it is another thing. If this is the case, you 'd better say, "Well, Ken Myer owns one of the scripts, even though we don't know which one. A script that he does not own is exactly owned by Pilar Ackerman.
No, not that good. This is the case. (Yes, This is an excuse. Despite a bit of loopholes, it is still a pretext .)
The following is a Windows 2000 solution (or some solutions ):Copy codeThe Code is as follows: strComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colProcessList = obw.miservice. ExecQuery ("Select * from Win32_Process Where "&_
"Name = 'cscript.exe 'or Name = 'wscript.exe '")
For Each objProcess in colProcessList
ObjProcess. GetOwner strNameOfUser, strUserDomain
Wscript. Echo "A script is running under the account belonging "_
& StrUserDomain & "\" & strNameOfUser &"."
Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.