April 10, 2013

VBS Script to remotely execute a commands and modify local groups on a bunch of servers.


Following VBS script was used to modify two localgroups and execute two commands on a bunch of servers.

Dim objWMIService, errReturn, intProcessID, strCmd1, strCmd2, CmdPath

Dim ServerList(9)
ServerList(0) = "Server1"
ServerList(1) = "Server2"
ServerList(2) = "Server3"
ServerList(3) = "Server4"
ServerList(4) = "Server5"
ServerList(5) = "Server6"
ServerList(6) = "Server7"
ServerList(7) = "Server8"
ServerList(8) = "Server9"
ServerList(9) = "Server10"

CmdPath = """C:\Windows\System32"""
StrCmd1 = """C:\Windows\System32\ipconfig.exe""" & " /flushdns"
StrCmd2 ="""C:\Windows\System32\ipconfig.exe""" & " /registerdns"

For Each item In ServerList
Set objLocalGroup = GetObject("WinNT://" & item & "/Performance Monitor Users")
Set objLocalGroup2 = GetObject("WinNT://" & item & "/Performance Log Users")
Set objDomainUser = GetObject("WinNT://MYDOMAIN/This is a domain user")
Set objADGroup1 = GetObject("WinNT://MYDOMAIN/This is a global group")
If (objLocalGroup.IsMember(objADGroup1.ADsPath) = False) Then
objLocalGroup.Add(objADGroup1.ADsPath)
End If
If (objLocalGroup.IsMember(objDomainUser.ADsPath) = False) Then
objLocalGroup.Add(objDomainUser.ADsPath)
End If
If (objLocalGroup2.IsMember(objADGroup1.ADsPath) = False) Then
objLocalGroup2.Add(objADGroup1.ADsPath)
End If
If (objLocalGroup2.IsMember(objDomainUser.ADsPath) = False) Then
objLocalGroup2.Add(objDomainUser.ADsPath)
End If
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & item & _
    "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create(strCmd1,null,null,intProcessID)
if errReturn = 0 Then
Msgbox strCmd1 & " was started with a process ID of " _
& intProcessID & " on " & item & "."
Else
Msgbox strCmd1 & " could not be started. Error: " & errReturn & " on " & item & "."
End If
Set intProcessID = Nothing
Set errReturn = Nothing
errReturn = objWMIService.Create(strCmd2,null,null,intProcessID)
if errReturn = 0 Then
Msgbox strCmd2 & " was started with a process ID of " _
& intProcessID & " on " & item & "."
Else
Msgbox strCmd2 & " could not be started. Error: " & errReturn & " on " & item & "."
End If
Set objWMIService = Nothing
Set intProcessID = Nothing
Set errReturn = Nothing
Set objLocalGroup = Nothing
Set objADGroup1 = Nothing
Set objLocalGroup2 = Nothing
Set objDomainUser = Nothing
Next