vbScript to Obtain IP Address

No Comments »

Being an administrator, you can’t do much if you don’t know about IP Addresses, and how frustrating and stupid it is that it is so difficult to get the IP Address of a machine is a single variable when you are scripting on Windows!!!

sure you can get it with “IPCONFIG” or “ipconfig | find “IP Address”" (winXP) or ipconfig | find “IPv4″ but that gives you a full line and you have to go filter for the ip address only.

Other solutions, vbscript , method, wmi? or registry?

can do both except that the WMI is weird and doesn’t seem to work similarly on winXP as on win7.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
 
For Each objItem In colItems
wscript.echo objItem.IPAddress(0)
Next

Gives you the address but an error which doesn’t quite seem to make sense..

the other option, get it from Registry. This is tricky because the network interface subkey is different for every machine. The trick is to recursively search the subkey for the ValueName you are looking for.. et voilĂ !!

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces"
retval = objReg.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys)
'WScript.Echo retval
strValueName = "IPAddress"
For Each subkey In arrSubKeys
      strKeyPath1 = strKeyPath & "\" & subkey
	  'wscript.echo strKeyPath
	  Return = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath1, _
    strValueName,arrValues)
If (Return = 0) And (Err.Number = 0) Then
' Treat the multistring value as a collection of strings separated by spaces and output
    For Each strValue In arrValues
    WScript.Echo  strValue
Next
End If
 
Next
Posted on August 13th 2010 in IT/ Network Admin

Backup Script check

No Comments »

We have two scripts

1. Connects to all server and check the last backup file (date) & the number of backup in the folder (should be 5),

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile0 = objFSO.OpenTextFile("D:\Script\CheckFilesOnServer\cobianServers.txt", 1)
Dim Last
 
Do Until objFile0.AtEndOfStream
strComputer = objFile0.ReadLine
num = 0
 
' generate a filename base on the script name
strOutputFile = "D:\Script\CheckFilesOnServer\" & Split(WScript.ScriptName, ".")(0) & ".log"
'  Create object and file for output
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
Set OutputLogFile= objFileSystem.GetFile(strOutputFile)
 
'  Create object for file path
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "\\" & strComputer & "-serveur\B$\2009-2010"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
 
	'get path of every file and test path length
		Last= " "
        For Each objFile in colFiles
         on error resume next
         Last=objFile.Name
        	num = num + 1
		Next
		objOutputFile.WriteLine ("Last: " & Last & " - Numb: " & num & "- Server : " & strComputer )
Loop

2. Go and delete any backups older than 11 days on the school servers. deleteOlderFiles

Dim fso, f, f1, fc
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Script\CheckFilesOnServer\cobianServers.txt", 1)
 
Do Until objFile.AtEndOfStream
On error resume next
 
strComputer = objFile.ReadLine
 
'##### Creating Log File and prepare for writing #####
strOutputFile = "D:\Script\CheckFilesOnServer\" & Split(WScript.ScriptName, ".")(0) & ".log"
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
Set OutputLogFile= objFileSystem.GetFile(strOutputFile)
 
'##### Preparing Folder where files are to be deleted #####
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("\\" & strComputer & "-serveur\B$\2009-2010")
Set fc = f.Files
 
'##### Deleting Files where Last Modified is less than 11 days ######
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > 11 Then
f1.Delete
End If
Next
Set fso = Nothing
Set f = Nothing
Set fc = Nothing 
 
objOutputFile.WriteLine ("cleaned backup folder for " & strComputer & " server " )
 
Loop
wscript.echo("Terminé!!!")
Posted on February 8th 2010 in IT/ Network Admin

Checking disk space and service > Excel file

No Comments »

Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2

objExcel.Cells(1, 1).Value = “Machine Name”
objExcel.Cells(1, 2).Value = “Drive”
objExcel.Cells(1, 3).Value = “Total Size”
objExcel.Cells(1, 4).Value = “Free Space”
objExcel.Cells(1, 5).Value = “Free Space Percentage”
objExcel.Cells(1, 6).Value = “SNMP Service status”

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“F:\Script\MachineList.txt”, 1)

Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
Set objWMIService = GetObject(“winmgmts://” & strComputer & “”)

Set colDisks = objWMIService.ExecQuery (“SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3″)

For Each objDisk In colDisks
objExcel.Cells(intRow, 1).Value = Ucase(strComputer)
objExcel.Cells(intRow, 2).Value = objDisk.DeviceID
objExcel.Cells(intRow, 3).Value = FormatNumber(objDisk.Size/1024, 0)
objExcel.Cells(intRow, 4).Value = FormatNumber(objDisk.FreeSpace/1024, 0)
objExcel.Cells(intRow, 5).Value = FormatPercent(objDisk.FreeSpace/objDisk.Size, 0)

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Set colServices = objWMIService.ExecQuery (“Select * from Win32_Service Where Name = ‘snmp’”)

For Each objService in colServices
if objService.State =”Stopped” Then
objService.StartService()
objExcel.Cells(intRow, 6).Value = objService.DisplayName & ” has been restarted”
Else
objExcel.Cells(intRow, 6).Value = objService.DisplayName & ” ” & objService.State
End If
Next

intRow = intRow + 1

Next
Loop

objExcel.Range(“A1:F1″).Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

Wscript.Echo “Done”

Posted on July 17th 2009 in IT/ Network Admin

Terminate Process, Stop Service, delete registry key

No Comments »

First reads machine list from file.. (machine name or ip on each line)

——————————————-
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“F:\Script\MachineList.txt”, 1)

Do Until objFile.AtEndOfStreamOn error resume next
strComputer = objFile.ReadLine
wscript.echo “” wscript.echo “================== ” & strComputer & ” ===================”

‘ Terminating the processes

Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)

Set colProcessList = objWMIService.ExecQuery(“SELECT * FROM Win32_Process WHERE Name = notepad.exe’ OR Name = ‘iexplore.exe’”)

For Each objProcess in colProcessList
objProcess.Terminate()
Next

‘ Stopping the service and setting it to Manual
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Set colServices = objWMIService.ExecQuery (“Select * from Win32_Service Where Name = ‘SNMP’”)

For Each objService in colServices On error resume next
strStartupType = “Manual” intRC = objService.Change(,,,,strStartupType)

if intRC > 0 then
WScript.Echo ” Error setting service startup type: ” & intRC
else
WSCript.Echo ” Successfully set service startup type”
end if

if objService.State =”Stopped” Then
Wscript.Echo objService.DisplayName & ” already stopped”
Else objService.StopService() Wscript.Echo “” & objService.DisplayName & ” has been set to STOPPED”
End If

Next

‘Remove from StartUP
Set WshShell = WScript.CreateObject(“WScript.Shell”)

CAL1 = “HKLM\Software\Microsoft\Windows\CurrentVersion\Run\advertising program”
wscript.echo “App removed from startup”
WshShell.RegDelete cal1

Loop
Wscript.Echo “Done”

—————————————————–

Posted on July 17th 2009 in IT/ Network Admin

Map a printer on a Machine

No Comments »

Map Printer
————————————————————————
Option Explicit
Dim netPrinter, UNCpath
UNCpath = “\\Server\PrinterName”
Set netPrinter = CreateObject(“WScript.Network”)

netPrinter.AddWindowsPrinterConnection UNCpath

WScript.Echo “Your printer is mapped from : ” & UNCpath

——————————————————–

Posted on July 17th 2009 in IT/ Network Admin

Show network mapped printer

No Comments »

Either mapped on a local port or remote printer.. you’ll see then and eventually decide to remove or keep or log.. whatever..

——————————————————————————-
Set objNetwork = WScript.CreateObject(“WScript.Network”)
Set Printers = objNetwork.EnumPrinterConnections

For intDrive = 0 to Printers.Count -1 Step 2
intNetLetter = IntNetLetter +1

IF instr(Printers.Item(intDrive), “IP_”) > 0 OR instr(Printers.Item(intDrive +1), “\\”) > 0 then
WScript.Echo “Path ” & Printers.Item(intDrive) & ” = ” & Printers.Item(intDrive +1)
END IF

Next
———————————————————–

Posted on July 17th 2009 in IT/ Network Admin

Make Remote Machine file run a program

No Comments »

Push file on the server and make it run the program.. more power when run by the machine and liberate my machine to do other things.. Also, doing that, if you have to run the same script on 40 servers, they can run simultaneously, and finish earlier.. smart eh?!!
————————————————————–
Dim filesysDim server(0)
server(0) = “10.20.105.4″

set filesys = CreateObject (“Scripting.FileSystemObject”)
set ourfile = filesys.GetFile(“C:\temp\MsnMsgs.Msi”)

for each scode in server

‘* First copy the batch file to the remote system
Set objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objWMIService = GetObject(“winmgmts:\\” & scode & “\root\cimv2″)
ObjFSO.CopyFile ourfile, “\\” & scode & “\C$\”& ourfile.Name

‘* Launch the process on the remote system
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & scode & “\root\cimv2:Win32_Process”)

strCommand = “C:\” & ourfile.Name “-s”

errReturn = objWMIService.Create(strCommand,null,null,intProcessID)

if errReturn = 0 Then
Wscript.Echo strCommand & ” was started with a process ID of ” _ & intProcessID & “.”
Else
Wscript.Echo strCommand & ” could not be started due to error ” & _ errReturn & “.”
End If

Next

wscript.echo(“Done!!!”)
———————————————————–

Posted on July 17th 2009 in IT/ Network Admin

Checking Service on remote machine

No Comments »

Checking SNMP Service on Remote Machine and restarting it if stopped..

—————————————————————–

server(47) = “OneServer”
server(48) = “TwoServer”

for each scode in server
Set objWMIService = GetObject(“winmgmts:\\” & scode & “\root\cimv2″)

Set colServices = objWMIService.ExecQuery(“Select * from Win32_Service Where Name = ‘SNMP’”)

For Each objService in colServices

if objService.State =”Stopped” Then
objService.StartService()
Wscript.Echo scode & ” Server ” & VbTab & objService.DisplayName & ” has been restarted”
Else
Wscript.Echo scode & ” Server ” & VbTab & objService.DisplayName & VbTab & objService.State
End If

Next

Next
wscript.echo(“END”)

—————————————————————–

Posted on July 17th 2009 in IT/ Network Admin

Checking Remote Machine Registry

No Comments »

Checking if remote machines have Windows Messenger by checking the registry key
————————————————————————-
server(0) = “10.20.129.139″
server(1) = “10.20.129.140″
server(2) = “10.20.129.142″

for each scode in server
Set filesys = CreateObject (“Scripting.FileSystemObject”)
Const HKEY_LOCAL_MACHINE = &H80000002

On Error Resume Next
Set objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & scode & “\root\default:StdRegProv”)
strKeyPath =”Software\Microsoft\Windows Live\Communications Clients\Shared”strValueName=”Messenger Install Root”

objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Err.Number = 0 Then

if strValue <> EMPTY THEN
WScript.Echo “INSTALLED ” & scode & ” under ” & strValue
else
WScript.ECHO “none ” & scode
End IF

Else
WScript.Echo “ERROR ” & scode & ” ” & Err.Number

End IF

NEXT
————————————————————————-

Posted on July 17th 2009 in IT/ Network Admin

Viewing all processes running on a remote machine

No Comments »

Viewing all processes on a remote machine:
—————————————————————–
Dim server(0)

server(0) = “OneServer”

for each scode in server

Set objWMIService = GetObject(“winmgmts:\\”& scode & “.csdccs.edu.on.ca\root\cimv2″)
Set colProcesses = objWMIService.ExecQuery(“Select * from Win32_Process”)

For Each objProcess in colProcesses
Wscript.Echo objProcess.Name
Next

Next

wscript.echo(“END”)
—————————————————————–

Posted on July 17th 2009 in IT/ Network Admin