views:

1346

answers:

2

Hi,

I'm using this code to create user account,

Function CreateUserAccount (acc, paas)

 Dim WinUserAccountName, WinUserAccountPass

 WinUserAccountName = Session.Property("WIN_USER_ACCOUNT")
 WinUserAccountPass = Session.Property("WIN_USER_PASS")

 strComputer = "."
 set objSystem = GetObject("WinNT://" & strComputer)
 set objUser = objSystem.Create("user", WinUserAccountName)
 objUser.SetPassword WinUserAccountPass
 objUser.SetInfo

End Function

It works OK so far, the only thing I need to implement is:

  1. When the user name is already in the machine. Display message to the user telling him/her that this user name is already exist.

How can I do that in VBScript? Is there error code I can catch?

Thanks,

+1  A: 

OK, I found the script I was looking for:

QueryForUser("kenmyer")
Wscript.Echo "This user account does not exist."

Sub QueryForUser(strUserName)
    strComputer = "."
    Set objDomain = GetObject("WinNT://" & strComputer)
    objDomain.Filter = Array("user")
    For Each User In objDomain
        If lcase(User.Name) = lcase(strUserName) Then
            WScript.Echo User.Name & " already exists."
            WScript.Quit
        End If    
    Next
End Sub
A: 

QueryForUser("kenmyer") Wscript.Echo "This user account does not exist."

Sub QueryForUser(strUserName) strComputer = "." Set objDomain = GetObject("WinNT://" & strComputer) objDomain.Filter = Array("user") For Each User In objDomain If lcase(User.Name) = lcase(strUserName) Then WScript.Echo User.Name & " already exists." WScript.Quit End If
Next End Sub