Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » В помощь системному администратору » Автоматизация администрирования

Модерирует : lynx, Crash_Master, dg, emx, ShriEkeR

emx (13-12-2006 21:07): http://forum.ru-board.com/topic.cgi?forum=8&topic=19088#1  Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

   

Jovanotti



Advanced Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору

Hack 8 Execute a Command on Each Computer in a Domain
 
 
This handy script lets you easily run any command on a specified subset of computers in your domain.
 
Running the same command on multiple computers in your domain can be tedious indeed, but such a scenario is common in an administrator's life. I've written this hack to make this chore easier. The script traverses member systems of a domain, executing a command against each system that has a name that matches a particular specification you specify in the command line. Note that regular expressions are legal in this script, which makes it a powerful and flexible addition to the administrator's toolkit.
 
The Code
To use this script, type it into a text editor such as Notepad (make sure Word Wrap is disabled) and save it with a .vbs extension as ExecuteAll.vbs. Alternatively, if you don't want to wear your fingers out, you can download the script from the O'Reilly web site.

Код:
 
'Script Name: ExecuteAll.vbs
 
 
 
Option Explicit
 
 
 
Dim oDomain, oService, oItem, oShell
 
Dim strDomain, strSpec, strCommand, intButton
 
Dim oArgs, strFinalCommand, oRegEx, boolConfirm
 
 
 
' Prepare to execute commands & do popups
 
Set oShell = CreateObject("WScript.Shell")
 
 
 
GetArguments
 
 
 
' Access the domain so we can traverse objects
 
WScript.Echo "Accessing NT Domain " & strDomain
 
Set oDomain = GetObject("WinNT://" & strDomain)
 
 
 
' Initiate our regular expression support
 
Set oRegEx = New RegExp
 
oRegEx.Pattern = strSpec
 
oRegEx.IgnoreCase = True
 
 
 
' Traverse each computer (WinNT) object in the domain
 
WScript.Echo "Searching for " & strSpec
 
oDomain.Filter = Array("Computer") ' only look at computers
 
For Each oItem In oDomain
 
If oRegEx.Test(oItem.Name) Then
 
WScript.Echo " Matched " & oItem.Name
 
strFinalCommand = Replace(strCommand, "$n", oItem.Name)
 
 
 
intButton = vbNo
 
If boolConfirm Then
 
intButton = oShell.Popup("Execute " & strFinalCommand & "?",,_
 
"System " & oItem.Name, vbYesno + vbQuestion)
 
End If
 
If (boolConfirm = False) Or (intButton = vbYes) Then
 
WScript.Echo " Executing: " & strFinalCommand
 
execute strFinalCommand
 
End If
 
End If
 
Next
 
 
 
' All done; clean up
 
Set oItem = Nothing
 
Set oRegEx = Nothing
 
Set oDomain = Nothing
 
Set oShell = Nothing
 
Set oArgs = Nothing
 
 
 
'
 
' Glean the arguments for our run from the command line, if provided.
 
' If any are missing, prompt for input. A blank input signals an abort.
 
'
 
' /Y is an optional last argument
 
Sub GetArguments
 
Dim i, strConfirm, intButton
 
Set oArgs = WScript.Arguments
 
 
 
boolConfirm = True ' assume always confirm
 
strDomain = "" ' domain to be traversed
 
strSpec = "" ' name specification to be matched
 
strCommand = "" ' command to be executed on each match
 
strConfirm = "" ' track prompting for confirmation setting
 
 
 
' Look for our optional 4th argument
 
If oArgs.Length = 4 Then
 
If UCase(oArgs.Item(3)) = "/Y" Then
 
boolConfirm = False
 
strConfirm = "/Y" ' don't prompt below
 
End If
 
End If
 
 
 
' Look for any specified arguments, in order
 
If oArgs.Length >= 1 Then strDomain = oArgs(0)
 
If oArgs.Length >= 2 Then strSpec = oArgs(1)
 
If oArgs.Length >= 3 Then strCommand = oArgs(2)
 
 
 
' Prompt for any arguments not specified on the command line
 
If strDomain = "" Then
 
strDomain = InputBox _
 
("Enter the name of the NT Domain to be traversed", _
 
"NT Domain")
 
End If
 
If strDomain = "" Then WScript.Quit
 
strDomain = UCase(strDomain)
 
 
 
If strSpec = "" Then
 
strSpec = InputBox _
 
("Enter your name specification for the computer(s) " & _
 
"that will be matched within the " & strDomain & " Domain." & _
 
vbCrlf & "Regular Expressions are acceptable.", _
 
"Name Specification")
 
End If
 
If strSpec = "" Then WScript.Quit
 
 
 
If strCommand = "" Then
 
strCommand = InputBox _
 
("Enter the command to be executed on each computer matching " & _
 
strSpec & " within the " & strDomain & " Domain." & _
 
vbCrlf & "$n will be substituted for the computer name.", _
 
"Command to Execute")
 
End If
 
If strCommand = "" Then WScript.Quit
 
 
 
If strConfirm = "" Then
 
intButton = oShell.Popup("Confirm each command prior to execution?",,_
 
"Confirm?", vbYesNo + vbQuestion)
 
If intButton = vbNo Then
 
boolConfirm = False
 
End If
 
End If
 
End Sub
 
 
 
' Execute a command. Each is always run under a new instance of the command
 
' processor. This allows the use of built-in commands and I/O redirection.
 
'
 
' We won't wait for command completion.
 
Sub Execute(strCommand)
 
Dim RetVal
 
 
 
strCommand = "%COMSPEC% /c " & strCommand
 
 
 
RetVal = oShell.Run(strCommand, 1, False)
 
End Sub

 
Running the Hack
Here is the syntax for running the script:
 
ExexcuteAll.vbs <DomainToTraverse> <ComputerSpecification> <Command> [/Y]
When the script runs, the matched system's name will be substituted for the occurrence of $n in the command to be performed. By default, each command instance is confirmed before it is executed, but you can specify /Y to always answer Yes instead.
 
Here's an example of how to run the script:
 
ExexcuteAll.vbs MYDOMAIN WKSATL* "del \\$n\admin$\activitylog.txt"
This example traverses the MYDOMAIN domain, looking for computer names that start with WKSATL* (note the wildcard) and deletes the activitylog.txt file from the C:\Winnt folder.
 
—Hans Schefske
 

Всего записей: 718 | Зарегистр. 22-12-2002 | Отправлено: 15:38 26-04-2005 | Исправлено: Jovanotti, 16:19 27-04-2005
   

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » В помощь системному администратору » Автоматизация администрирования
emx (13-12-2006 21:07): http://forum.ru-board.com/topic.cgi?forum=8&topic=19088#1


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru