Question Set Password Never Expire Local Machine User (Not Active Directory User)

WellsCarrie

Well-known member
Joined
Jul 12, 2005
Messages
95
Location
Arkansas
Programming Experience
5-10
I have a windows form where administrators will enter user information and when submitted will create a local machine user (not active directory). I can create the user, but have been unable to set the password to NEVER expire.

this is a requirement for the server of our RF Guns; each user must have a local (non network) account on the server in order to sign into the RF Guns. The passwords have to be set to never expire or the users get unexpectedly booted from the guns.

I am using this to create the user:
VB.NET:
Private Function CreateLocalUser(ByVal userAcct As String, ByVal userName As String, _
                                    ByVal password As String, ByVal homedir As String, _
                                    ByVal desc As String) As Boolean
        Dim bol As Boolean = True
        Try
            Dim MyProc As New System.Diagnostics.Process()
            MyProc.StartInfo.WorkingDirectory = "C:\WINDOWS\SYSTEM32\"
            MyProc.StartInfo.FileName = "net.exe"
            MyProc.StartInfo.UseShellExecute = False
            MyProc.StartInfo.RedirectStandardError = True
            MyProc.StartInfo.RedirectStandardInput = True
            MyProc.StartInfo.RedirectStandardOutput = True
            MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden


            MyProc.StartInfo.Arguments = " user " & userAcct & " " & password & " /ADD /ACTIVE:YES " & _
                                         "/EXPIRES:NEVER /FULLNAME:""" & userName & _
                                         """ /HOMEDIR:""" & homedir & """ /PASSWORDCHG:NO /PASSWORDREQ:YES" & _
                                         " /PASSWORDEXP:NO /COMMENT:""" & desc & """"

            MyProc.Start()
            MyProc.WaitForExit()
            MyProc.Close()




        Catch ex As Exception
            bol = False
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Source)
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Message)
        End Try
        Return bol
    End Function
However I can find no way to set the Password to never expire.

I have also tried:
VB.NET:
    Private Function CreateWinUser(ByVal username As String, ByVal password As String, _
                                   ByVal description As String, ByVal homeDir As String, _
                                   ByVal fullname As String) As Boolean

        Dim bol As Boolean = True
        Try
            'Initiate DirectoryEntry Class To Connect Through WINNT Protocol
            Dim enTryString As String = "WinNT://" + Environment.MachineName + ",computer"
            Dim dirEnTry As DirectoryEntry = New DirectoryEntry(enTryString)

            'Search If Specified User Already Exists
            Dim userFound As Boolean = False

            Try
                If Not dirEnTry.Children.Find(username, "user") Is Nothing Then
                    userFound = True
                End If
            Catch
                userFound = False
            End Try

            If Not userFound Then
                Dim NewUser As DirectoryEntry = New DirectoryEntry(enTryString)
                NewUser.Children.Add(username, "user") 'Add user
                NewUser.CommitChanges()

                If description.Trim() <> "" Then
                    NewUser.Invoke("Description", New Object() {description})
                End If

                NewUser.Invoke("FullName", New Object() {fullname})
                NewUser.Invoke("homedir", New Object() {homeDir})

                '1. User Can not Change Password
                NewUser.Invoke("Put", New Object() {"userFlags", "0x000000040"})

                '2. Password Never Expires
                NewUser.Invoke("Put", New Object() {"userFlags", "0x00010000"})

                NewUser.Invoke("SetPassword", New Object() {password}) 'Set password
                NewUser.CommitChanges()
            End If
        Catch ex As Exception
            bol = False
            rtbNotes.ForeColor = Color.Red
            rtbNotes.Clear()
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText("ERROR:")
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Source)
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Message)
        End Try
        Return bol
    End Function
But it always returns this error: Active Directory Not implemented

I do not want an Active Directory account, only a local machine user.

does anyone have working VB code that will do this or know where I can get it?
 
One way is to use the WMI class Win32_UserAccount, set PasswordExpires property and commit it. Generate a VB.Net class as described in this thread post 5 and add it to project. Sample code:
VB.NET:
Dim user As Win32.UserAccount = CType(Win32.UserAccount.GetInstances("Name=""accountName""")(0), Win32.UserAccount)
user.PasswordExpires = False        
user.CommitObject()
Regarding /expires switch for Net User the documentation says this is for account expiration, not password.

Here's an example using StringBuilder:
VB.NET:
Dim builder As New System.Text.StringBuilder

builder.AppendFormat("user {0} {1} /add", userAcct, password)
builder.AppendFormat(" /fullname:""{0}"" /homedir:""{1}"" /comment:""{2}""", userName, homedir, desc)
builder.Append(" /active:yes /expires:never /passwordchg:no /passwordreq:yes")

MyProc.StartInfo.Arguments = builder.ToString
 
Help with Sample

Hello all,
I am trying to set a local user passwords to never expire just as in the example above, hwoever I am getting something strange. When I add the code I get an error: Win32.UserAccount.UserAccountCollection cannot be indexed because it has no default proerty.

I have created the useraccount class and added it to the project. This is the only line that gives me an error.
 
This is the exact code I use to create my local users.
I do not use my object to create the users since I could never get it to work.
VB.NET:
Private Function CreateLocalUser(ByVal userAcct As String, ByVal userName As String, _
                                    ByVal password As String, ByVal homedir As String, _
                                    ByVal desc As String) As Boolean
        Dim bol As Boolean = True
        Dim builder As New System.Text.StringBuilder
        Try
            Dim MyProc As New System.Diagnostics.Process()
            MyProc.StartInfo.WorkingDirectory = "C:\WINDOWS\SYSTEM32\"
            MyProc.StartInfo.FileName = "net.exe"
            MyProc.StartInfo.UseShellExecute = False
            MyProc.StartInfo.RedirectStandardError = True
            MyProc.StartInfo.RedirectStandardInput = True
            MyProc.StartInfo.RedirectStandardOutput = True
            MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

            builder.AppendFormat("user {0} {1} /add", userAcct, password)
            builder.AppendFormat(" /fullname:""{0}"" /homedir:""{1}"" /comment:""{2}""", userName, homedir, desc)
            builder.Append(" /active:yes /expires:never /passwordchg:no /passwordreq:yes")

            MyProc.StartInfo.Arguments = builder.ToString()

            MyProc.Start()
            MyProc.WaitForExit()
            MyProc.Close()

            Dim userList As Win32.UserAccount.UserAccountCollection
            Dim user As Win32.UserAccount
            userList = Win32.UserAccount.GetInstances("Name=""" & userAcct & """")
            For Each user In userList
                If user.Name = userAcct Then
                    If user.PasswordExpires Then
                        user.PasswordExpires = False
                        user.CommitObject()
                    End If
                    Exit For
                End If
            Next
        Catch ex As Exception
            bol = False
            rtbNotes.ForeColor = Color.Red
            rtbNotes.Clear()
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText("ERROR:")
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Source)
            rtbNotes.AppendText(vbCrLf)
            rtbNotes.AppendText(ex.Message)
        End Try
        Return bol
    End Function

Instead I use the net.exe cmd and then afterwards set the password to never expire.
Not elegant but it works.
 

Latest posts

Back
Top