Home All Groups Group Topic Archive Search About

Create Local Administrator

Author
10 Feb 2006 1:22 PM
Philip Wagenaar
What is the best way to create a local user on the machine with administrator
rights?

I have problems with my code on machines that have password policy. The
problem is that when the user is created there is no password, and password
policy is set to min 7 chars or something. So the code only works if I turn
of group policy. Can I create a user with pass (and even add him to
administrators group) in one line/action? My code:

Private Sub CreateUser(ByVal User As String)
        'Create Account
        Try
            Dim strUser As String = User
            Dim oDomain As Object = GetObject("WinNT://" + ComputerName)
            Dim oUser As Object = oDomain.Create("user", strUser)
            If (Err.Number = 0) Then
                oUser.SetInfo()
                oUser.SetPassword(Password)
                oUser.SetInfo()
                SaveLog("Created user " & User)
            Else
                SaveLog("Unable to create user " & User)
                Diagnostics.EventLog.WriteEntry("Rename Tool", "Unable to
create user " & User & " with password " & Password,
EventLogEntryType.Warning)
            End If
        Catch ex As Exception
            SaveLog("Unable to create user " & User & ". More info: " &
ex.Message)
            Diagnostics.EventLog.WriteEntry("Rename Tool", "Unable to create
user " & User & " with password " & Password & ".More info: " & ex.Message,
EventLogEntryType.Warning)
        End Try

    End Sub

Author
13 Feb 2006 3:28 AM
Yuan Ren[MSFT]
Hi Philip,

Thanks for posting!

At the current stage, your code is appropriate for create a local user.

>"I have problems with my code on machines that have password policy."

As far as I know, if you have joined the domain, you are restricted with
the domain policy. This is why the current issue is encountered. So, I
think you need to follow the policy if you don't quit from the current
domain.

Thanks for your understanding!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
Author
13 Feb 2006 8:39 AM
Philip Wagenaar
But I want to create a user with a password that matches the password policy.
But I cannot create the user and set its password in one action. That is why
I need to disable the passwordpolicy and thus my question how to create a
user and set it's password in action.


""Yuan Ren[MSFT]"" wrote:

Show quoteHide quote
> Hi Philip,
>
> Thanks for posting!
>
> At the current stage, your code is appropriate for create a local user.
>
> >"I have problems with my code on machines that have password policy."
>
> As far as I know, if you have joined the domain, you are restricted with
> the domain policy. This is why the current issue is encountered. So, I
> think you need to follow the policy if you don't quit from the current
> domain.
>
> Thanks for your understanding!
>
> Regards,
>
> Yuan Ren [MSFT]
> Microsoft Online Support
>
>
Author
13 Feb 2006 8:59 AM
Yuan Ren[MSFT]
Hi Philip,

Thanks for your reply!

I'm sorry for misunderstanding. But I wonder your meaning is the
SetPassword doesn't work in the current application. If this is true, could
you please supply more details? This means some error message or event log.
Thanks for your understanding.

I'm looking forward your reply!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
Author
13 Feb 2006 1:17 PM
Philip Wagenaar
I do not know the 'exact' error message. But when I execute the code:

Dim oUser As Object = oDomain.Create("user", strUser)

I catch an exception that users password does not match the policy.

This makes sense because the user is first created and then password is set.
But because the policy enforces 7 char password (for example) the creation of
the user fails and I never even get a chance to set the password for the
user. Thus my question if I can create a user AND set the password in one
'call' to the system.

I will try to get the exact error message if need.




""Yuan Ren[MSFT]"" wrote:

Show quoteHide quote
> Hi Philip,
>
> Thanks for your reply!
>
> I'm sorry for misunderstanding. But I wonder your meaning is the
> SetPassword doesn't work in the current application. If this is true, could
> you please supply more details? This means some error message or event log.
> Thanks for your understanding.
>
> I'm looking forward your reply!
>
> Regards,
>
> Yuan Ren [MSFT]
> Microsoft Online Support
>
>
Author
13 Feb 2006 1:41 PM
Philip Wagenaar
I set my local security policy min. password length to seven and ran the code
again and I receive this exact error (i tried to create the user "dus"):

Unable to create user dus with password The password does not meet the
password policy requirements. Check the minimum password length, password
complexity and password history requirements. (Exception from HRESULT:
0x800708C5)

When I set the min. password length to 0 the code works fine. Once more the
code to create the user:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        CreateUser("dus")
    End Sub

    Private Sub CreateUser(ByVal User As String)
        'Create Account
        Try
            Dim strUser As String = User
            Dim oDomain As Object = GetObject("WinNT://" +
Environment.MachineName)
            Dim oUser As Object = oDomain.Create("user", strUser)
            If (Err.Number = 0) Then
                oUser.SetInfo()
                oUser.SetPassword("Nrg2005")
                oUser.SetInfo()
                'SaveLog("Created user " & User)
            Else
                'SaveLog("Unable to create user " & User)
                Diagnostics.EventLog.WriteEntry("Rename Tool", "Unable to
create user " & User & " with password ", EventLogEntryType.Warning)
            End If
        Catch ex As Exception
            'SaveLog("Unable to create user " & User & ". More info: " &
ex.Message)
            Diagnostics.EventLog.WriteEntry("Rename Tool", "Unable to create
user " & User & " with password " & ex.Message, EventLogEntryType.Warning)
        End Try

    End Su

""Yuan Ren[MSFT]"" wrote:

Show quoteHide quote
> Hi Philip,
>
> Thanks for your reply!
>
> I'm sorry for misunderstanding. But I wonder your meaning is the
> SetPassword doesn't work in the current application. If this is true, could
> you please supply more details? This means some error message or event log.
> Thanks for your understanding.
>
> I'm looking forward your reply!
>
> Regards,
>
> Yuan Ren [MSFT]
> Microsoft Online Support
>
>
Author
14 Feb 2006 7:16 AM
Yuan Ren[MSFT]
Hi Philip,

Thanks for your reply!

I'm sorry for careless about the current issue. In the code as below,
please remove the first setinfo method and try to run again:
If (Err.Number = 0) Then
'oUser.SetInfo() Please remove this one
oUser.SetPassword("Nrg2005")
oUser.SetInfo()

After my performing, the user has been created on my machine.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support