how to create a VB script file without a pop up window - vbscript

I googled a code that works just as I wanted,
But when I schedule it in task manager issue occurs ..after every pop up screen i need to click ok..then only the file gets updated.Please let me know what changes are to be done so that after running VBS it silently updates the file.
actual code:
source:http://www.wisesoft.co.uk/scripts/vbscript_disk_space_usage_report.aspx
OPTION EXPLICIT
CONST strComputer = "."
CONST strReport = "D:\diskspace.txt"
DIM objWMIService, objItem, colItems
DIM strDriveType, strDiskSize, txt
SET objWMIService = GETOBJECT("winmgmts:\\" & strComputer & "\root\cimv2")
SET colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)" & vbcrlf
FOR EACH objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & vbtab & pctFreeSpace & vbcrlf
NEXT
writeTextFile txt, strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' Procedure to write output to a text file
PRIVATE SUB writeTextFile(BYVAL txt,BYVAL strTextFilePath)
DIM objFSO,objTextFile
SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
SET objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
SET objTextFile = NOTHING
END SUB

Call the script with cscript script_file.vbs instead of wscript script_file.vbs.

Popup massage genarated by wscript.echo if you delete that line, code will run silently
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt

Related

Parse properties string in an array to For Each loop

I am trying to get information about the battery in my laptop by using VBScript.
It is more properties. I want to parse arrayItems("Name","Availability","BatteryStatus","Chemistry") for each objItem.
For example:
For iii = 0 To UBound(arrayItems)
WScript.Echo "Result of iii:" & objItem.arrayItems (iii)
Next
I do not want to enter manually as "Availability: " & objItem.Availability, "BatteryStatus: " & objItem.BatteryStatus, ...
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
For Each objItem In colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "Availability: " & objItem.Availability
WScript.Echo "BatteryStatus: " & objItem.BatteryStatus
WScript.Echo "Chemistry: " & objItem.Chemistry
WScript.Echo "DesignVoltage: " & objItem.DesignVoltage
WScript.Echo "EstimatedChargeRemaining: " & objItem.EstimatedChargeRemaining
WScript.Echo "Status: " & objItem.Status
Next
What you want can be done via the object's properties_ property set:
arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")
For Each objItem In colItems
For Each name In arrayItems
WScript.Echo name & ": " & objItem.properties_(name)
Next
Next

Combine 2 VBScripts using the same file

Pretty new to scripting but i managed to put something together where i retrieve free disk space - formatted how i want. then the file is imported to SQL.
the error i am getting is that the output file is being used by the connection. how can i get this to run under one script without errors.. all help is appreciated.
`CONST strComputer = "."
AuditPath = "C:\Users\Pam\Desktop\Khalid.txt"
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=khalid;Data Source=GRIMLEY"
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(AuditPath,True)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Volume")
Set objConn = CreateObject ("ADODB.Connection")
d = date()
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=khalid;Data Source=GRIMLEY"
DIM objWMIService, objItem, colItems
DIM AuditPath, txt , strSQL , ConnectionString
txt = "Server" & vbtab & "DriveLetter" & vbtab & "DriveName" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "% Free" & vbtab & "Date" & vbcrlf
Server = "ALL_IN_ONE_Final"
For Each objItem In colItems
ObjFile.Write Server & ","
objFile.write objItem.DriveLetter & ","
ObjFile.Write objItem.Label & ","
objFile.write Int(objItem.Capacity /1073741824)\1 & ","
objFile.write Int((objItem.Capacity - objItem.FreeSpace )/1073741824)\1 & ","
objFile.write Int (objItem.FreeSpace /1073741824)\1 & ","
objFile.write ((objItem.FreeSpace/objItem.Capacity) * 100)\1 & "," & vbcrlf
next
WScript.Sleep 8000
Set objConn = CreateObject ("ADODB.Connection")
objConn.Open ConnectionString
strSQL = strSQL & " BULK INSERT Server_Space"
strSQL = strSQL & " FROM 'C:\Users\Pam\Desktop\Khalid.txt' with"
strSQL = strSQL & " ( FIELDTERMINATOR =',', ROWTERMINATOR = '0x0a')"
objConn.Execute strSQL
objConn.Close
WScript.Quit()`

VBscript for use with multiple IP addresses

I'm trying to write a script in vbscript but being a near noob and online tutorials didn't work, I had to resort to posting here asking for help.
The script that I've mixed and match from different sources displays domain, user, computer name, ip address. The script is working. However in certain environment, a user could potentially have multiple IP addresses and when displaying in MsgBox, only the last IP address result is returned and in many cases, that's wrong.
I would like to know how I add/can store the address in an array and have MsgBox display the other IP addresses if there was more than one result.
Thank you.
Script attached below:
Option Explicit
DIM WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
Msgbox message, , title
In your code MsgBox will show the first address of the network adapter last enumerated. If you want to show all IP addresses, change this:
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
into this:
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
ReDim arrIP(-1)
For Each objItem In colItems
For Each addr In objItem.IPAddress
ReDim Preserve arrIP(UBound(arrIP)+1)
arrIP(UBound(arrIP)) = addr
Next
Next
title = "Who Am I?"
message = "Domain:" & vbTab & vbTab & WshNetwork.UserDomain & vbNewLine & _
"User Name:" & vbTab & UCase(WshNetwork.UserName) & vbNewLine & _
"Computer Name:" & vbTab & WshNetwork.ComputerName & vbNewLine & _
"IP Address1:" & vbTab & Join(arrIP, ", ")
The following was tested in Windows 8; works flawlessly!
Option Explicit
DIM objHTTP, WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3), addr
Set objHTTP = WScript.CreateObject("MSXML2.ServerXmlHttp")
objHTTP.Open "GET", "http://icanhazip.com", False
objHTTP.Send
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
ReDim arrIP(-1)
For Each objItem In colItems
For Each addr In objItem.IPAddress
ReDim Preserve arrIP(UBound(arrIP)+1)
arrIP(UBound(arrIP)) = addr
Next
Next
title = "Who Am I?"
message = "Domain:" & vbTab & vbTab & WshNetwork.UserDomain & vbNewLine & _
"User Name:" & vbTab & UCase(WshNetwork.UserName) & vbNewLine & _
"Computer Name:" & vbTab & WshNetwork.ComputerName & vbNewLine & _
"Public IP Address: " & vbTab & objHTTP.ResponseText & vbNewLine & _
"Network IPs v4 & v6: " & vbNewLine & vbTab & vbTab & Join(arrIP, ", " & vbNewLine & vbTab & vbTab) & "."
Msgbox message, , title
Set objHTTP = Nothing</code>
Option Explicit
DIM WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
' Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
Msgbox message, , title

Adding a summary report and disk information to VB Script

I was hoping someone could help me with this code. I wanted to add two things to this script but can't seem to get it working at all.
The script works fine but what isn't working is trying to add the disk information and trying to create a summary report for total size of disk.
at the end of it I'm trying to make an output of what
wmic diskdrive list brief /format:list
would give you.
something like this:
Caption=WDC WD2500BEKT-75PVMT1
DeviceID=\\.\PHYSICALDRIVE0
Model=WDC WD2500BEKT-75PVMT1
Partitions=1
Size=250056737280
Here is the script so far
Option Explicit
const strComputer = "."
const strReport = "c:\path\to\file"
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)" & vbcrlf
For Each objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & vbtab & pctFreeSpace & vbcrlf
Next
writeTextFile txt, strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' Procedure to write output to a text file
private sub writeTextFile(byval txt,byval strTextFilePath)
Dim objFSO,objTextFile
set objFSO = createobject("Scripting.FileSystemObject")
set objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
SET objTextFile = nothing
end sub
The wmic call and your script do entirely different things. The former queries the Win32_DiskDrive class to gather information about the physical disks, whereas the latter queries the Win32_LogicalDisk class to gather information about the volumes.
You can reproduce the output of the wmic command with something like this:
Set wmi = GetObject("winmgmts://./root/cimv2")
For Each disk In wmi.ExecQuery("SELECT * FROM Win32_DiskDrive")
WScript.Echo "Caption=" & disk.Caption & vbNewLine _
& "DeviceID=" & disk.DeviceID & vbNewLine _
& "Model=" & disk.Model & vbNewLine _
& "Partitions=" & disk.Partitions & vbNewLine _
& "Size=" & disk.Size
Next
However, the size returned by this query is the raw capacity of the physical disk. At this level you can't distinguish if a block is "free" or "used". Those are concepts that apply to filesystems. On that level, a sector either does or doesn't contain one or more files or file fragments. It's "free" when it doesn't contain any file and "used" otherwise. On that level, you don't get any information about "partitions", though, because those exist on a lower level (partitions contain filesystems).
What you need to do is decide which information you actually want to report, and then choose the appropriate properties from the relevant WMI class(es).

How to make the columns in VBscript fixed

I'm a beginner in VBscript and I got a script which obtains disk space usage of local drives. However, when some columns would contain long numeric value, some adjacent columns and even values are moving to the right and thus makes the output disorganized. I already
Please see below the contents of the script:
Option Explicit
const strComputer = "."
const strReport = "F:\dba_scripts\diskspace.txt"
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = "DRIVE" & vbtab & vbtab & "SIZE" & vbtab & vbtab & "USED" & vbtab & vbtab & "FREE" & vbtab & vbtab & "FREE(%)" & vbcrlf
For Each objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = round((objItem.Size /1073741824),1) & " GB"
strFreeSpace = round((objItem.FreeSpace /1073741824),1) & " GB"
strUsedSpace = round(((objItem.Size-objItem.FreeSpace)/1073741824),1) & " GB"
txt = txt & objItem.Name & vbtab & vbtab & strDiskSize & vbtab & vbtab & strUsedSpace & vbTab & vbtab & strFreeSpace & vbtab & vbtab & pctFreeSpace & vbcrlf
Next
writeTextFile txt,strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' Procedure to write output to a text file
private sub writeTextFile(byval txt,byval strTextFilePath)
Dim objFSO,objTextFile
set objFSO = createobject("Scripting.FileSystemObject")
set objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
SET objTextFile = nothing
end sub
The output file looks OK but when I send/email it using the free bmail, the results are disorganized (meaning some columns and values moved to the right.
My question is are there ways to make the columns and values results fixed ( meaning no columns and values are moving to the right )?
Function RightJustified(ColumnValue, ColumnWidth)
RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue
End Function
Usage example:
output = output & _
RightJustified(strDiskSize, 15) & _
RightJustified(strUsedSpace, 15) & _
RightJustified(strFreeSpace, 15) & _
RightJustified(pctFreeSpace, 15) & _
vbCrLf
EDIT
Add the RightJustified function to your script.
Then, replace this line of your code:
txt = txt & objItem.Name & vbtab & vbtab & strDiskSize & vbtab & vbtab & strUsedSpace & vbTab & vbtab & strFreeSpace & vbtab & vbtab & pctFreeSpace & vbcrlf
with:
txt = txt & objItem.Name & _
RightJustified(strDiskSize, 15) & _
RightJustified(strUsedSpace, 15) & _
RightJustified(strFreeSpace, 15) & _
RightJustified(pctFreeSpace, 15) & _
vbCrLf
EDIT 2
I added the RightJustified function at the bottom of your script, and then called it within your loop to format the columns. I also used it on the column headers. Below is the script and at the bottom is the output on my machine.
Option Explicit
const strComputer = "."
const strReport = "F:\dba_scripts\diskspace.txt"
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = RightJustified("DRIVE", 10) & _
RightJustified("SIZE", 15) & _
RightJustified("USED", 15) & _
RightJustified("FREE", 15) & _
RightJustified("FREE(%)", 15) & _
vbCrLf
For Each objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = round((objItem.Size /1073741824),1) & " GB"
strFreeSpace = round((objItem.FreeSpace /1073741824),1) & " GB"
strUsedSpace = round(((objItem.Size-objItem.FreeSpace)/1073741824),1) & " GB"
txt = txt & _
RightJustified(objItem.Name, 10) & _
RightJustified(strDiskSize, 15) & _
RightJustified(strUsedSpace, 15) & _
RightJustified(strFreeSpace, 15) & _
RightJustified(pctFreeSpace, 15) & _
vbCrLf
Next
writeTextFile txt,strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' Procedure to write output to a text file
Sub writeTextFile(byval txt,byval strTextFilePath)
Dim objFSO,objTextFile
set objFSO = createobject("Scripting.FileSystemObject")
set objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
Set objTextFile = nothing
End Sub
Function RightJustified(ColumnValue, ColumnWidth)
RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue
End Function
Output produced:
DRIVE SIZE USED FREE FREE(%)
C: 48.4 GB 40.6 GB 7.8 GB 16.1
D: 100.6 GB 56.8 GB 43.8 GB 43.5
You could write out a table using HTML. This should work in an email.

Resources