'Written by Alan Kaplan, alan@akaplan.com for VISN6 'You must have ADSI on PC running this script for it to work 'written 11/1/01 'last change 11/1/01. Added run on all option. Option Explicit Dim DnameObj,objContainer,colPCs,objEnv,objDomain Dim isnew, path, newpath, oFolders,fso,WSfile dim message,dname,mydomain,title, command,allcommand Dim major,minor,ver,wshshell,key,key2 dim index,d,a, i, s , xfile ,exname, quote, ntreskit dim filename, NotThisPC, readfile, rserver,allsystems Set WshShell = WScript.CreateObject("WScript.Shell") set fso = CreateObject("Scripting.FileSystemObject") Set d = CreateObject("Scripting.Dictionary") filename="ExcludeList.txt" index=0 AllSystems = TRUE rserver = "RemotePCName" 'message format helper.. quote = chr(34) Function RunCommandOn(Rserver) '********* EDIT HERE! **************** 'trivial example -- ping all. Note the space after ping 'Rserver is the variable representing the remote server/workstation allcommand = "cmd /c ping " & Rserver 'more interesting example -- reboot time is in seconds 'need path to ntreskit -- add trailing \ 'ntreskit = "\\vhasbyapps\ntreskit$\" 'message = "You may shutdown and restart at any time." 'allcommand = ntreskit & "shutdown \\" & Rserver & " /r /t:3600 " & quote & message & quote & " /y /c" 'show what is running in main window Wscript.echo "Running " & allcommand '1 is in Window, 0 hidden. true is wait to complete, false don't wshShell.Run allcommand, 1, true '********* END EDITS **************** End Function on error resume next 'Check adequacy of current config. SysTest on error goto 0 'get resource domain name as default search string within logon domain key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon\CachePrimaryDomain" mydomain = WshShell.RegRead (key) 'alternate: get domain logged into 'set objEnv = WshShell.Environment("process") 'mydomain = objEnv("USERDOMAIN") 'Get current domain name ' Define dialog box variables. message = "Please enter domain name to query for system names." title = "Domain to Query" 'get domain name, domain default dname = InputBox(message, title, mydomain) ' Evaluate the user input. If dname = "" Then ' Canceled by the user WScript.quit End If message="Run command on all systems in " & Dname & "?" title = "All Systems" if(MsgBox(message,vbYesNo+ vbQuestion,title) = vbNo) then AllSystems = FALSE message = "Do you want to create/review the list " & vbcrlf message = message & "of computers to exclude from processing?" If (MsgBox(message,vbYesNo + vbQuestion,"Exclude Files") = vbYes) Then OpenNotepad() End If End if 'Bind to the builtin administrators group to see if you can connect 'This is a kludge, but binding to non-existent domain does not raise error GetObject("WinNT://" & dname &"/administrators") if err.number then message = "Cannot bind to " &dname WshShell.Popup message,0,"Connection or Authority Failure",vbCritical WScript.Quit Else 'This should work with with Win2k Active directory per docs.... DnameObj = "WinNT://" & dname 'Case counts here! End If message="Ready to run command on " & Dname & "?" title = "Last Chance to Cancel" if(MsgBox(message,vbYesNo+ vbQuestion,title) = vbNo) then Wscript.quit End if Set objContainer=GetObject(DnameObj) objContainer.Filter=Array("computer") If AllSystems = FALSE then SelectBoxes() Else AllBoxes End IF 'Show done WshShell.Popup "Command run on " & DNAME,3,"Action Complete",64 If AllSystems = FALSE then 'cleanup the big one... a = d.RemoveAll End If Wscript.Quit 'Script ends Sub SysTest() on error resume next ' WSH version tested Major = (ScriptEngineMinorVersion()) Minor = (ScriptEngineMinorVersion())/10 Ver = major + minor 'Need version 5.5 If err.number or ver < 5.5 then message = "You have WScript Version " & ver & ". Please load Version 5.5" End If 'Test for ADSI err.clear key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\version" key2 = WshShell.RegRead (key) if err <> 0 then message = message & "ADSI must be installed on local workstation to continue" & vbCrLf WshShell.Popup message,0,"Workstation Setup Error",vbInformation WScript.Quit End if If (Not IsCScript()) Then message = "You must set default host to cscript." &vbcrlf &_ "Run command wscript //h:cscript." wshShell.Popup message,10,"Workstation Setup Error",vbCritical WScript.Quit ' Terminate script. End If End Sub function addpc(name) wscript.echo "Adding " & name & " to list of computers to process" d.Add name,index index = index +1 End function Function ProcessExcludes() Set readfile=fso.OpenTextFile(xfile,1,False) Do while not readfile.atendofstream on error resume next 'not sure if upper needed, but keeps case clean NotThisPC=UCASE(readfile.readline) d.Remove NotThisPC wscript.echo "Removed " & NotThisPC & " from list" loop readfile.close End Function Function IsCScript() ' Check whether CScript.exe is the host. If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = True Else IsCScript = False End If End Function Function GetPath ' Retrieve path to the script file path = WScript.ScriptFullName ' Script file name GetPath = Left(path, InstrRev(path, "\")) End Function Function SelectBoxes() For Each colPCs in objContainer AddPC(colpcs.name) Next processexcludes() 'Run on each computer in dictionary WSCRIPT.ECHO vbcrlf & D.COUNT & " systems to run command on." &vbcrlf a = d.keys ' Get the keys For i = 0 To d.Count -1 ' Iterate the array. rserver = a(i) ' Create return string. RuncommandOn Rserver Next End Function Function AllBoxes() For Each colPCs in objContainer RuncommandOn colpcs.name Next End Function Function OpenNotepad() getpath() xfile=getpath&filename If (Not fso.fileExists(xfile)) Then ' file doesn't exist; create it. Set Exname = fso.createtextfile(xfile) 'Close the file so it may be edited! Exname.Close message="Put NetBIOS name for each computer in file." &vbcrlf message=message & "One system name on each line." &vbcrlf &vbcrlf message=message & "Save on closing, do not rename file." title = "Exclusion File Information" command=MsgBox(message,64,title) End If command = "notepad.exe " & xfile wshShell.Run command, 1, true End Function