Welcome › Forums › General PowerShell Q&A › Set-expirationdate delay
- This topic has 2 replies, 2 voices, and was last updated 1 month, 1 week ago by
Participant.
-
AuthorPosts
-
-
December 11, 2020 at 4:14 am #278694
Hello,
I use a script where a user is created , one of the settings is the expiration date.
When the line for the set-expirationdate is called, powershell throws a error, that the aduser can not be found.
I tried to workaround this, by pausing for 6 seconds to give AD the time to “think”
This is not working, only when I do a get-aduser $username (after all is done) the usewr is present in AD.
Then when I manually run the Set-expirationdate line, it is working fine. ( at that moment)I am wondering why this is happening, and of course how I can fix this
PowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344454647# This will be the info needed to create the account$ADM = "adm-fu-"$FirstName = Read-Host "FirstName is?"$MiddleInitial = Read-Host "What are the Middle Initals?"$LastName = Read-Host "Lastname is?"$Description = Read-Host "Fill in the Ticketnr, and the function"$FJDomain = Read-host "what the e-mail domain, (like @name.com)"$Expiration = Get-Date $((Get-Date).adddays(360)) -f 'yyyy-MM-dd'$Emailaddress = $FirstName + '.' +$LastName + $FJDomain$Company = Read-Host "What is the name of the Company"$DefaultPassword= Read-Host "Create a strongpassword"# setting the values for a adminaccount$Username = 'adm-fu-' + $lastname.substring(0,4) + $Firstname.substring(0,1)+'1'#Create the New user Account$NewUserParams = @{'UserPrincipalName' = $Username'Name' = $Username'GivenName' = $FirstName'Surname' = $LastName'DisplayName' = $FirstName + $MiddleInitial + $LastName'Description' = $Description'EmailAddress'= $Emailaddress'SamAccountName' = $Username'AccountPassword' = (ConvertTo-SecureString $DefaultPassword -AsPlainText -Force)'CannotChangePassword' = $false'Enabled' = $True'Initials' = $MiddleInitial'Path' = "$OU,$Dn"'Company' = $company#'AccountExpirationDate' = Set-ADAccountExpiration -DateTime $Expiration#Because of COVID and RDP, do not set " change at next logon"'ChangePasswordAtLogon' = $false}#Create the new user accountNew-AdUser @NewUserParamsSleep -Seconds 6#set-expirationdateSet-ADAccountExpiration $username -DateTime $Expiration -
December 11, 2020 at 4:22 am #278697
Specify the same domain controller with the -Server parameter for both cmdlets. You’re probably hitting a different DC when you run Set-ADAccountExpiration and the new account hasn’t replicated.
-
December 14, 2020 at 2:07 am #279351
Hello Matt,
Thank you for pointing the right direction.
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.