简体   繁体   中英

Error when adding new AD user

I'm working on a small script to make adding users to our AD easier. It's just a one line New-ADUser powershell script that asks for inputs. It looks like this:

New-ADUser -Name (Read-Host "User Name") -AccountExpirationDate 0 -CannotChangePassword 1 -ChangePasswordAtLogon 0 -PasswordNeverExpires 1 -Company (Read-Host "Company") -Department (Read-Host "Department") -Title (Read-Host "Title") -Manager (Read-Host "Manager's User Name") -GivenName (Read-Host "Full Name") -MobilePhone (Read-Host "Cell Phone Number") -State (Read-Host "State") -AccountPassword (Read-Host -AsSecureString "Password") -ProfilePath (Read-Host "Roaming Profile Path") -Path (Read-Host "AD Path to User")

It asks for everything like it should but after inputting everything it returns: "New-ADUser : Not a valid Win32 FileTime. At line:1 char:11." I checked that location in the command (it's the space after "New-ADUser") and I don't know why it would return that.

Is this an efficient way of doing what I want? How can I fix my command to not return the error? Is it what I'm inputting into the spots?

You have to change:

-AccountExpirationDate 0

with

-AccountExpirationDate $null

or you can simply not set this parameter for an unexpiring account.

This technet page has an error.

For Windows Server 2016 the problem above exists, and the solution above does not work (completely).

I have found a workaround.

Yes, the Technet page above has an error; unfortunately, that also results in the PowerShell scripting system having a problem parsing the script

So - Error #1 - If you use the command New-ADUser as specified with 0 as the parameter, you get error New-ADUser : Not a valid win32 FileTime.

When you change 0 to $null , you get Error #2 - The term '(the next parameter in your command)' is not recognized as the name of a cmdlet, function

I found the following solution to completely solve the problem:

  1. Change (as above) -AccountExpirationDate $null

  2. Move this parameter to the LAST Parameter! Otherwise, you then end up with the error where the next item is incorrectly parsed. You'll note that in the IDE where the parameter shows up in the wrong color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM