The method of using VBS to write the output content to the screen to cover the content on the current screen

Time:2021-4-20

Q:
Hello, script expert! How to write the output to the screen to cover the current screen?
— KM

A:
Hello, km. If you really need to write the output to the command window, we can’t give you an answer: Although we have dealt with some problems, we have never found a simple and direct way to cover the information in the command window.
However, if you want to export information to the Internet Explorer window, we do have a solution. And the answer is right away:
Set objExplorer = CreateObject(“InternetExplorer.Application”)

objExplorer.Navigate “about:blank”   

objExplorer.ToolBar = 0

objExplorer.StatusBar = 0

objExplorer.Width = 400

objExplorer.Height = 200 

objExplorer.Left = 0

objExplorer.Top = 0

Do While (objExplorer.Busy)

    Wscript.Sleep 200

Loop    

objExplorer.Document.Title = “Process Information”   

objExplorer.Visible = 1  

objExplorer.Document.Body.InnerHTML = “Retrieving process information.” 

Wscript.Sleep 2000

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery(“Select * from Win32_Process”)

For Each objItem in colItems

    objExplorer.Document.Body.InnerHTML = objItem.Name

    Wscript.Sleep 500

Next

objExplorer.Document.Body.InnerHTML = “Process information retrieved.”

Wscript.Sleep 3000

objExplorer.Quit

It does look a little long, but it’s very simple, as you’ll soon find out. For example, at first glance, this code can be terrifying:
Set objExplorer = CreateObject(“InternetExplorer.Application”)

objExplorer.Navigate “about:blank”   

objExplorer.ToolBar = 0

objExplorer.StatusBar = 0

objExplorer.Width = 400

objExplorer.Height = 200 

objExplorer.Left = 0

objExplorer.Top = 0

However, it turns out that all we did was create an empty instance of “Internet Explorer”; moreover, this was done through the first two lines of code. The rest of the code is just to configure the properties of the Internet Explorer window: we set the width to 400 pixels, hide the toolbar, and then position the Internet Explorer window in the upper left corner of the screen. If you want to use the default configuration of the Internet Explorer window, you can skip lines 3 to 8.
After setting the “Internet Explorer” window, we use the “do while” loop below to abort the script after “Internet Explorer” is fully loaded
Do While (objExplorer.Busy)

    Wscript.Sleep 200

Loop

Did you see? Not bad. After starting and running Internet Explorer, specify a title for the Internet Explorer window and set the visible property to true (1). The reason we do this is that, so far, Internet Explorer is still running in hidden windows and not visible on the screen. However, the following code can solve this small problem:
objExplorer.Document.Title = “Process Information”   

objExplorer.Visible = 1

Now we’re going to write something in the window. For this sample script, it’s as simple as the following code:
objExplorer.Document.Body.InnerHTML = “Retrieving process information.”

As you can see, we’re just giving   Document . Body   Object   InnerHTML   Property. In this case, we’re just   InnerHTML   Give some text: retrieve process information. However, we can easily add some   HTML   Tag and create more unique output. For example, this line of code will bold the text written to the window:
objExplorer.Document.Body.InnerHTML = “<B>Retrieving process information.</B>”

Well, maybe that’s not exactly what it’s supposed to be. But you’ve got the answer.
stay   Internet   Explorer   After our text string is displayed in the window, we can abort the script   two   Second (2000 seconds)   MS). There’s no reason you have to do this, just so that you can see the original text before you cover it.  
Next is the following code:
strComputer = “.”

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

Set colItems = objWMIService.ExecQuery(“Select * from Win32_Process”)

For Each objItem in colItems

    objExplorer.Document.Body.InnerHTML = objItem.Name

    Wscript.Sleep 500

Next

We just need to retrieve the collection of processes running on the local computer. This is not very important; we just need to run the script and get some interesting results. What we are concerned about today is what will happen in the “for each” loop that we set up to traverse all the items in the collection
For Each objItem in colItems

    objExplorer.Document.Body.InnerHTML = objItem.Name

    Wscript.Sleep 500

Next

In most ﹣ WMI ﹣ scripts, we echo property values (for example, name) within the ﹣ for each ﹣ loop. However, we do not use it here Wscript.Echo Instead, assign the name of the first process in the collection to the “innerHTML” attribute of the script body:
objExplorer.Document.Body.InnerHTML = objItem.Name

What is the result of this? The name of the first process in the collection will be used to override the existing content of the Internet Explorer window. For example, when we want to start the loop, the Internet Explorer window will contain the following text:
Retrieve process information.
When we start the loop, the text is replaced with the name of the first process in the collection. For example:
Winword.exe。

After covering the contents of the window, we abort the script for half a second (500 milliseconds). Again, this is not a must; we do it just to slow down the script so that you can see the name of the first process displayed. After half a second, enter the next loop and retrieve information about the second process in the collection. After that, the script will replace the existing content (technically, the value assigned to the innerHTML attribute) in the Internet Explorer window with the name of the second process, and so on.
After traversing the entire collection, we indicate that the script has ended, abort for 3 seconds, and then close the Internet Explorer window:
objExplorer.Document.Body.InnerHTML = “Process information retrieved.”

Wscript.Sleep 3000

objExplorer.Quit

The operation effect is very good, and it is very simple. Because it uses HTML, it means that you can make the output as much as you want; in fact, you can even make the Internet Explorer window look like a command window. If that’s not cool enough, then we don’t know how to be cool. (in fact, as a script expert’s son often reminds his script expert’s father, we don’t know what cool is.)