Import-Csv + Set-ADAccountExpiration problem

Hi,

Have a problem with updating user account account expiry dates that is being fed in from a CSV file.

If I have just one record in the csv file, then it works and the account exipration date gets updated, but if I have two or more users listed in the file then I see this error…

Set-ADAccountExpiration : Cannot convert ‘System.Object[]’ to the type ‘System.Nullable`1[System.DateTime]’
required by parameter ‘DateTime’. Specified method is not supported.At line:9 char:101
+ CategoryInfo : InvalidArgument: (:slight_smile: [Set-ADAccountExpiration], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADAccountE
xpiration”…

$Users = Import-Csv “C:\Temp\New Starters.csv”

$User = $Users.UserName
$EndDate = $Users.EndDate

ForEach ($User in $Users)
{
Get-ADUser -Properties AccountExpirationDate $User.UserName | Set-ADAccountExpiration -DateTime $EndDate
}

Can someone please let me know where I am going wrong…

Thanks

Please format your code as code using the code tag button (“PRE”). Thanks.

You should create a [DateTime] type from your string data from your CSV … try this:

$UserList = Import-Csv 'C:\Temp\New Starters.csv'

ForEach ($User in $UserList) {
    Set-ADAccountExpiration -Identity $User.UserName -DateTime (Get-Date $($Users.EndDate))
}

Thanks that has worked - Great.

 

 

…now have another problem where AD sets the expiration date one day less than the date passed through my ps script…

I have tried something like the following, but not sure where to place it in the above statement…any ideas…?

Thanks for any help with this

$date = (Get-Date).AddDays(+1)

Hi,

What do EndDate values look like in your CSV file? What is returned when you run Get-Date against one of those values on the command line?

Hi,

The End Date Value shows “01/11/2020” and Get-Date shows “01 November 2020 00:00:00”

The reason I need to insert the following statement (somewhere into my statement), is that AD will populate the expiry date field to 31/10/2020…see link below which goes into detail why this happens.

$date = (Get-Date).AddDays(+1)

https://social.technet.microsoft.com/Forums/en-US/9a78f4b2-e992-4323-89ce-2375b2a03c6c/ad-sets-account-expiration-date-one-day-less-than-the-date-passed-through-powershell

Any help will be much appreciated.

Thanks

 

 

[quote quote=199646]The End Date Value shows “01/11/2020” and Get-Date shows “01 November 2020 00:00:00”

The reason I need to insert the following statement (somewhere into my statement), is that AD will populate the expiry date field to 31/10/2020…see link below which goes into detail why this happens.[/quote]

So you may add a few hours to your “End Date value” to circumvent this issue

(Get-Date “01/11/2020”).AddHours(6).AddDays(1)

Maybe it depends on your time zone setting as well …

…thanks for getting back… I don’t think that would work as the date would need to be a variable.

just to recap…I need to import a CSV file, convert the date from a string into a date type, add +1 day and then feed this new date variable into the AD expiration field.

Hope that this is clear…otherwise I will have to try some alternative arrangement.

Thanks again

 

 

 

 

 

Would it hurt you if you tried? You told that a date of 1st of November will be “converted” to 31st of October. So I’d think it could be the case of some weird little automatic conversion in the background - maybe because of time zone differences or something like this. Just try it - I’m pretty sure it will not kill you. :wink:

Thanks once again, I have tried this…but not sure where I would enter in the command

(Get-Date "01/11/2020").AddHours(6).AddDays(1)

…here is the working script, which grabs the date from the New Starters.csv

$UserList = Import-Csv "C:\Temp\New Starters.csv"

$User = $User.UserName
$EndDate = $User.EndDate


ForEach ($User in $UserList) {
Set-ADAccountExpiration -Identity $User.UserName -DateTime (Get-Date $($User.EndDate))
}

…but not to worry if my message is unclear…I will try another way.

Thanks once again !

 

 

$UserList = Import-Csv "C:\Temp\New Starters.csv"
ForEach ($User in $UserList) {
Set-ADAccountExpiration -Identity $User.UserName -DateTime $((Get-Date $User.EndDate).AddHours(6).AddDays(1))
}

Thank You - that has worked!

…and I have learnt a lot more about ps since!