Basic VBS help - optimizing VBS script - vbscript

I found this simple script that outputs the logical disk sizes.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size
Next
My VBS skills are very poor and I need help:
I would like to get a single size number of ONLY the C and D partitions added together
if the size (from step1) is not equal to 500-GB (between 450,000,000,000 and 550,000,000,000) I need the computer to prompt a warning and "press any key" to continue
I don't want a pop-up window since this is going to run from the prompt of WinPE, is it possible to get the output in the prompt window?
I'm asking a lot so thank you in advance if you can help

You will need to start your script using cscript.
The code for this comes from http://ask.metafilter.com/79481/vbscript-printing-to-command-line
This allows the echos to go to the command line instead of a Message box.
CheckStartMode
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
If(objDisk.DeviceID="C:" or objDisk.DeviceID="D:") then
Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size
TotalSize = CCur(TotalSize) + CCur(objDisk.Size)
End if
Next
If(TotalSize <450000000000 or TotalSize >550000000000) then
Wscript.Echo "Disk size of " & TotalSize & " is out of range."
Wscript.Echo "Press enter to contine."
z = WScript.StdIn.Read(1)
End if
Wscript.Echo "Complete, Press enter to end."
z = WScript.StdIn.Read(1)
Sub CheckStartMode
' Returns the running executable as upper case from the last \ symbol
strStartExe = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )
If Not strStartExe = "CSCRIPT.EXE" Then
' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
' wscript.scriptfullname is the full path to the actual script
set oSh = CreateObject("wscript.shell")
oSh.Run "cscript.exe """ & wscript.scriptfullname & """"
wscript.quit
End If
End Sub

Related

Call recently added script from another script

As soon as the file is added to script folder it is detected by this code.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
I want to execute VBScript as soon as it is added to script folder from this VBScript. How to do that? Getting the name of the file that is added to script folder and then executing that VBScript.
Replace this line of code with the code you want executed any time a new file is detected: Wscript.Echo objLatestEvent.TargetInstance.PartComponent. For instance, next code snippet shows a possible approach (and that's why there is wide Echo output, wider than necessary...):
''''(unchanged code above)
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
''''Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Call DoWithName( objLatestEvent.TargetInstance.PartComponent)
Loop
Sub DoWithName( strPartComp)
Dim arrFileName
arrFileName = Split( strPartComp, """")
If True Or UBound(arrFileName) > 0 Then
Wscript.Echo strPartComp _
& vbNewLine & UBound( arrFileName) _
& vbNewLine & "[" & arrFileName( 0) & "]" _
& vbNewLine & "[" & arrFileName( 1) & "]" _
& vbNewLine & "[" & arrFileName( 2) & "]" _
& vbNewLine & ShowAbsolutePath( arrFileName( 1))
End If
End Sub
Function ShowAbsolutePath( strPath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
ShowAbsolutePath = fso.GetAbsolutePathName( strPath)
End Function
Note that
the ShowAbsolutePath( arrFileName( 1)) returns the name of the file that is added to script folder; now you could
check whether it's a valid .vbs file name, and if so, launch it combining any Windows Script Host engine (wscript.exe or cscript.exe) in either
Run Method or
Exec Method.
You can try this modified script :
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""d:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Call DoWithName(objLatestEvent.TargetInstance.PartComponent)
Loop
End if
' --------------------------------------
Sub DoWithName( strPartComp)
Dim Title,arrFileName,Question,ws
Title = "Execute vbscript"
set ws = CreateObject("wscript.shell")
arrFileName = Split( strPartComp, """")
If True Or UBound(arrFileName) > 0 Then
Wscript.Echo strPartComp _
& vbNewLine & UBound( arrFileName) _
& vbNewLine & "[" & arrFileName( 0) & "]" _
& vbNewLine & "[" & arrFileName( 1) & "]" _
& vbNewLine & "[" & arrFileName( 2) & "]" _
& vbNewLine & DblQuote(ShowAbsolutePath(arrFileName(1)))
End If
Question = MsgBox("Did you want to execute this vbscript : " & DblQuote(ShowAbsolutePath(arrFileName(1))),vbYesNo+vbQuestion,Title)
If Question = vbYes Then
ws.run DblQuote(ShowAbsolutePath(arrFileName(1)))
Else
End if
End Sub
' --------------------------------------
Function ShowAbsolutePath( strPath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
ShowAbsolutePath = fso.GetAbsolutePathName( strPath)
End Function
' --------------------------------------
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
' --------------------------------------
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
' --------------------------------------
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
' --------------------------------------

VBScript to start multiple services

I need to start multiple services using a VBScript.
I wrote this script to start the services and when I run it, it doesn't give any errors and it doesn't start the services. Any Ideas where the problem might be ?
sComputer = "."
aTargetSvcs= Array ("ServiceOne" &_
"ServiceTwo" &_
"ServiceThree" &_
"ServiceFour")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State = "Stopped" Then
oService.StartService()
End If
End If
Next
Next
Your aTargetSvcs is not what you think it is - an array containing four strings - but:
Option Explicit
Dim aTargetSvcs : aTargetSvcs = Array ("ServiceOne" &_
"ServiceTwo" &_
"ServiceThree" &_
"ServiceFour")
WScript.Echo UBound(aTargetSvcs)
WScript.Echo aTargetSvcs(UBound(aTargetSvcs))
output:
cscript 21036510.vbs
0
ServiceOneServiceTwoServiceThreeServiceFour
I finally got it to work. I had to use a comma-delimited. Here the change and it works:
However, how can I make each service on its own line to be easy because I have lots of services that I start up. If there is a way to make each service on its own, it would be much easier to read and manage.
sComputer = "."
aTargetSvcs= Array ("ServiceOne","ServiceTwo","ServiceThree")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State <> "Stopped" Then
oService.StartService()
End If
End if
Next
Next

Using WMI objects in VB script to decide which programs to run

I have a vbscript that will find the OS, Service pack and OS Architecture but when i try to use the object in a If then statement its not defined, I tried to declare it but that didn't work. What i am trying to complete is create a script that will define the OS, Service pack and whether the system is x86 or x64 and the execute a program that will install the updates specific for that system but I'm stuck.
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo "Version: " & objOperatingSystem.Version
Wscript.Echo "Service Pack: " & objOperatingSystem.CSDVersion
Wscript.Echo "CPU Bit: " & objOperatingSystem.OSArchitecture
Next
I am assuming you are not storing those variables you are echoing with "wscript.echo"..
You could use this example (or make a case statement):
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
osVersion = objOperatingSystem.Version
osCSDVersion = objOperatingSystem.CSDVersion
osArchitecture = objOperatingSystem.OSArchitecture
Next
Wscript.Echo "Version: " & osVersion
Wscript.Echo "Service Pack: " & osCSDVersion
Wscript.Echo "CPU Bit: " & osArchitecture
If InStr(LCase(osArchitecture), "64-bit") Then
wscript.echo "64-bit"
' .... Do stuff here ....
Else
wscript.echo "not 64-bit"
' .... Do stuff here ....
End If

VBS Code for Program Terminator

I would like a VBS code that:
Displays an input box to enter a process name
and can append the process to the code written below:
Option Explicit
Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess
strComputer = "."
strProcessToKill = "notepad.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer _
& "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")
For Each objProcess in colProcess
msgbox "... terminating " & objProcess.Name
objProcess.Terminate()
Next
Replace the line
strProcessToKill = "notepad.exe"
with
strProcessToKill = InputBox("Enter process name:")
You should add some safety checks, though, in case the user presses Cancel or presses OK without having anything entered:
If IsEmpty(strProcessToKill) Then
WScript.Echo "User pressed 'Cancel'."
WScript.Quit 1
ElseIf strProcessToKill = "" Then
WScript.Echo "Nothing entered."
WScript.Quit 1
End If

Getting Script Name from wscript.exe Process

I am using this code:
Dim name
name = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%computername%")
Set wmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& name & "\root\cimv2")
For Each hwnd In wmi.InstancesOf("Win32_Process")
If hwnd.Name = "wscript.exe" Then
'get name and possibly location of currently running script
End If
Next
I am successfully listing all processes and picking out the wscript.exe. However, I have searched and have found no way to find the name of the script running in wscript.exe, ie. is it myscript.vbs or jscript.js or anything. Bonus if there is a way to find the whole path of the script.
EDIT:
With more searching, I found a solution. In the above script, the hwnd variable stores the handle for the wscript.exe process. There is a property for handles: hwnd.CommandLine. It shows how to call it from the command line, so it would be something like:
"C:\Windows\System32\wscript.exe" "C:\path\to\script.vbs"
So I can parse the hwnd.CommandLine string to find the path and name of all running scripts.
You have ScriptName and ScriptFullName properties.
' in VBScript
WScript.Echo WScript.ScriptName
WScript.Echo WScript.ScriptFullName
// in JScript
WScript.Echo(WScript.ScriptName);
WScript.Echo(WScript.ScriptFullName);
[EDIT] Here you go (use .CommandLine property):
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& "." & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery( _
"Select * from Win32_Process " _
& "Where Name = 'WScript.exe'", , 48)
Dim strReport
For Each objProcess in colProcesses
' skip current script, and display the rest
If InStr (objProcess.CommandLine, WScript.ScriptName) = 0 Then
strReport = strReport & vbNewLine & vbNewLine & _
"ProcessId: " & objProcess.ProcessId & vbNewLine & _
"ParentProcessId: " & objProcess.ParentProcessId & _
vbNewLine & "CommandLine: " & objProcess.CommandLine & _
vbNewLine & "Caption: " & objProcess.Caption & _
vbNewLine & "ExecutablePath: " & objProcess.ExecutablePath
End If
Next
WScript.Echo strReport
myProcess="wscript.exe"
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, myProcess, vbTextCompare) = 0 Then 'check if process exist
CmdLine=process.commandline
End If
Next
myArr=split(CmdLine,"\")
mySN=replace(myArr(ubound(myArr)),"""","")
Wscript.Echo "Full Pth: " & Cmdline &vbcrlf&"Script Name: "& mySN

Resources