Вы находитесь на странице: 1из 6

Run a VBScript file To run a VBScript from within another VBScript: Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.

Run "cscript c:\batch\demo.vbs" Run a CMD batch file To run a CMD batch file from VBScript: Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run "c:\batch\test.cmd" Run a PowerShell script To run a PowerShell script from VBScript: Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run "powershell -file ""c:\batch\demo.ps1""" -----------------------------------------------------------------------------------------------------------------------------------------Find and replace a text string in a file. 'usage: cscript replace.vbs Filename "StringToFind" "stringToReplace" Option Explicit Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent strFilename=WScript.Arguments.Item(0) strSearch=WScript.Arguments.Item(1) strReplace=WScript.Arguments.Item(2) 'Does file exist? Set fso=CreateObject("Scripting.FileSystemObject") if fso.FileExists(strFilename)=false then wscript.echo "file not found!" wscript.Quit end if 'Read file set objFile=fso.OpenTextFile(strFilename,1) oldContent=objFile.ReadAll

'Write file newContent=replace(oldContent,strSearch,strReplace,1,-1,0) set objFile=fso.OpenTextFile(strFilename,2) objFile.Write newContent objFile.Close Example cscript //Nologo replace.vbs c:\docs\demo.txt "Madonna" "Lady Gaga" ---------------------------------------------------------------------------------------------------List all the computers in a given domain. ' ListComputers.vbs On Error Resume Next Set objDomain = GetObject("WinNT://SS64Domain") WScript.echo "Domain : " + objDomain.name For each objDomainItem in objDomain if objDomainItem.Class = "Computer" then WScript.echo objDomainItem.Name end if Next -------------------------------------------------------------------------------------------------List all the user accounts in a specific domain. 'ListUsers.vbs On Error Resume Next Set objDomain = GetObject("WinNT://SS64Domain") WScript.echo "Domain : " + objDomain.name For each objDomainItem in objDomain if objDomainItem.Class = "User" then WScript.echo objDomainItem.Name + " : Full Name: " + objDomainItem.FullName 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 wscript.echo " 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 Wscript.echo "testing 0..." boolReturn = IsBlank(0) if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing 123..." boolReturn = IsBlank(123) if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank" Wscript.echo "testing 100-100..." boolReturn = IsBlank(100-100) if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank" Wscript.echo "testing null..." boolReturn = IsBlank(null) if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank" Wscript.echo "testing empty string..." boolReturn = IsBlank("") if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank" Wscript.echo "testing string..." boolReturn = IsBlank("The quick brown fox") if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank" -----------------------------------------------------------------------------------------------------------------CSCRIPT datetime.vbs '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 = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For Each objItem in colItems dtmLocalTime = objItem.LocalDateTime 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 = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") For Each objItem In Win32Computer oGMT = (objItem.CurrentTimeZone / 60) DaySave = objItem.DaylightInEffect Next Wscript.Echo dtmYear & " " & dtmMonth & " " & dtmDay & " " & dtmHour & " " & dtmMinutes & " " & dtmSeconds & " " & oGMT & " " & DaySave ------------------------------------------------------------------------------------------------------------------------------Delete files older than N days from a folder. cscript delolder.vbs "D:\Demo\log files" 90 Option Explicit 'on error resume next Dim objFS Dim strDirectoryPath Dim objFolder Dim objFileCollection Dim objFile Dim intDaysOld strDirectoryPath = WScript.Arguments.Item(0) intDaysOld = WScript.Arguments.Item(1) ' Check the number of days is 1 or greater (otherwise it will just delete everything) If (intDaysOld<=0) Then WScript.quit -1 wscript.echo "Delete files more than " & intDaysOld & " days old:" If (IsNull(strDirectoryPath)) Then WScript.quit -1 wscript.echo "Delete from: " & strDirectoryPath wscript.echo "" Set objFS = CreateObject("Scripting.FileSystemObject") set objFolder = objFS.GetFolder(strDirectoryPath) set objFileCollection = objFolder.Files For each objFile in objFileCollection If objFile.DateLastModified < (Date() - intDaysOld) Then

Wscript.Echo objFile.Name & " " & objFile.DateLastModified 'objFile.Delete(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 ---------------------------------------------------------------------------------------------------------------------------------------