This script will install a batch file to the computers listed in the text file.
For executing this, you need to create a text file (Servers.txt) listing computer names (one name per line).
This will check if the application is already present in the machine and if present it will reinstall the application and if not it will install to that machine.
----------------------------------------------------------------------------------------------------------------------
Dim strComputer
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems
Dim objFSO
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems
Dim objFSO
Const INPUT_FILE_NAME = "C:\Servers.txt"
Const FOR_READING = 1
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
dim shell
dim objTextFile
set shell=createobject("wscript.shell")
Set objTextFile = objFSO.CreateTextFile("C:\SMSClient.txt", True)
objTextFile.WriteLine("Remote installation of SMS Client:")
objTextFile.WriteLine("----------------------------------")
objTextFile.WriteBlankLines(1)
rem Next
objTextFile.WriteLine("Remote installation of SMS Client:")
objTextFile.WriteLine("----------------------------------")
objTextFile.WriteBlankLines(1)
rem Next
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
On Error Resume Next
For Each strComputer In arrComputers
If Trim(strComputer) <> "" Then
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"\root\CIMV2",UserName,Password)
If Trim(strComputer) <> "" Then
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"\root\CIMV2",UserName,Password)
If (Err.Number <> 0) Then
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Error encountered connecting to '" & strComputer & "': " & Err.Number & ", " & Err.Description
objTextFile.WriteBlankLines(1)
rem Next
Err.Clear
Else
Set colItems = objWMIService.ExecQuery("Select * from Win32_WMISetting",,48)
If (Err.Number <> 0) Then
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Error querying Win32_WMISetting on '" & strComputer & "': " & Err.Number & Err.Description
objTextFile.WriteBlankLines(1)
rem Next
Err.Clear
Else
For Each objItem in colItems
Const HKEY_LOCAL_MACHINE = &H80000002
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4A39A27F-005B-407E-8CF5-F4D8065658E4}"
strValueName = "DisplayName"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Error encountered connecting to '" & strComputer & "': " & Err.Number & ", " & Err.Description
objTextFile.WriteBlankLines(1)
rem Next
Err.Clear
Else
Set colItems = objWMIService.ExecQuery("Select * from Win32_WMISetting",,48)
If (Err.Number <> 0) Then
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Error querying Win32_WMISetting on '" & strComputer & "': " & Err.Number & Err.Description
objTextFile.WriteBlankLines(1)
rem Next
Err.Clear
Else
For Each objItem in colItems
Const HKEY_LOCAL_MACHINE = &H80000002
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4A39A27F-005B-407E-8CF5-F4D8065658E4}"
strValueName = "DisplayName"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If IsNull(strValue) Then
objFSO.CopyFile "Install.bat" , "\\" & strComputer & "\C$\", True
strCommand = "\\" & strComputer & "\C$\Install.bat"
Const INTERVAL = "n"
Const MINUTES = 1
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objFSO.CopyFile "Install.bat" , "\\" & strComputer & "\C$\", True
strCommand = "\\" & strComputer & "\C$\Install.bat"
Const INTERVAL = "n"
Const MINUTES = 1
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
Wscript.Sleep 150000
objFSO.DeleteFile("\\" & strComputer & "\C$\Install.bat")
objTextFile.WriteLine "" & Date & " " & Time & ":" & " SMS Client is not found in " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
If errReturn <> 0 then
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Failed to Install SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
Else
WScript.Sleep 20000
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Successfully installed SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
End if
Else
strValue= "Processing on "
objTextFile.WriteLine(Date & " " & Time & ":" & " " & strValue & strComputer)
objTextFile.WriteBlankLines(1)
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Reinstalling SMS Client in " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Failed to Install SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
Else
WScript.Sleep 20000
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Successfully installed SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
End if
Else
strValue= "Processing on "
objTextFile.WriteLine(Date & " " & Time & ":" & " " & strValue & strComputer)
objTextFile.WriteBlankLines(1)
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Reinstalling SMS Client in " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
intCacheSize = 2048
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/ccm/SoftMgmtAgent")
Set colItems = objWMIService.ExecQuery("Select * from CacheConfig")
Set colItems = objWMIService.ExecQuery("Select * from CacheConfig")
For Each objItem_1 in colItems
objItem_1.Size = intCacheSize
objItem_1.Put_ 0
Next
objItem_1.Size = intCacheSize
objItem_1.Put_ 0
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'CCMExec'")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'CCMExec'")
For Each objItem_1 in colItems
objItem_1.StopService(strServiceName)
Wscript.Sleep 10000
objItem_1.StartService(strServiceName)
objTextFile.WriteLine "" & Date & " " & Time & ":" & " The Cache On " & UCase(strComputer) & " is changed"
objTextFile.WriteBlankLines(1)
rem Next
Next
objItem_1.StopService(strServiceName)
Wscript.Sleep 10000
objItem_1.StartService(strServiceName)
objTextFile.WriteLine "" & Date & " " & Time & ":" & " The Cache On " & UCase(strComputer) & " is changed"
objTextFile.WriteBlankLines(1)
rem Next
Next
objFSO.CopyFile "Reinstall.bat" , "\\" & strComputer & "\C$\", True
strCommand = "\\" & strComputer & "\C$\Reinstall.bat"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
Wscript.Sleep 150000
objFSO.DeleteFile("\\" & strComputer & "\C$\Reinstall.bat")
If errReturn <> 0 then
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Failed to Install SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
Else
WScript.Sleep 20000
objTextFile.WriteLine "" & Date & " " & Time & ":" & " Successfully installed SMS Client to " & strComputer
objTextFile.WriteBlankLines(1)
rem Next
End if
End If
Next
End If
End If
End If
Next
End If
End If
End If
Next
No comments:
Post a Comment