Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Have a file with Machine name and Ntlogin. Need to add the users in the local Administrator group of the machine.

Hi,

Have a file with Machine name and Ntlogin. Need to add the users in the local Administrator group of the machine.
For each machine with the user next to him.

File as this

Machine name,Username/NT Login

Is there a way to run a script and get this done remotely.

Regards
Sharath
0
bsharath
Asked:
bsharath
  • 37
  • 24
1 Solution
 
sirbountyCommented:
This should do it...
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=arrValue(0)
  User=arrValue(1)
  AddUserToAdmin (User, PC)
Next
 
wscript.quit
 
Sub AddUserToAdmin (strUser, strPC)
  Dim objUser : Set objUser = GetObject("WinNT://" & strPC & "/" strUser)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      10
Char:      28
Error:      Cannot use parentheses when calling a Sub
Code:      800A0414
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      10
Char:      28
Error:      Cannot use parentheses when calling a Sub
Code:      800A0414
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sirbountyCommented:
Change line 10 from this:
  AddUserToAdmin (User, PC)
to this:
  AddUserToAdmin User, PC
0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      16
Char:      66
Error:      Expected ')'
Code:      800A03EE
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      16
Char:      66
Error:      Expected ')'
Code:      800A03EE
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
What does the message reveal in this version?
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strUser, strPC)
  wscript.echo "Adding " & strUser & " to Administrators group on " & strPC
  Dim objUser : Set objUser = GetObject("WinNT://" & strPC & "/" strUser)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I getb this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      66
Error:      Expected ')'
Code:      800A03EE
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
I getb this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      66
Error:      Expected ')'
Code:      800A03EE
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
Odd...not sure what ')' it's expecting.
If this version doesn't work - comment out that line (should be line 18 now...starts with Dim objUser...by placing a ' at the start of it)
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strUser, strPC)
  wscript.echo "Adding " & strUser & " to Administrators group on " & strPC
  UserPath=strPC & "/" & strUser
  Dim objUser : Set objUser = GetObject("WinNT://" & UserPath)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
Now i get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      9
Char:      3
Error:      Subscript out of range: '[number: 1]'
Code:      800A0009
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
Looks to be the input file...
Presumably it's laid out like
ComputerName,UserName
correct?
0
 
bsharathAuthor Commented:
Sorry have a ; instead of a ,

Now i get this

---------------------------
Windows Script Host
---------------------------
Adding Echj to Administrators group on Dev-mrd01
---------------------------
OK  
---------------------------


Then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      18
Char:      17
Error:      The group name could not be found.
Code:      800708AC
Source:       (null)

---------------------------
OK  
---------------------------

0
 
bsharathAuthor Commented:
Sorry have a ; instead of a ,

Now i get this

---------------------------
Windows Script Host
---------------------------
Adding Echj to Administrators group on Dev-mrd01
---------------------------
OK  
---------------------------


Then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      18
Char:      17
Error:      The group name could not be found.
Code:      800708AC
Source:       (null)

---------------------------
OK  
---------------------------

0
 
sirbountyCommented:
I think the user/pc are off...I was using user,pc - but it looks like you said it was pc,user?Sorry - it's late here...if this still fails to work, check that the Administrators group exists as that name on the target device.
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin PC, User
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  UserPath=strPC & "/" & strUser
  Dim objUser : Set objUser = GetObject("WinNT://" & UserPath)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      17
Error:      Unspecified error
Code:      80004005
Source:       (null)

---------------------------
OK  
---------------------------

Should i use it as
USer,pC?
0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      17
Error:      Unspecified error
Code:      80004005
Source:       (null)

---------------------------
OK  
---------------------------

Should i use it as
USer,pC?
0
 
sirbountyCommented:
No I corrected it in the code - should be
pc,user

To reduce the space, I'll just update the subroutine...try replacing the entire sub at the bottom with this one:
It should pop up a box that says

Dev-mrd01/Echj

Does that user already exist on the target machine?  Or are you trying to add a domain user?



Sub AddUserToAdmin (strPC, strUser)
  UserPath=strPC & "/" & strUser
msgbox UserPath
  Dim objUser : Set objUser = GetObject("WinNT://" & UserPath)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------

---------------------------
Echj/Dev-mrd01
---------------------------
OK  
---------------------------

But get this later

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      18
Char:      17
Error:      Unspecified error
Code:      80004005
Source:       (null)

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
I get this

---------------------------

---------------------------
Echj/Dev-mrd01
---------------------------
OK  
---------------------------

But get this later

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      18
Char:      17
Error:      Unspecified error
Code:      80004005
Source:       (null)

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
Hmm..something wrong with the add routine...another test:

Sub AddUserToAdmin (strPC, strUser)
  UserPath=strPC & "/" & strUser
  Dim objUser : Set objUser = GetObject("WinNT://" & UserPath)
msgbox objUser.Name & vbNewLine & objUser.ADsPath
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
msgbox objAdmin.Name
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      17
Error:      Unspecified error
Code:      80004005
Source:       (null)

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
no popups at all?
Does this user account already exist on the target device?
0
 
bsharathAuthor Commented:
No popup's just the error.
No the user is not a member of Administrators group...
0
 
sirbountyCommented:
Right, but the user you're trying to add...is that user already defined on the target PC, or is this supposed to be a domain account?
0
 
bsharathAuthor Commented:
The user i am trying to add is a Domain Account. I have running from an account with Domain Admin permissions on to a Remote machine name in the file with the Domain User name.

0
 
bsharathAuthor Commented:
The user i am trying to add is a Domain Account. I have running from an account with Domain Admin permissions on to a Remote machine name in the file with the Domain User name.

0
 
sirbountyCommented:
That changes things...we're using the WinNT provider to do this, so we either have to switch to LDAP, or you might be able to get by with this change:

Note the change to the objUser path - you'll need to enter your actual domain name there...
Sub AddUserToAdmin (strPC, strUser)
  Dim objUser : Set objUser = GetObject("WinNT://YourDomain/" & strUser)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------
 
I have the txt file as

Machine name,Username/NT Login
0
 
sirbountyCommented:
What do you mean by Username/NT Login
Can you post a line or two from your file?
What's your domain name?
0
 
bsharathAuthor Commented:
Domain Name " Development"

I Have this in the file
Dev-mrd01,SharathK
Inside,Sharathk
Dev-mrd01,Echj
0
 
bsharathAuthor Commented:
Domain Name " Development"

I Have this in the file
Dev-mrd01,SharathK
Inside,Sharathk
Dev-mrd01,Echj
0
 
sirbountyCommented:
Full script...let me know...
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      17
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------

I have 5 machine names with users and all machines and NTlogins are live...
0
 
sirbountyCommented:
So, you can successfully ping Dev-mrd01?
0
 
bsharathAuthor Commented:
Yes i can
0
 
sirbountyCommented:
Odd - try moving another line up top in your file, just for testing.  And you do have access to these PCs as an Admin, correct?

Other than that, try this version...doubt it'll yield much different though.
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      18
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
That looks like it's having trouble finding the group Administrators...the line before seems to have gotten to the PC okay.
Can you verify the Admin group name is Administrators and exists on that device?
0
 
bsharathAuthor Commented:
Yes The group name is Administrators and it exists...
0
 
sirbountyCommented:
Tested this further on a couple of my work computers with no problem.

Line 17 appears to be working, so the computer is being discovered.  Perhaps the adspath is incorrect?

Change these lines - I dropped a new line in between them:


  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
Msgbox objPC.AdsPath & "/Administrators"
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")

I'd like to know what pop up message you get.
0
 
bsharathAuthor Commented:
First got this
---------------------------

---------------------------
WinNT://Echj/Administrators
---------------------------
OK  
---------------------------

Then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      19
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
First got this
---------------------------

---------------------------
WinNT://Echj/Administrators
---------------------------
OK  
---------------------------

Then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      19
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
I've been unable to reproduce this - it works on my network with 5 different devices.
Can you try reordering your input list and see if it's just a problem with that one device?
0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      19
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------


Full code as in snippet

The txt file is as

Machinename,Username
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
Msgbox objPC.AdsPath & "/Administrators"
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      19
Char:      18
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------


Full code as in snippet

The txt file is as

Machinename,Username
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin User, PC
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
Msgbox objPC.AdsPath & "/Administrators"
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
sirbountyCommented:
Sorry Sharath, please try this one...
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  arrValue = Split(item,",")
  PC=Trim(arrValue(0))
  User=Trim(arrValue(1))
  AddUserToAdmin PC, User
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  On Error Resume Next
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  If Err.Number <> 0 Then
    wscript.echo "An error has occured:" & vbNewLine & Err.Description
  End If
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
Sorry for the delay...
I get this...

a box
An error has occured then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      8
Char:      3
Error:      Subscript out of range: '[number: 0]'
Code:      800A0009
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
Sorry for the delay...
I get this...

a box
An error has occured then this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add Users to Groups in Machines.vbs
Line:      8
Char:      3
Error:      Subscript out of range: '[number: 0]'
Code:      800A0009
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
Apology unnecessary.
That would indicate that there is no PC in the first field...this should test for empty lines:
SourceFile = "C:\SourceFile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  If item <> "" Then
    arrValue = Split(item,",")
    PC=Trim(arrValue(0))
    User=Trim(arrValue(1))
    AddUserToAdmin PC, User
  End If
Next
 
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  On Error Resume Next
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  If Err.Number <> 0 Then
    wscript.echo "An error has occured:" & vbNewLine & Err.Description
  End If
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I just get this

---------------------------
Windows Script Host
---------------------------
An error has occured:


---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
I just get this

---------------------------
Windows Script Host
---------------------------
An error has occured:


---------------------------
OK  
---------------------------
0
 
sirbountyCommented:
If you click OK, do any of them get added?  That's a bypassable error...
0
 
bsharathAuthor Commented:
You are right now it add's the use but get that error....

Can i get a done button or a success/failure results file.
0
 
bsharathAuthor Commented:
You are right now it add's the use but get that error....

Can i get a done button or a success/failure results file.
0
 
sirbountyCommented:
Run the script with

cscript "C:\Add Users to Groups in Machines.vbs"
SourceFile = "C:\SourceFile.txt"
OutputFile = "C:\AdminAddResults.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLog : Set objLog = objFSO.CreateTextFile(OutputFile)
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  If item <> "" Then
    arrValue = Split(item,",")
    PC=Trim(arrValue(0))
    User=Trim(arrValue(1))
    objLog.WriteLine "Attempting to add " & User & " to Admin group on " & PC
    AddUserToAdmin PC, User
  End If
Next
 
objLog.Close
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  On Error Resume Next
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  If Err.Number <> 0 Then
    objLog.WriteLine vbTab "An error has occured:  " & Err.Description
  Else
    objLog.WriteLine vbTab & "...added successfully."
  End If
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
I get this

C:\>cscript "C:\Add Users to Groups in Machines.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Add Users to Groups in Machines.vbs(29, 28) Microsoft VBScript compilation error: Expected end of statement
0
 
bsharathAuthor Commented:
I get this

C:\>cscript "C:\Add Users to Groups in Machines.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Add Users to Groups in Machines.vbs(29, 28) Microsoft VBScript compilation error: Expected end of statement
0
 
sirbountyCommented:
Sorry about that...
SourceFile = "C:\SourceFile.txt"
OutputFile = "C:\AdminAddResults.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLog : Set objLog = objFSO.CreateTextFile(OutputFile)
 
arrData = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
 
For each item in arrData
  If item <> "" Then
    arrValue = Split(item,",")
    PC=Trim(arrValue(0))
    User=Trim(arrValue(1))
    objLog.WriteLine "Attempting to add " & User & " to Admin group on " & PC
    AddUserToAdmin PC, User
  End If
Next
 
objLog.Close
wscript.quit
 
Sub AddUserToAdmin (strPC, strUser)
  On Error Resume Next
  Dim objUser : Set objUser = GetObject("WinNT://Development/" & strUser)
  Dim objPC : Set objPC = GetObject("WinNT://" & strPC)
  Dim objAdmin : Set objAdmin = GetObject(objPC.ADsPath & "/Administrators")  
 
  objAdmin.Add (objUser.ADsPath)
  If Err.Number <> 0 Then
    objLog.WriteLine vbTab & "An error has occured:  " & Err.Description
  Else
    objLog.WriteLine vbTab & "...added successfully."
  End If
  Set objAdmin = Nothing
  Set objPC = Nothing
  Set objUser = Nothing
End Sub

Open in new window

0
 
sirbountyCommented:
Glad it's working for you - thanx for the grade! :^)
0
 
bsharathAuthor Commented:
Thank U very much... Very useful one for me... :-))
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 37
  • 24
Tackle projects and never again get stuck behind a technical roadblock.
Join Now