Friday, March 21st, 2008
Batch-scripting is not as powerful as WSH-scripting or PowerShell.
But with some tools you can perform some simple but useful actions.
One tool I often use is robocopy.
- Asking for values in a batch-script
@echo off
@set /P pctocheck=Enter name of pc:
@cscript.exe "script.vbs" %pctocheck%
pause
- Copy non-existing folders back to C-drive
@echo off
rem Check if the script was called with parameter RUN
IF "%1"=="RUN" GOTO run
rem Since we are not running the script with parameter RUN, start it using cmd whilst enabling delayed environment variable expansion (/V:ON)
cmd /V:ON /C H:\copy_c-drive.cmd RUN
GOTO :EOF
:run
echo ...Copying extra folders on C-drive
rem List all existing files and directories on the C-drive, including hidden ones.
dir /a:d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemdirs.txt
dir /a:hd /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemdirs.txt
dir /a:-d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemfiles.txt
dir /a:h-d /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemfiles.txt
rem Build up variables
set existingdirs=
FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemdirs.txt) do set existingdirs=!existingdirs! /XD "%%i"
set existingfiles=
FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemfiles.txt) do set existingfiles=!existingfiles! /XF "%%i"
rem Do the copy thing
robocopy /COPYALL /E X:\ C:\ /TEE /LOG+:C:\WINDOWS\TEMP\rc_system.log /XO /R:1 /W:3 %existingdirs% %existingfiles%
Tags: Batch, Input, Robocopy, Scripting, Windows
Posted in Batch, IT related, Scripting | No Comments »
Friday, March 21st, 2008
Using WSH with WMI enables you to query lots of information of a system.
Here are some examples of code that I regularly use.
- Determine UpTime of a computer
strComputer = InputBox("Enter computername","Determine Uptime",".")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")
intSystemUptime = 0
For Each objOS in colOperatingSystems
Dim intSystemUptimeSec
Dim intSystemUptimeMin
Dim intSystemUptimeHour
Dim intSystemUptimeDay
intSystemUptimeDay = Int(objOS.SystemUpTime / 86400)
intSystemUptimeHour = Int(objOS.SystemUpTime / 3600) - (intSystemUptimeDay*24)
intSystemUptimeMin = Int(objOS.SystemUpTime / 60) - (intSystemUptimeHour*60) - (intSystemUptimeDay*24*60)
intSystemUptimeSec = Int(objOS.SystemUpTime) - (intSystemUptimeMin*60) - (intSystemUptimeHour*60*60) - (intSystemUptimeDay*24*60*60)
intSystemUptime = Int(objOS.SystemUpTime / 60)
MsgBox("Uptime for " & strcomputer & " = " & intSystemUptimeDay & "d " & intSystemUptimeHour & "h " & intSystemUptimeMin & "m " & intSystemUptimeSec & "s")
Next
- Ping a computer
strComputer = InputBox("Enter computername","Ping a computer",".")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
MsgBox("Ping failed")
else
MsgBox("Ping succeeded")
End If
Next
- Map a Networkdrive
Set oNet = CreateObject("wscript.network")
oNet.MapNetworkDrive "X:", "\\" & strComputer & "\c$"
- Get Computers from an OU in Active Directory
Set ObjOU = GetObject("LDAP://OU=Desktops,OU=Computers,DC=corp,DC=contoso,DC=com")
ObjOU.Filter = Array("Computer")
For Each ObjComp in ObjOU
- List running processes
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")
For Each objProcess in colProcess
WScript.Echo objProcess
Next
- Count DiskErrors
Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System'" _
& " and SourceName = 'disk'")
iDiskErrors = colLoggedEvents.count
- Show logged on user
Set Users = objWMIService.InstancesOf("Win32_ComputerSystem")
for each User in Users
If isNull(User.UserName) then
WScript.Echo "No User is logged on"
else
WScript.Echo User.UserName
end if
Next
- Enumerate all profiles stored in Documents and Settings
Set objFolder = objFSO.GetFolder("\\" & strComputer & "\c$\Documents And Settings")
valStoredprofiles = ""
For Each Subfolder in objFolder.SubFolders
If IsStandardUserProfile(SubFolder.Path) then
arrPath = split(Subfolder.Path,"\")
'sSize = Round(SubFolder.Size/1024/1024,2)
'valStoredprofiles = valStoredprofiles & arrPath(UBound(arrPath,1)) & " [" & sSize & "MB] " & " - "
valStoredprofiles = valStoredprofiles & arrPath(UBound(arrPath,1)) & " - "
end if
Next
Function IsStandardUserProfile(sFolder)
Dim iMatches
iMatches = 0
If Instr(sFolder,"Administrator") > 0 then iMatches = iMatches + 1
If Instr(sFolder,"All Users") > 0 then iMatches = iMatches + 1
If Instr(sFolder,"Default User") > 0 then iMatches = iMatches + 1
If Instr(sFolder,"LocalService") > 0 then iMatches = iMatches + 1
If Instr(sFolder,"NetworkService") > 0 then iMatches = iMatches + 1
If iMatches = 0 then
IsStandardUserProfile= true
Else
IsStandardUserProfile= false
end if
End Function
- Read and write an XML-file
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.load "result.xml"
'WScript.Echo objXML.parseError.errorCode
If (objXML.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = objXML.parseError
MsgBox("You have error " & myErr.reason)
Else
'WScript.Echo objXML.xml
'WScript.Echo objXML.documentElement.attributes.item(0).nodeValue
Dim i
i = 1
ReDim PreServe arrPcs(i+1)
Set pcs = objXML.getElementsByTagName("pc")
'WScript.Echo pcs
for each pc in pcs
arrPcs(i-1) = pc.getAttribute("id") & ";" & pc.getAttribute("location")
i=i+1
ReDim PreServe arrPcs(i)
next
End If
for each pc in pcs
pcID = pc.getAttribute("id")
if pcID = current PC then
'<ip>192.168.1.19</ip>
'<uptime>0</uptime>
'<diskerrors>0</diskerrors>
'<user>NONE</user>
'<storedprofiles>0</storedprofiles>
'WScript.Echo pc.childNodes.length
pc.childNodes.Item(0).firstChild.nodeValue = valIP
pc.childNodes.Item(2).firstChild.nodeValue = valUptime
pc.childNodes.Item(3).firstChild.nodeValue = valDiskerrors
'WScript.Echo valUser
pc.childNodes.Item(4).firstChild.nodeValue = valUser
pc.childNodes.Item(5).firstChild.nodeValue = valStoredprofiles
exit for
end if
next
objXML.documentElement.Attributes.Item(0).nodeValue = now
objXML.save("result.xml")
Tags: Active Directory, Computers, Disk Errors, Logged on User, Running Processes, Scripting, Uptime, VBScript, Windows, WMI, WSH
Posted in IT related, Scripting, VBScript, WSH | No Comments »