Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vbs to determine under which account the script is running

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use vbs to determine under which account the script is running", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use vbs to determine which account the script is running under" this article.

Q:

How do I determine under which account the script is running?

A:

You know, it's been a while since we started this column with a variety of excuses, and it's not easy for us: after all, finding excuses is what we scripting experts are good at. With this clear, let's start with one of our favorite excuses: the script we'll introduce you to is only valid on Windows XP and Windows Server 2003. We'll show you how to make this script equally effective on Windows 2000, but the latter is definitely not as good as the former.

Oh, yes: it feels good now.

Well, no more excuses (at least for now). Let's talk about the script. The script is as follows:

The code is as follows:

StrComputer = "."

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\ root\ cimv2")

Set colProcessList = objWMIService.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 to" _

& strUserDomain & "\" & strNameOfUser & ".

End If

Next

As you can see, although it is also easy to run this script against a remote computer, we first connect to the WMI service on the local computer. Yes, we have said that many times. But this is not an excuse, it's just a statement of the fact that almost all WMI scripts work as well on remote computers as they do on local computers. We do talk about substance from time to time! )

Then we come across the following line of code:

The code is as follows:

Set colProcessList = objWMIService.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 perform our tasks because Win32_Process is the WMI class used to track all processes currently running on the computer. Of course, we don't care about all the processes running on the computer, we only care about scripts. For this reason, we have added a Where clause that will only return information about the instances hosted by the following two Windows scripts: Cscript.exe and Wscript.exe.

Note: yes, we could have written this script in a slightly different way, perhaps saving a line or two of code in the process. We chose this method because it is more similar to the way we performed this task on Windows 2000.

After issuing the query, we set up a For Each loop to iterate through the returned collection. In this case, we are trying 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 do we do that? By using this line of code:

If InStr (objProcess.CommandLine, "test.vbs") Then

What we are going to do here is to use the InStr function to determine whether the string test.vbs can be found somewhere in the property CommandLine. What is the CommandLine attribute? Simply put, it is the command string required to start the script from the command prompt. For example, CommandLine might be the following:

C:\ Scripts\ Test Scripts\ Test.vbs

Since we assume that there is no script named MyTest.vbs, we will examine test.vbs. If you are worried about such name conflicts, we can just use InStr and test against strings like\ test.vbs. This is a question that you must decide.

If we do 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 pass a pair of "output parameters" using GetOwner. The output parameter is the variable that the method will populate with a value (we name the variable ourselves). Here, we will pass 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.

At this point, all we have to do is echo the return message:

Wscript.Echo "This script is running under the account belonging to" _

& strUserDomain & "\" & strNameOfUser & ".

So why can't we run this script on Windows 2000? In fact, there is good reason to explain this: the CommandLine attribute is available only on Windows XP and Windows Server 2003. On other versions of Windows, we cannot identify individual scripts; the best way is to return owner information for all instances of Cscript.exe and Wscript.exe that happen to be running. If there is only one script running, there is no problem either: a single instance of CScript.exe or Wscript.exe must be that single script. In other words, this means that the owner of the script host process is also the owner of the script process. If you run multiple scripts, it's a different story. If that's the case, you'd better say, "well, Ken Myer has one of the scripts, although we don't know which one. One of the scripts he doesn't own happens to be owned by Pilar Ackerman."

No, it's not that good. But that's the way it is. Yes, this is an alibi. Although full of loopholes, it is still an alibi. )

Here is the Windows 2000 solution (or part of the solution):

The code is as follows:

StrComputer = "."

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\ root\ cimv2")

Set colProcessList = objWMIService.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 to" _

& strUserDomain & "\" & strNameOfUser & ".

Next

The above is all the contents of the article "how to use vbs to determine under which account the script is running". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report