IT Bloguje - pomoc pro všechny IT nadšence
VBS

Hromadná změna hesela lokálního administratora počítačů v síti

VBS
Napsal uživatel Ondřej Soukup   
Čtvrtek, 14 Květen 2009

Nedávno jsem na internetu (http://cwashington.netreach.net/depo/view.asp?Index=1130&ScriptType=vbscript) našel krásný script, pomocí něhož je možné hromadně změnit hesla lokálních administrátorů na všech počítačích v síti.

VBS Script pro hromadnou změnu hesela lokálního administrátora počítačů v síti


' LocalAdmin,vbs
' Script to Change Local Admin Password on selected machines in domain
' Usage: Localadmin [RERUN] [SILENT] [TEST]
' RERUN: This option is used after the 1st initial run. Then the script will try to re-do the machines
' that previosuly failed. Rerun can be run as often as you like. It always keeps track of which
' machines are still failing and which have been sucessfully changed
' SILENT: Without this option there will be a popup at the end saying "DONE". If you plan to schedule
' then run it in SILENT mode.
' TEST: In TEST mode the script will generate a computers.txt file (so you can check if filtering worked correctly)
' but it will use a testcomputers.txt file as its input (you need to create that file manually).
' this way you can check on lab pcs etc. if the script works fine and you can still check if filtering works
'
' Note: you have to put your domain name in the variable "strDomain" a few lines down from here
' Send your comments to: 
 Tato emailová adresa je chráněna před spamboty, abyste ji viděli, povolte JavaScript
 


Dim Password
On Error Resume Next

' ++++++++++++++++++++ Change these variables according to your requirement +++++++++++++++++++++
strDomain="YOUR DOMAIN NAME HERE" ' Put your Domain Name Here, I use the NetBios Domain Name
pwlength = 8 ' Set this variable to the number of characters of the required password
' This variable defines what case of characters to use in the password.
CharCase = 1 ' 1 = lower case characters only
' 2 = upper case characters only
' 3 = mixed case characters
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


' Check for command line arguments
Set objArgs = WScript.Arguments

For i=0 to objArgs.count
Select Case ucase(objargs(i))
Case "RERUN" Rerun="RERUN"
Case "SILENT" silent=1
CASE "TEST" test=1
End Select
Next

' RERUN: Previous failed.txt will be used as the input file and a new failed.txt
' will be generated.
' Copy Failed.txt to Rerun.txt
If Rerun ="RERUN" then
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "failed.txt", "rerun.txt"
End If

set oDomain= GetObject("WinNT://"& strDomain)
oDomain.filter = Array("Computer") ' Create filter for computers

' -=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-
' IMPORTANT FILTERING SECTION: Please change to meet your requirement
' -=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-
' SortComp are filter variables. It will only consider computers that start
' with any of the mentioned strings. To use your own filtering rules modify
' the if statements under "Filtering" section

Sortcomp1="wa"
Sortcomp2="wd"
SortComp3="wt"

' Write to failed.txt
set fso1=createobject("scripting.filesystemobject")
set f1=fso1.OpenTextFile ("failed.txt",2, True)

' Append to sucess.txt
set fso2=createobject("scripting.filesystemobject")
set f2=fso2.OpenTextFile ("sucess.txt",8, True)

' Computer names matching search are written here in the 1st run
' If this is a rerun then there is no need to create the computers.txt since
' the previous failed.txt will be used as input
If Rerun"RERUN" then
set fso3=createobject("scripting.filesystemobject")
set f3=fso3.OpenTextFile("computers.txt",2, True) ' This file is opend in writing mode
End if


'Start looping through the machine names in the Domain
'The below IF Statement will look for any computer that starts with one of the above
'strings sortcomp1,2,3
'You could do your own filter rules to get the computers you need

' If This is the 1st Run and not in TEST mode then write all matching computer names to the file computers.txt

If rerun"RERUN" THEN

for each computer in oDomain
' -------- Filtering Statements ------
' Change the Filtering Rules to whatever meets your requirement
' This Filtering rule will include computers only that start with any
' of the text strings defined by Sortcomp1, 2, 3

If mid(ucase(computer.name),1,len(sortcomp1)) = ucase(sortcomp1)or _
mid(ucase(computer.name),1,len(sortcomp2)) = ucase(sortcomp2) or _
mid(ucase(computer.name),1,len(sortcomp3)) = ucase(sortcomp3) _
then
f3.writeline Computer.Name
END IF
Next

END IF

f3.close


'if this is a rerun we have to process the computers listed in failed.txt
'else it's a 1st run and we need to work on computers.txt

IF rerun="RERUN" then

set fso4=createobject("scripting.filesystemobject")
set f4=fso4.OpenTextFile("rerun.txt",1, True) ' This file is opend for reading

Else

set fso4=createobject("scripting.filesystemobject")

if test=0 then set f4=fso4.OpenTextFile("computers.txt",1, True) ' This file is opend in reading
if test=1 then set f4=fso4.OpenTextFile("testcomputers.txt",1, True) ' This file is opend in reading

End if

Do While f4.AtEndOfStream True

CompName = trim(f4.readline)
set usr = GetObject("WinNT://" & CompName & "/administrator,user")

If Err Then ' if there is an error then save failed computer name to failed.txt and skip changing pw
HandleErr()
Wscript "Error Computer Name: " & CompName
Err.Clear
Else
password = PasswordGen ' Generate Random Password
usr.SetPassword password
usr.SetInfo
' Log Computer Name / Password to sucess.txt
f2.Writeline CompName & ", " & password
End IF
Loop

f1.close
f2.close
f3.close
f4.close


If silent1 then wscript.echo "Done!"

wscript.quit(0)


' Save failed computer names in the file failed.txt
Sub HandleErr()
f1.WriteLine CompName
End Sub


Function PasswordGen()
'***** Variables *****

' Dim Password
Dim RandomPassword


' this is the array of characters used
' to generate the random passwords

Dim array_cod(62)
array_cod(0) = "0"
array_cod(1) = "1"
array_cod(2) = "2"
array_cod(3) = "3"
array_cod(4) = "4"
array_cod(5) = "5"
array_cod(6) = "6"
array_cod(7) = "7"
array_cod(8) = "8"
array_cod(9) = "9"
array_cod(10) = "a"
array_cod(11) = "b"
array_cod(12) = "c"
array_cod(13) = "d"
array_cod(14) = "e"
array_cod(15) = "f"
array_cod(16) = "g"
array_cod(17) = "h"
array_cod(18) = "i"
array_cod(19) = "j"
array_cod(20) = "k"
array_cod(21) = "l"
array_cod(22) = "m"
array_cod(23) = "n"
array_cod(24) = "o"
array_cod(25) = "p"
array_cod(26) = "q"
array_cod(27) = "r"
array_cod(28) = "s"
array_cod(29) = "t"
array_cod(30) = "u"
array_cod(31) = "v"
array_cod(32) = "w"
array_cod(33) = "x"
array_cod(34) = "y"
array_cod(36) = "z"
array_cod(37) = "A"
array_cod(38) = "B"
array_cod(39) = "C"
array_cod(40) = "D"
array_cod(41) = "E"
array_cod(42) = "F"
array_cod(43) = "G"
array_cod(44) = "H"
array_cod(45) = "I"
array_cod(46) = "J"
array_cod(47) = "K"
array_cod(48) = "L"
array_cod(49) = "M"
array_cod(50) = "N"
array_cod(51) = "O"
array_cod(52) = "P"
array_cod(53) = "Q"
array_cod(54) = "R"
array_cod(55) = "S"
array_cod(56) = "T"
array_cod(57) = "U"
array_cod(58) = "V"
array_cod(59) = "W"
array_cod(60) = "X"
array_cod(61) = "Y"
array_cod(62) = "Z"

for cod=0 to pwlength -1 ' generate a pwlength characters random password
MaxCod = 62 ' Upper bound of the array
MinCod = 0 ' Lower bound of the array
Randomize ' Initialize the randomizer

' generate the password character by character until it has 10 characters, and convert it to a string
RandomPassword = CStr(RandomPassword & array_cod(Int(Rnd * MaxCod) - MinCod))
next

' Change pw case to 1 = lower only, 2= Upper only, 3= Mixed
Select Case CharCase
Case 1 PasswordGen = lcase(RandomPassword)
Case 2 PasswordGen = ucase(RandomPassword)
Case 3 PasswordGen = RandomPassword
End Select

End Function

 

 
Potřebujete pomoc?

V případě problémů, či žádosti o radu je možné se kdykoliv přímo obrátit na naše konzultanty:

GAUZY, s.r.o.
 

Ondřej Soukup
Solution Consultant
Tel.: +420 224 400 013
Mob.: +420 775 142 899
E-mial: soukup@gauzy.cz