Account Expiration Default Value for "Never"

%3CLINGO-SUB%20id%3D%22lingo-sub-663852%22%20slang%3D%22en-US%22%3EAccount%20Expiration%20Default%20Value%20for%20%22Never%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663852%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20somebody%20has%20an%20idea%20about%20my%20concern.%20I%20really%20need%20it%20badly%20as%20I%20have%20a%20client%20testing%20tomorrow.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyways%2C%20the%20story%20is%20this%2C%20I%20need%20to%20create%20a%20script%20to%20update%20the%20attributes%20of%20all%20the%20users%20from%20a%20CSV%20file%20if%20they%20already%20exist%20in%20AD%20and%20if%20not%2C%20they%20will%20be%20created%20also.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EProblem%20is%2C%20some%20users%20in%20the%20CSV%20file%20has%20the%20Account%20Expiration%20Date%20value.%20And%20I%20need%20to%20set%20that%20if%20that%20value%20is%20blank%20it%20will%20be%20automatically%20set%20to%20%22NEVER%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20also%20read%20that%20setting%20it%20to%2012%2F30%2F1600%20will%20automatically%20set%20the%20value%20to%20%22NEVER%22.%20But%20when%20I%20try%20to%20do%20it%2C%20an%20error%20value%20of%20%22Not%20a%20valid%20Win32%20FileTime%22%20will%20return.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20Ideas%3F%3C%2FP%3E%3CP%3EThank%20You%20in%20Advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20the%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24NA%20%3D%20%2212%2F30%2F1600%22%3CBR%20%2F%3E%24Path%20%3D%20'CN%3DUsers%2CDC%3Dadsftest%2CDC%3Dcom%2CDC%3Dph'%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Eif%20(Get-ADUser%20-Filter%20%7BSamAccountName%20-eq%20%24Username%7D)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23If%20User%20Exist%2C%20Update%20Attributes%3CBR%20%2F%3E%24USERS%20%3D%20Set-ADUSer%20-Identity%20%24Name%20-EmployeeID%20%24EmployeeID%20-UserPrincipalName%20%22%24Username%40adsftest.com.ph%22%20-Title%20%24Title%20-AccountExpirationDate%20%24AccountExpirationDate%20-Office%20%24Office%20-Department%20%24Department%20-StreetAddress%20%24StreetAddress%20-State%20%24State%20-City%20%24City%20-Company%20%24Company%20-Fax%20%24Fax%20-PostalCode%20%24PostalCode%20-Description%20%24Description%20-Enabled%20%24true%3CBR%20%2F%3EWrite-Output%20%22User%20%24SamAccountName%20Already%20Exist%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eif%20(%24User.%22LAST%20EMPLOYMENT%20DATE%22%20-ne%20%24null)%20%7B%3C%2FP%3E%3CP%3ESet-ADUser%20-Identity%20%24Username%20-AccountExpirationDate%20%24NA%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3Eelse%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23User%20does%20not%20exist%20then%20proceed%20to%20create%20the%20new%20user%20account%3CBR%20%2F%3E%24Results%20%3D%20new-aduser%20-Name%20%24Name%20-SamAccountName%20%24Username%20-UserPrincipalName%20%22%24Username%40adsftest.com.ph%22%20-EmployeeID%20%24EmployeeID%20-GivenName%20%24GivenName%20-Surname%20%24Surname%20-DisplayName%20%24DisplayName%20-EmailAddress%20%24EmailAddress%20-Title%20%24Title%20-Office%20%24Office%20-Department%20%24Department%20-StreetAddress%20%24StreetAddress%20-State%20%24State%20-City%20%24City%20-Company%20%24Company%20-Fax%20%24Fax%20-PostalCode%20%24PostalCode%20-Description%20%24Description%20-AccountPassword%20(ConvertTo-SecureString%20-AsPlainText%20'*****'%20-Force)%20-ChangePasswordAtLogon%20%24True%20-PasswordNeverExpires%20%24false%20-path%20%24Path%20-Enabled%20%24True%3CBR%20%2F%3EWrite-Output%20%22User%20%24Name%20has%20been%20created%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EError%20Result%3A%3C%2FP%3E%3CP%3ESet-ADUser%20%3A%20Not%20a%20valid%20Win32%20FileTime.%3CBR%20%2F%3EAt%20C%3A%5CScript%5CUL-ADCreate-New.ps1%3A45%20char%3A1%3CBR%20%2F%3E%2B%20Set-ADUser%20-Identity%20%24Username%20-AccountExpirationDate%20%24NA%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(u2%3AADUser)%20%5BSet-ADUser%5D%2C%20ArgumentOutOfRangeException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ActiveDirectoryCmdlet%3ASystem.ArgumentOutOfRangeException%2CMicrosoft.ActiveDirec%3CBR%20%2F%3Etory.Management.Commands.SetADUser%3CBR%20%2F%3E%3CBR%20%2F%3ESet-ADUser%20%3A%20Cannot%20bind%20parameter%20'AccountExpirationDate'.%20Cannot%20convert%20value%20%22%22%20to%20type%3CBR%20%2F%3E%22System.DateTime%22.%20Error%3A%20%22String%20was%20not%20recognized%20as%20a%20valid%20DateTime.%22%3CBR%20%2F%3EAt%20C%3A%5CScript%5CUL-ADCreate-New.ps1%3A40%20char%3A148%3CBR%20%2F%3E%2B%20...%20ExpirationDate%20%24AccountExpirationDate%20-Office%20%24Office%20-Department%20%24Department%20-S%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BSet-ADUser%5D%2C%20ParameterBindingException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20CannotConvertArgumentNoMessage%2CMicrosoft.ActiveDirectory.Management.Commands.S%3CBR%20%2F%3EetADUser%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-663852%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-680213%22%20slang%3D%22en-US%22%3ERe%3A%20Account%20Expiration%20Default%20Value%20for%20%22Never%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-680213%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F304952%22%20target%3D%22_blank%22%3E%40almarlibetario%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20checked%20your%20older%20post%20where%20I%20have%20posted%20some%20ideas.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FWindows-PowerShell%2FAccount-Expiration-Default-Value-for-quot-Never-quot%2Fm-p%2F663854%23M721%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FWindows-PowerShell%2FAccount-Expiration-Default-Value-for-quot-Never-quot%2Fm-p%2F663854%23M721%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Hello Everyone,

 

I hope somebody has an idea about my concern. I really need it badly as I have a client testing tomorrow.

 

Anyways, the story is this, I need to create a script to update the attributes of all the users from a CSV file if they already exist in AD and if not, they will be created also.

 

Problem is, some users in the CSV file has the Account Expiration Date value. And I need to set that if that value is blank it will be automatically set to "NEVER".

 

I have also read that setting it to 12/30/1600 will automatically set the value to "NEVER". But when I try to do it, an error value of "Not a valid Win32 FileTime" will return.

 

Any Ideas?

Thank You in Advance

 

Here's the script:

 

$NA = "12/30/1600"
$Path = 'CN=Users,DC=adsftest,DC=com,DC=ph'


if (Get-ADUser -Filter {SamAccountName -eq $Username})
{
#If User Exist, Update Attributes
$USERS = Set-ADUSer -Identity $Name -EmployeeID $EmployeeID -UserPrincipalName "$Username@adsftest.com.ph" -Title $Title -AccountExpirationDate $AccountExpirationDate -Office $Office -Department $Department -StreetAddress $StreetAddress -State $State -City $City -Company $Company -Fax $Fax -PostalCode $PostalCode -Description $Description -Enabled $true
Write-Output "User $SamAccountName Already Exist"
}
if ($User."LAST EMPLOYMENT DATE" -ne $null) {

Set-ADUser -Identity $Username -AccountExpirationDate $NA

}
else
{
#User does not exist then proceed to create the new user account
$Results = new-aduser -Name $Name -SamAccountName $Username -UserPrincipalName "$Username@adsftest.com.ph" -EmployeeID $EmployeeID -GivenName $GivenName -Surname $Surname -DisplayName $DisplayName -EmailAddress $EmailAddress -Title $Title -Office $Office -Department $Department -StreetAddress $StreetAddress -State $State -City $City -Company $Company -Fax $Fax -PostalCode $PostalCode -Description $Description -AccountPassword (ConvertTo-SecureString -AsPlainText '*****' -Force) -ChangePasswordAtLogon $True -PasswordNeverExpires $false -path $Path -Enabled $True
Write-Output "User $Name has been created"

 

Error Result:

Set-ADUser : Not a valid Win32 FileTime.
At C:\Script\UL-ADCreate-New.ps1:45 char:1
+ Set-ADUser -Identity $Username -AccountExpirationDate $NA
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (u2:ADUser) [Set-ADUser], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentOutOfRangeException,Microsoft.ActiveDirec
tory.Management.Commands.SetADUser

Set-ADUser : Cannot bind parameter 'AccountExpirationDate'. Cannot convert value "" to type
"System.DateTime". Error: "String was not recognized as a valid DateTime."
At C:\Script\UL-ADCreate-New.ps1:40 char:148
+ ... ExpirationDate $AccountExpirationDate -Office $Office -Department $Department -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.S
etADUser

1 Reply
Highlighted