Home » Scripting (Automation)

Category Archives: Scripting (Automation)

Delete Old Files

Batch script to delete X days old file from specified location. Where is X is number of days.

@echo off
REM Remove files backup older than X days
Echo Deleting Bak file….
forfiles /p “Folder Path” /s /m *.bak /c “cmd /c Del @path” /d -X
Exit

Note:
Folder Path= Replace with target folder.
Replace X with no of days (Eg :3 for 3 days old)

Thanks,
WintelAdmin

Ping multiple server and display IP with status.

This script will ping multiple server available in servers.txt and display IP with status.
Copy below contents and save as filename.vbs and create a servers.txt will all server names one per line.

************************************************************************************************************

Const ForReading = 1
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objShell = CreateObject(“WScript.Shell”)
If not objFSO.FileExists(“servers.txt”) THEN
wscript.echo “Please create a file named ‘servers.txt’ with one PC name to be pinged per line,”&_
vbcrlf&”with a hard return at the end of each line.”
wscript.quit
end if
tempobj=”temp.txt”

Set objTextFile = objFSO.OpenTextFile(“servers.txt”, ForReading)
logfile=”results.csv”
Set ofile=objFSO.CreateTextFile(logfile,True)
strText = objTextFile.ReadAll
objTextFile.Close
wscript.echo “Ping batch starting, please be patient. This could take some time to”&_
vbcrlf&”finish, depending on the number of hosts to check. You “_
&”will be “&vbcrlf&”notified upon the completion of this script.”
ofile.WriteLine “,”&”Ping Report — Date: ” & Now() & vbCrLf
arrComputers = Split(strText, vbCrLF)
for each item in arrcomputers
objShell.Run “cmd /c ping -n 1 -w 1000 ” & item & ” >temp.txt”, 0, True
Set tempfile = objFSO.OpenTextFile(tempobj,ForReading)
Do Until tempfile.AtEndOfStream
temp=tempfile.readall
striploc = InStr(temp,”[“)
If striploc=0 Then
strip=””
Else
strip=Mid(temp,striploc,16)
strip=Replace(strip,”[“,””)
strip=Replace(strip,”]”,””)
strip=Replace(strip,”w”,” “)
strip=Replace(strip,” “,””)
End If

If InStr(temp, “Reply from”) Then
ofile.writeline item & “,”&strip&”,”&”Online.”
ElseIf InStr(temp, “Request timed out.”) Then
ofile.writeline item &”,”&strip&”,”&”No response (Offline).”
ELSEIf InStr(temp, “try again”) Then
ofile.writeline item & “,”&strip&”,”&”Unknown host (no DNS entry).”

End If
Loop
Next
tempfile.close
objfso.deletefile(tempobj)
ofile.writeline
ofile.writeline “,”&”Ping batch complete “&now()
wscript.echo “Ping batch completed. The results will now be displayed.”
objShell.Run(“””C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\excel.exe “””&logfile)

************************************************************************************************************

Thanks,
WintelAdmin

VB Script to collect Windows server hardware details on multiple machines.

VB Script to collect Windows server hardware details. Script will Collect Serial Number, Make, Model, OS, CPU, RAM, Drive details on multiple servers remotely.

Copy below contents and save as filename.vbs and create a machines.txt will all server names one per line.

 

***********************************************************************************************************************

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Const PATH_TO_INPUT = “Machines.txt”
Const PATH_TO_OUTPUT = “MachineInventory.csv”

Dim fso
Set fso = WScript.CreateObject(“Scripting.FileSystemObject”)

Dim shl
Set shl = WScript.CreateObject(“WScript.Shell”)

Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)

Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)

output.WriteLine “Hostname,Serial Number,Make,Model,BIOS Version,Operating System,CPU,Memory (MB),Disk Drives”

Dim wmiService
Dim wmiResults

Dim hostname
Dim make
Dim model
Dim biosversion
Dim operatingSystem
Dim serialNumber
Dim cpu
Dim memory
Dim drives

Dim line
Dim exec
Dim pingResults
While Not input.AtEndOfStream
line = input.ReadLine
hostname = “”
make = “”
model = “”
biosversion = “”
operatingSystem = “”
serialNumber = “”
cpu = “”
memory = “”
drives = “”

Set exec = shl.Exec(“ping -n 2 -w 1000 ” & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, “reply from”) Then
WScript.Echo “Reply From: ” & line
On Error Resume Next

Set wmiService = GetObject(“winmgmts:\\” & line & “\root\CIMV2”)

If Not Err.Number = 0 Then
output.WriteLine line & “,Error: ” & Err.Description
WScript.Echo line & “,Error: ” & Err.Description
On Error GoTo 0
Else
On Error GoTo 0
hostname = line

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_BIOS”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim item
For Each item In wmiResults
serialNumber = Trim(item.SerialNumber)
biosversion = Trim(item.SMBIOSBIOSVersion)
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_ComputerSystem”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
make = Trim(item.Manufacturer)
model = Trim(item.Model)
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_OperatingSystem”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
operatingSystem = Trim(item.Name)
operatingSystem = Split(operatingSystem, “|”)(0)
memory = Round(Trim(item.TotalVisibleMemorySize) / 1024, 2)
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_Processor”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
cpu = Trim(item.Name)
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_LogicalDisk WHERE DriveType=3”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
drives = drives & Trim(item.DeviceID) & ” ” & Round(Trim(item.Size) / (1024^2), 2) & “;”
Next

output.WriteLine hostname & “,” & serialNumber & “,” & make & “,” & model & “,” & biosversion & “,” & operatingSystem & “,” & cpu & “,” & memory & “,” & drives
WScript.Echo hostname & “,” & serialNumber & “,” & make & “,” & model & “,” & biosversion & “,” & operatingSystem & “,” & cpu & “,” & memory & “,” & drives
End If
Else
output.WriteLine line & “,No Response”
WScript.Echo line & “,No Response”
End If
Wend

output.Close
input.Close

Set wmiService = Nothing
Set wmiresults = Nothing

***********************************************************************************************************************

 

Thanks,

WintelAdmin

 

Powershell Script to start/stop/disable service on multiple remote server

Powershell script to control windows service on multiple remote servers.

Get-content “Filename” | foreach {set-Service -ComputerName $_ -Name “Service Name” -StartupType Disabled -Status Stopped}

Note:
Replace Filename with filename location (Ex C:\hostname.txt).
Add all servers names to filesname (Ex hostname.txt)
Change StartupType as Disabled/Manual/Automatic
Change Status as Stopped/started

-WintelAdmin


Visitors Count

Subscriber Count

    3806
March 2017
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031