From: becky on
It's supposed to just read from a file with a list of hostnames, read
from a file with a list of
account names, loop through them, create the accounts on each host and
add that account to local admin (these are all local accounts). I'm a
bit new to VBScript, so I'm not sure what I'm doing wrong, when I
debug, it appears to pass the variables and parse the files, but it
just will not loop through all the host names, it does create the
accounts and add them to the admin group, but on one host only.

Here's the script, the first part just pings the hosts in the text
file to make sure they are up.:

hostnames = "C:\scripts\hostnames.txt"
hostlog = "c:\scripts\hostlogs.txt"
accountnames = "c:\scripts\accounts.txt"
'strPassword = inputbox("Enter Password")
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objHostFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set objOutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objHostFile.atEndofStream
strComputer = objHostFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus In objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
objOutputFile.WriteLine strComputer
End If
Next
Loop
objHostFile.Close
WScript.Echo "All Done Pinging"

Set objAccountFile = objFSO.OpenTextFile(accountnames, ForReading,
True)
strpassword = InputBox ("What is the password?")
InputHostName = "c:\scripts\hostlogs.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputHosts = objFSO.OpenTextFile(InputHostName, ForReading,
True)
strComputers = objInputHosts.ReadAll
arrComputers = Split(strComputers, vbCrLf)
strAccounts = objAccountFile.ReadAll
arrAccounts = Split(strAccounts, vbCrLf)
objInputHosts.Close
objAccountFile.Close
For Each strComputer In arrComputers
Set colAccounts = GetObject("WinNT://" & strComputer & "")
For Each strAccount In arrAccounts
Set objUser = colAccounts.Create("user", strAccount)
objUser.SetPassword strPassword
objUser.SetInfo
Set objGroup = GetObject("WinNT://" & strComputer & "/
Administrators")
Set objUser = GetObject("WinNT://" & strAccount)
objGroup.Add(objUser.ADsPath)
WScript.Echo "Added account " & strAccount & " " strComputer
Next
Next
WScript.Echo "All Done!"

From: Pegasus [MVP] on


"becky" <ladymcse2000(a)gmail.com> wrote in message
news:b444eb4c-fac1-43a3-b0d2-d209b108ad4c(a)n8g2000prh.googlegroups.com...
> It's supposed to just read from a file with a list of hostnames, read
> from a file with a list of
> account names, loop through them, create the accounts on each host and
> add that account to local admin (these are all local accounts). I'm a
> bit new to VBScript, so I'm not sure what I'm doing wrong, when I
> debug, it appears to pass the variables and parse the files, but it
> just will not loop through all the host names, it does create the
> accounts and add them to the admin group, but on one host only.
>
> Here's the script, the first part just pings the hosts in the text
> file to make sure they are up.:
>
> hostnames = "C:\scripts\hostnames.txt"
> hostlog = "c:\scripts\hostlogs.txt"
> accountnames = "c:\scripts\accounts.txt"
> 'strPassword = inputbox("Enter Password")
> Const ForWriting = 2
> Const ForReading = 1
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objHostFile = objFSO.Opentextfile(hostnames, ForReading, True)
> Set objOutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
> Do Until objHostFile.atEndofStream
> strComputer = objHostFile.ReadLine
> Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
> ExecQuery("select * from Win32_PingStatus where address = '"_
> & strComputer & "'")
> For Each objStatus In objPing
> If objStatus.StatusCode = 0 Then
> WScript.Echo strComputer & " is reachable"
> objOutputFile.WriteLine strComputer
> End If
> Next
> Loop
> objHostFile.Close
> WScript.Echo "All Done Pinging"
>
> Set objAccountFile = objFSO.OpenTextFile(accountnames, ForReading,
> True)
> strpassword = InputBox ("What is the password?")
> InputHostName = "c:\scripts\hostlogs.txt"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objInputHosts = objFSO.OpenTextFile(InputHostName, ForReading,
> True)
> strComputers = objInputHosts.ReadAll
> arrComputers = Split(strComputers, vbCrLf)
> strAccounts = objAccountFile.ReadAll
> arrAccounts = Split(strAccounts, vbCrLf)
> objInputHosts.Close
> objAccountFile.Close
> For Each strComputer In arrComputers
> Set colAccounts = GetObject("WinNT://" & strComputer & "")
> For Each strAccount In arrAccounts
> Set objUser = colAccounts.Create("user", strAccount)
> objUser.SetPassword strPassword
> objUser.SetInfo
> Set objGroup = GetObject("WinNT://" & strComputer & "/
> Administrators")
> Set objUser = GetObject("WinNT://" & strAccount)
> objGroup.Add(objUser.ADsPath)
> WScript.Echo "Added account " & strAccount & " " strComputer
> Next
> Next
> WScript.Echo "All Done!"
>

Your code suffers from some structural problems. Your current approach works
like this:
1. You read the list of machines
2. You ping each machine
3. You record the successful pings in a text file
4. You create accounts on each machine whose name you find in the text file

A more structured method would go like this:
1. You read the list of machines
2. You ping the current machine
3. If successful, you create accounts on the current machine
4. Continue with Step 2 for the next machine name

Here is how you could implement this approach. Note the use of functions and
subroutines for improved structure. There is a simple main routine and the
actual work is performed in tightly defined subroutines or functions.

'------------------------------------------
'Create a number of accounts on several PCs
'------------------------------------------
hostnames = "d:\hostnames.txt"
accountnames = "c:\scripts\accounts.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")

aAccountNames = Split(objFSO.OpenTextFile(AccountNames).ReadAll, VbCrLf)
aHostNames = Split(objFSO.OpenTextFile(hostnames).ReadAll, VbCrLf)
For Each sHost In aHostNames
if bPing(sHost) then CreateAccounts(sHost)
Next

'---------------
'Create accounts
'---------------
Sub CreateAccounts (sName)
Set objAccountFile = objFSO.OpenTextFile(accountnames, ForReading,True)
strpassword = InputBox ("What is the password?")
Set colAccounts = GetObject("WinNT://" & sName & "")
For Each strAccount In aAccounts
Set objUser = colAccounts.Create("user", strAccount)
objUser.SetPassword strpassword
objUser.SetInfo
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://" & strAccount)
objGroup.Add(objUser.ADsPath)
WScript.Echo "Added account " & strAccount & " " & strComputer
Next
End Sub
'---------------
'Ping a PC
'---------------
Function bPing (sName)
Set objPing =
GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select *
from Win32_PingStatus where address = '" & sName & "'")
For Each objStatus In objPing
bPing = (objStatus.StatusCode = 0)
Next
End Function


From: Pegasus [MVP] on


"becky" <ladymcse2000(a)gmail.com> wrote in message
news:b444eb4c-fac1-43a3-b0d2-d209b108ad4c(a)n8g2000prh.googlegroups.com...
> It's supposed to just read from a file with a list of hostnames, read
> from a file with a list of
> account names, loop through them, create the accounts on each host and
> add that account to local admin (these are all local accounts). I'm a
> bit new to VBScript, so I'm not sure what I'm doing wrong, when I
> debug, it appears to pass the variables and parse the files, but it
> just will not loop through all the host names, it does create the
> accounts and add them to the admin group, but on one host only.
>
> Here's the script, the first part just pings the hosts in the text
> file to make sure they are up.:
>
> hostnames = "C:\scripts\hostnames.txt"
> hostlog = "c:\scripts\hostlogs.txt"
> accountnames = "c:\scripts\accounts.txt"
> 'strPassword = inputbox("Enter Password")
> Const ForWriting = 2
> Const ForReading = 1
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objHostFile = objFSO.Opentextfile(hostnames, ForReading, True)
> Set objOutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
> Do Until objHostFile.atEndofStream
> strComputer = objHostFile.ReadLine
> Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
> ExecQuery("select * from Win32_PingStatus where address = '"_
> & strComputer & "'")
> For Each objStatus In objPing
> If objStatus.StatusCode = 0 Then
> WScript.Echo strComputer & " is reachable"
> objOutputFile.WriteLine strComputer
> End If
> Next
> Loop
> objHostFile.Close
> WScript.Echo "All Done Pinging"
>
> Set objAccountFile = objFSO.OpenTextFile(accountnames, ForReading,
> True)
> strpassword = InputBox ("What is the password?")
> InputHostName = "c:\scripts\hostlogs.txt"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objInputHosts = objFSO.OpenTextFile(InputHostName, ForReading,
> True)
> strComputers = objInputHosts.ReadAll
> arrComputers = Split(strComputers, vbCrLf)
> strAccounts = objAccountFile.ReadAll
> arrAccounts = Split(strAccounts, vbCrLf)
> objInputHosts.Close
> objAccountFile.Close
> For Each strComputer In arrComputers
> Set colAccounts = GetObject("WinNT://" & strComputer & "")
> For Each strAccount In arrAccounts
> Set objUser = colAccounts.Create("user", strAccount)
> objUser.SetPassword strPassword
> objUser.SetInfo
> Set objGroup = GetObject("WinNT://" & strComputer & "/
> Administrators")
> Set objUser = GetObject("WinNT://" & strAccount)
> objGroup.Add(objUser.ADsPath)
> WScript.Echo "Added account " & strAccount & " " strComputer
> Next
> Next
> WScript.Echo "All Done!"

Sorry, there were a couple of incorrect variable names in my first reply.
Here is the marked up version.

'------------------------------------------
'Create a number of accounts on several PCs
'------------------------------------------
hostnames = "c:\scripts\hostnames.txt"
accountnames = "c:\scripts\accounts.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
aAccounts = Split(objFSO.OpenTextFile(AccountNames).ReadAll, VbCrLf)
aHosts = Split(objFSO.OpenTextFile(hostnames).ReadAll, VbCrLf)
For Each sHost In aHosts
if bPing(sHost) then CreateAccounts(sHost)
Next

'---------------
'Create accounts
'---------------
Sub CreateAccounts (sName)
wscript.echo "Creating accounts on", sName
strpassword = InputBox ("What is the password?")
Set colAccounts = GetObject("WinNT://" & sName & "")
For Each strAccount In aAccounts
Set objUser = colAccounts.Create("user", strAccount)
objUser.SetPassword strpassword
objUser.SetInfo
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://" & strAccount)
objGroup.Add(objUser.ADsPath)
WScript.Echo "Added account " & strAccount & " on " & sName
Next
End Sub
'---------------
'Ping a PC
'---------------
Function bPing (sName)
Set objPing =
GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select *
from Win32_PingStatus where address = '" & sName & "'")
For Each objStatus In objPing
bPing = (objStatus.StatusCode = 0)
Next
End Function