This ensures that Windows will recognize the language that your script has been written and handled correctly.
Windows provides a tool called WMI, or Windows Management Instrumentation, that provides scripts with an interface to access operating system components. You can run queries with WMI to get current information about the system. Microsoft provides a complete list of all types of queries that can be performed with the system.
Although VBA can be used to get computer information into Excel, you can do the same by using a simple VB script.
In this example, we will query the system to get processor information (model, manufacturer, core number), battery information (description and status), logical disk information (name, content). remaining amount, and overall size). Then we will export all this information to a CSV file for easy viewing.
The first step is to set up the FileSystemObject to use to export the CSV file and create the file:
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile1 = "MyComputerInfo.csv"
Set oFile1 = oFSO.CreateTextFile(sFile1, 1)
The next step is to set up WMI queries and execute it:
strQuery = "SELECT Family,Manufacturer,NumberOfCores FROM Win32_Processor" Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
Finally, sort the results and include the information in the CSV file.
oFile1.WriteLine "Processor Information"
oFile1.WriteLine "------"
For Each objResult In colResults
strResults = "Family:,"+CStr(objResult.Family)
oFile1.WriteLine strResults
strResults = "Manufacturer:,"+CStr(objResult.Manufacturer)
oFile1.WriteLine strResults
strResults = "Number of Cores:,"+CStr(objResult.NumberOfCores)
oFile1.WriteLine strResults
Next
If you run the above code you will have the output like the image below:
For the next two parts of the code, simply repeat and change the query to find additional information. This is querying battery information:
oFile1.WriteLine ""
strQuery = "SELECT Description,Status FROM Win32_Battery"
Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
oFile1.WriteLine "Battery Information"
oFile1.WriteLine "------"
For Each objResult In colResults
strResults = "Status:,"+CStr(objResult.Description)
oFile1.WriteLine strResults
strResults = "Description:,"+CStr(objResult.Status)
oFile1.WriteLine strResults
Next
And this is the next part of the logical disk query:
oFile1.WriteLine ""
strQuery = "Select Name, FreeSpace, Size from Win32_LogicalDisk"
Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
oFile1.WriteLine "Disk Information"
oFile1.WriteLine "------"
'Identify the Logical Disk Space
For Each objResult In colResults
strResults = "Name:,"+CStr(objResult.Name)
oFile1.WriteLine strResults
strResults = "Free Space:,"+CStr(objResult.FreeSpace)
oFile1.WriteLine strResults
strResults = "Disk Size:,"+CStr(objResult.Size)
oFile1.WriteLine strResults
Next
Finally, remember to close the code by closing the file and setting the objects to "Nothing":
oFile1.Close
Set oFile1 = Nothing
set colResults = Nothing
strResults = ""
Put all the code in the new .WSF file, then run and this is the result:
Just change these queries with other computer devices or software that want to receive information and you can get full system reports at any time with just one click.
Sometimes specific services will have problems and require a quick restart to be able to run normally. If you want to quickly turn off and restart the service, you just need to save the following script. It will prompt the user to type the name of the service you want to reboot and the rest of it will do.
Since stopping and starting services requires special administrative rights, you need to place the following code when starting the script to grant advanced scripts to the script.
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If
Once done, add the rest of the code to run the input box for user input, launch the command window and send "net stop" and "net start commands":
Set cmdShell = CreateObject("WScript.Shell")
strServiceName=Inputbox("Inter Service to Stop","Input Required")
cmdShell.Run "cmd.exe"
WScript.Sleep 1000
cmdShell.SendKeys "net stop "+strServiceName
cmdShell.SendKeys "{Enter}"
WScript.Sleep 1000
cmdShell.SendKeys "net start "+strServiceName
cmdShell.SendKeys "{Enter}"
WScript.Sleep 1000
cmdShell.SendKeys "Exit"
cmdShell.SendKeys "{Enter}"
That's it, you don't need to find service tools, just run scipt to stop and start any service in seconds.
With the script below, we can do a couple of things. This script will show you how to edit the registry with a VB script. It will also provide a script that allows you to set the default Windows username and password by editing those registry settings.
This is how it works. First, because editing the registry requires admin rights, you need to set up advanced permissions for the script:
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If
First run two input boxes to request the username and password:
strUserName=Inputbox("Enter the default User Name","Input Required")
strPassword=Inputbox("Enter the default Password","Input Required")
Next, set the object in the shell and write those values into the appropriate registry keys:
Set wshShell = CreateObject( "WScript.Shell" )
wshShell.RegWrite "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogonDefaultUserName", strUserName, "REG_SZ"
wshShell.RegWrite "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogonDefaultPassword", strPassword, "REG_SZ"
Set wshShell = Nothing
The "RegWrite" method allows users to write any value for any registry key using a VB Windows script. All you need to know is the appropriate path.
Run the script and answer the messages that appear.
The input values will be inserted immediately into the registry settings you have set up in the script.
Resetting the network connection using VB script will reset all active connections of the user to resolve any network problems that are encountered.
As with other scripts, this script also needs administrative privileges, you need to add the code in the script above to the top for advanced permissions. Next, create the WMI object and query it with a list of network adapters enabled on the system:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")
Finally, go through all the enabled adapters and reset them:
For Each objItem in colItems
objItem.Disable
WScript.Sleep 1000
objItem.Enable
Next
This will reset all active network adapters, which is often the fastest way to resolve annoying network problems. Save this script and use it whenever you see slow networks and other unusual network problems.
This script is used to check if the website is working, then it will send the email to the user if the site has a problem. You can use this same script to monitor important servers or computers on the network and set up emailing you whenever the script can't ping the device.
First, set the script for the target to ping, create the shell object and then run the ping command.
strTarget = "topsecretwriters.com"
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & strTarget, 0, True)
Use a Select Case statement to run through the Ping results and respond accordingly. If the results return to zero, then the site (or server) is online and you do not have to do anything. If it returns "1" then the ping fails and you need to do something.
Select Case Ping
Case 0
Case 1
Set objMessage = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.Configuration")
objConfig.Load -1
Set Flds = objConfig.Fields
With Flds
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="xxxxxx@gmail.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="xxxxxxxxxxxxxxxxx"
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465
.Update
End With
With objMessage
Set .Configuration = objConfig
.Subject = "Your site is offline"
.From = "me@mycomputer.com"
.To = "xxxxxx@gmail.com"
.TextBody = "Hey, your website is offline."
.Send
End With
End Select
When the script runs and cannot ping the device or website, you will receive an email.
This way you can quickly and easily check your network.
These are just a few examples of some interesting things you can do with VB scripts to streamline your computer usage. There are many other things people can do like automatic backup with VB and Synctoy, automate telnet commands, or even open and control application windows.
See more: