Ldap – VBS Scripting – Access LDAP User object with samAccountName

active-directoryldapvbscript

I am trying to write a VBScript that meets 2 requirements:

  1. It unlocks a user's account.
  2. It can do so and reference the user using the samAccountName.

#1 works. However, the below script I have gotten working only references the user with their full AD name.

' UnlockUserAccount.vbs
Option Explicit

'Get the arguments
dim oArgs, strUser, strContainer
set oArgs = WScript.Arguments
strUser = "CN=" & trim(oArgs(0)) & "," 
strContainer = "OU=User Accounts,OU=Staff,OU=Org," 

' Bind to Active Directory and get the user object
dim objRootLDAP, objUser
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objUser = GetObject("LDAP://" & strUser & strContainer & objRootLDAP.Get("defaultNamingContext"))

'Unlock the user's account
objUser.IsAccountLocked = False
objUser.SetInfo

Wscript.Quit(1)

For example, suppose we have user 'bsmith', whose full name is Bill Smith.

I can only call this script and have it work by passing in "Bill Smithi" as the user.

How do I reference the user passing in "bsmith"? I can't figure this out.

Best Answer

Given that VBS is nearly obsolete, I recommend you try this in PowerShell. It will be nearly trivial using get-aduser and set-aduser. Try this:

 get-aduser bsmith |Unlock-ADAccount