Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using named arguments 1

Status
Not open for further replies.

rlee111

Technical User
Sep 10, 2001
27
0
0
GB
Hi

I have a script to move users to ou's using command line arguments, I run it like this scriptmane.vbs ouname users, because I am using unamed arguments it has to be done in that order. Here is the script

'Move users to OU using command line arguments

On Error Resume Next

Set objArgs = Wscript.Arguments

strOU = objArgs(0)

For I = 0 To objArgs.Count - 1
strUser = objArgs(I)
f_user = "LDAP://cn=" & strUser & ",cn=users,dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
f_ou = "LDAP://ou=" & strOU & ",dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
Err.Clear
Set objOU = GetObject(f_ou)
If Err Then
AdsiErr()
Else
Err.Clear
objou.MoveHere f_user, vbNullString
If Err Then
AdsiErr()
Else
Wscript.Echo "User " & strUser & " Has been added to the OU " & strOU
End If
End If
Next


Sub AdsiErr()

If Err.Number = &h80072030 Then
Wscript.Echo "Please check OU Name or User Name that object does not exist"
On Error Resume Next
Else
e = Hex(Err.Number)
Wscript.Echo Err.Number & " " & e & " " & Err.Description
Wscript.Quit
End If

End Sub

I want to use named arguments so that I can enter the arguments in in any order to save mistakes so I have come up with this

'Move users to OU using command line arguments

On Error Resume Next

Set colNamedArguments = Wscript.Arguments.Named

strOU = colNamedArguments.Item("OU")

'For I = 0 To colNamedArguments.Count - 1
strUser = colNamedArguments.Item("User")
f_user = "LDAP://cn=" & strUser & ",cn=users,dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
f_ou = "LDAP://ou=" & strOU & ",dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
Err.Clear
Set objOU = GetObject(f_ou)
If Err Then
AdsiErr()
Else
Err.Clear
objou.MoveHere f_user, vbNullString
If Err Then
AdsiErr()
Else
Wscript.Echo "User " & strUser & " Has been added to the OU " & strOU
End If
End If
'Next


Sub AdsiErr()

If Err.Number = &h80072030 Then
Wscript.Echo "Please check OU Name or User Name that object does not exist"
On Error Resume Next
Else
e = Hex(Err.Number)
Wscript.Echo Err.Number & " " & e & " " & Err.Description
Wscript.Quit
End If

End Sub

I want to be able to move multiple users like this

Scriptname.vbs /server:servername /user:username /user:username

but as the script stands it only recognises and moves the first user, I have tried to find information but I am stuck, any help would be great

Thanks
Rob
 
>I want to be able to move multiple users
You have to use a delimited string and then split and handle it inside the script. That in a sense is good without introducing superficial versatility in parsing.

[tt]Scriptname.vbs /server:servername /user:"john smith";"bob doe";pdavis;ajohnson[/tt]

Then in the vbscript, do the split.
[tt]
susers=wscript.arguments.named.item("user")
ausers=split(susers,";")
for each user in ausers
if trim(user)="" then
'do nothing-spurious entry
else
'do the real thing
end if
next
[/tt]
The choice of separator ";" is completely arbitrary and one can choose whatever symbol as long as collision with the real arguments can be avoided.

 
Hi I have tried what you have suggested but I get an error at this line in the script:

objou.MoveHere f_user, vbNullString

The error is 'an invalid dn syntax has been specified'

I am running the script like this

scriptname.vbs /ou:testou8 /user:testuser1;testuser2



 
Just to avoid misunderstanding, your script should look like this.
[tt]
On Error Resume Next

Set colNamedArguments = Wscript.Arguments.Named

strOU = colNamedArguments.Item("OU")

for each sUser in split(colNamedArguments.Item("User"),";")
if trim(sUser)=="" then
'do nothing
else
strUser=trim(sUser)
f_user = "LDAP://cn=" & strUser & ",cn=users,dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
f_ou = "LDAP://ou=" & strOU & ",dc=rldom,dc=modnetwork,dc=vernons,dc=biz"
Err.Clear
Set objOU = GetObject(f_ou)
If Err Then
AdsiErr()
Else
'Err.Clear 'no need
objou.MoveHere f_user, vbNullString
If Err Then
AdsiErr()
Else
Wscript.Echo "User " & strUser & " Has been added to the OU " & strOU
End If
End If
end if
next
[/tt]
Then after the above, I look at your AdsiErr(), there may need some revision despite the multi-user implementation.
[tt]
Sub AdsiErr()

If Err.Number = &h80072030 Then
Wscript.Echo "Please check OU Name or User Name that object does not exist"
'On Error Resume Next 'not appropriate
err.clear 'do this instead
Else
e = Hex(Err.Number)
Wscript.Echo Err.Number & " " & e & " " & Err.Description
Wscript.Quit 'can do but sort of dirty exit
End If

End Sub
[/tt]
 
Thankyou Tsuji

Spot on, and also thanks for pointing out mistakes etc..

Regards

Rob
 
Thanks rlee. Upon reading back, I had a typo up there.
[tt] if trim(sUser)[red]=[/red]"" then[/tt]
(doing jscript a bit too much recently)
 
Hi tsuji

Just a quick question, what does 'if trim(sUser)="" then' do is it just checking for no user entered?

Thanks

Rob
 
>what does 'if trim(sUser)="" then
It just tries to eliminate a possible habit in that case like entering
[tt] /user:"john smith";"bob doe";pdavis;ajohnson[red];[/red][/tt]
It is just an illustration of possible manual input problem and an attempt to salvage some unavoidable mix-up. You can either elaborate it or just discard it. It's all up to you.

The same is the use of trim. It is not a must=have. It tries to eliminate problems like
[tt] /user:"john smith";"bob doe";[highlight] [/highlight]pdavis;[highlight] [/highlight]ajohnson[/tt]

 
Correction:

The last example is simply a bad example. In that case, the last two entries will be lost altogether. A better illustration would be.
[tt] /user:"john smith";"[highlight] [/highlight]bob doe";pdavis;ajohnson[/tt]

I make the thing up when I draft the post. Again it is not a must-have measure.
 
Ok I understand now thanks once again

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top