Import-Csv + Set-ADAccountExpiration problem

Welcome Forums General PowerShell Q&A Import-Csv + Set-ADAccountExpiration problem

Viewing 11 reply threads
  • Author
    Posts
    • #199451
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      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: (:) [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

    • #199457
      Participant
      Topics: 5
      Replies: 2367
      Points: 5,981
      Helping Hand
      Rank: Community MVP

      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))
      }
      • This reply was modified 5 months, 1 week ago by Olaf.
    • #199508
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      Thanks that has worked – Great.

       

       

    • #199532
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      ..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)
    • #199535
      Participant
      Topics: 0
      Replies: 14
      Points: 151
      Rank: Participant

      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?

    • #199646
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      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

       

       

    • #199673
      Participant
      Topics: 5
      Replies: 2367
      Points: 5,981
      Helping Hand
      Rank: Community MVP

      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.

      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 …

      • This reply was modified 5 months, 1 week ago by Olaf.
    • #199736
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      ..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

       

       

       

       

       

    • #199739
      Participant
      Topics: 5
      Replies: 2367
      Points: 5,981
      Helping Hand
      Rank: Community MVP

      I don’t think that would work as the date would need to be a variable.

      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. 😉

      • This reply was modified 5 months, 1 week ago by Olaf.
    • #199751
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      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 !

       

       

    • #199793
      Participant
      Topics: 5
      Replies: 2367
      Points: 5,981
      Helping Hand
      Rank: Community MVP

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

      $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))
      }
    • #200192
      Participant
      Topics: 1
      Replies: 6
      Points: 25
      Rank: Member

      Thank You – that has worked!

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

Viewing 11 reply threads
  • The topic ‘Import-Csv + Set-ADAccountExpiration problem’ is closed to new replies.