0% found this document useful (0 votes)
27 views6 pages

Running VBScript and CMD Commands

The document provides examples of using VBScript to run other scripts and programs, find and replace text, retrieve domain information, check for blank values, get the date and time, and delete old files. Specifically, it shows how to run VBScript, batch, and PowerShell scripts, find and replace text in a file, list computers and users in a domain, define a function to check for blank values, get the date and time on a system along with time zone info, and delete files older than a specified number of days from a folder.

Uploaded by

Sudheer Sunku
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Running VBScript and CMD Commands

The document provides examples of using VBScript to run other scripts and programs, find and replace text, retrieve domain information, check for blank values, get the date and time, and delete old files. Specifically, it shows how to run VBScript, batch, and PowerShell scripts, find and replace text in a file, list computers and users in a domain, define a function to check for blank values, get the date and time on a system along with time zone info, and delete files older than a specified number of days from a folder.

Uploaded by

Sudheer Sunku
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Run a VBScript file To run a VBScript from within another VBScript: Dim objShell Set objShell = [Link]("[Link]") objShell.

Run "cscript c:\batch\[Link]" Run a CMD batch file To run a CMD batch file from VBScript: Dim objShell Set objShell = [Link]("[Link]") [Link] "c:\batch\[Link]" Run a PowerShell script To run a PowerShell script from VBScript: Dim objShell Set objShell = [Link]("[Link]") [Link] "powershell -file ""c:\batch\demo.ps1""" -----------------------------------------------------------------------------------------------------------------------------------------Find and replace a text string in a file. 'usage: cscript [Link] Filename "StringToFind" "stringToReplace" Option Explicit Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent strFilename=[Link](0) strSearch=[Link](1) strReplace=[Link](2) 'Does file exist? Set fso=CreateObject("[Link]") if [Link](strFilename)=false then [Link] "file not found!" [Link] end if 'Read file set objFile=[Link](strFilename,1) oldContent=[Link]

'Write file newContent=replace(oldContent,strSearch,strReplace,1,-1,0) set objFile=[Link](strFilename,2) [Link] newContent [Link] Example cscript //Nologo [Link] c:\docs\[Link] "Madonna" "Lady Gaga" ---------------------------------------------------------------------------------------------------List all the computers in a given domain. ' [Link] On Error Resume Next Set objDomain = GetObject("WinNT://SS64Domain") [Link] "Domain : " + [Link] For each objDomainItem in objDomain if [Link] = "Computer" then [Link] [Link] end if Next -------------------------------------------------------------------------------------------------List all the user accounts in a specific domain. '[Link] On Error Resume Next Set objDomain = GetObject("WinNT://SS64Domain") [Link] "Domain : " + [Link] For each objDomainItem in objDomain if [Link] = "User" then [Link] [Link] + " : Full Name: " + [Link] end if Next ----------------------------------------------------------------------------------------------------------------------------------------

IsBlank function

The IsBlank function below will return True if the variable or value passed to it is Empty or NULL or Zero. It will return False if the variable contains any string or a value other than '0'. Function IsBlank(Value) 'returns True if Empty or NULL or Zero If IsEmpty(Value) or IsNull(Value) Then IsBlank = True Exit Function ElseIf VarType(Value) = vbString Then If Value = "" Then IsBlank = True Exit Function End If ElseIf IsObject(Value) Then If Value Is Nothing Then IsBlank = True Exit Function End If ElseIf IsNumeric(Value) Then If Value = 0 Then [Link] " Zero value found" IsBlank = True Exit Function End If Else IsBlank = False End If End Function Arguably the numeric value '0' is as valid a value as any other number. The logic behind flagging this as blank is that you may have data like a Price or a Surname = 0 which most likely should be treated as being blank. All vbscript variables are variants. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Examples using the function above [Link] "testing 0..." boolReturn = IsBlank(0) if boolReturn = true then [Link] "It's Blank" else [Link] "not blank"

[Link] "testing 123..." boolReturn = IsBlank(123) if boolReturn = true then [Link] "It's Blank" else [Link] "not blank" [Link] "testing 100-100..." boolReturn = IsBlank(100-100) if boolReturn = true then [Link] "It's Blank" else [Link] "not blank" [Link] "testing null..." boolReturn = IsBlank(null) if boolReturn = true then [Link] "It's Blank" else [Link] "not blank" [Link] "testing empty string..." boolReturn = IsBlank("") if boolReturn = true then [Link] "It's Blank" else [Link] "not blank" [Link] "testing string..." boolReturn = IsBlank("The quick brown fox") if boolReturn = true then [Link] "It's Blank" else [Link] "not blank" -----------------------------------------------------------------------------------------------------------------CSCRIPT [Link] 'Returns Year,Month,Day,Hour,Minute,Seconds,Offset from GMT, Daylight Savings=True/False strComputer = "." ' Date and time Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = [Link]("Select * from Win32_OperatingSystem") For Each objItem in colItems dtmLocalTime = [Link] dtmMonth = Mid(dtmLocalTime, 5, 2) dtmDay = Mid(dtmLocalTime, 7, 2) dtmYear = Left(dtmLocalTime, 4) dtmHour = Mid(dtmLocalTime, 9, 2) dtmMinutes = Mid(dtmLocalTime, 11, 2) dtmSeconds = Mid(dtmLocalTime, 13, 2) Next

' Daylight savings Set Win32Computer = [Link]("SELECT * FROM Win32_ComputerSystem") For Each objItem In Win32Computer oGMT = ([Link] / 60) DaySave = [Link] Next [Link] dtmYear & " " & dtmMonth & " " & dtmDay & " " & dtmHour & " " & dtmMinutes & " " & dtmSeconds & " " & oGMT & " " & DaySave ------------------------------------------------------------------------------------------------------------------------------Delete files older than N days from a folder. cscript [Link] "D:\Demo\log files" 90 Option Explicit 'on error resume next Dim objFS Dim strDirectoryPath Dim objFolder Dim objFileCollection Dim objFile Dim intDaysOld strDirectoryPath = [Link](0) intDaysOld = [Link](1) ' Check the number of days is 1 or greater (otherwise it will just delete everything) If (intDaysOld<=0) Then [Link] -1 [Link] "Delete files more than " & intDaysOld & " days old:" If (IsNull(strDirectoryPath)) Then [Link] -1 [Link] "Delete from: " & strDirectoryPath [Link] "" Set objFS = CreateObject("[Link]") set objFolder = [Link](strDirectoryPath) set objFileCollection = [Link] For each objFile in objFileCollection If [Link] < (Date() - intDaysOld) Then

[Link] [Link] & " " & [Link] '[Link](True) 'To delete for real, remove the ' from the line above End If Next 'Clean up Set objFS = Nothing Set objFolder = Nothing Set objFileCollection = Nothing Set objFile = Nothing ---------------------------------------------------------------------------------------------------------------------------------------

You might also like