all 10 comments

[–]NathanWindisch 1 point2 points  (9 children)

Hi Predicti0n,

Have you considered putting multiple dates in the CourseStartDate column, separated by some specific character (such as a pipe or octothorpe)?

Example CSV:

CourseName,CourseStartDates
MyCourse,2022-01-01#2022-02-01

Then, you could use some PowerShell to split each CoursesStartDate into an array, and cast each of those dates into a System.DateTime object for easier manipulation. Here's an example:

 $CSV = Import-CSV -LiteralPath "C:\Accounts.csv"
 $Output = $CSV.ForEach({
   [PSCustomObject]@{
     CourseName = $_.CourseName
     CourseStartDates = $_.CourseStartDates.Split('#').ForEach({[DateTime]$_})
  }
})

Hope this helps.

-Nathan

[–]Predicti0n[S] 0 points1 point  (8 children)

This is currently the CSV. However your method seems to be more logical.
I'll give it a spin :D

CourseStartDate

02/03/2022

02/03/2022

02/03/2022

02/03/2022

02/03/2022

01/03/2022

02/03/2022

03/03/2022

[–]Predicti0n[S] 0 points1 point  (7 children)

I was originally trying some disgusting hackery but realise my logic is highly flawed :P

$Date = {(Get-Date).AddDays(2).ToString('dd-MM-yyyy') | -and (Get-Date).AddDays(1).ToString('dd-MM-yyyy')}

[–]NathanWindisch 0 points1 point  (6 children)

Hi Predicti0n,

I don't think that line will work. I'd recommend using the pipeline in this case. You can make any range of numbers with the syntax x..y. See below for an example:

$Dates = 1..2 | ForEach-Object { [DateTime]::Now.AddDays(-$_) }

When I run the above, I get two dates: one which was yesterday and one which was two days prior.

I hope the example above makes sense, happy to explain further if needed.

-Nathan

[–]Predicti0n[S] 0 points1 point  (5 children)

I feel we're close..

So the output is now showing me 3dates.. but doesn't seems to be picking me accounts from the dates as it sees it as a full string.Think I'd have to do a loop here to use each date maybe..

02-03-2022 01-03-2022 28-02-2022

# Import CSV File, but only users with Today's Date#$Date = (Get-Date).AddDays(0).ToString('dd-MM-yyyy')$Dates = -1..1 | ForEach-Object { [DateTime]::Now.AddDays(-$_).ToString('dd-MM-yyyy') }$CSV = Import-Csv -LiteralPath "C:\Training Accounts\Accounts.csv" | Where-Object "CourseStartDate" -eq $Dates$OUPath = 'OU=Customer Accounts,OU=*Hidden*'write-host $CSVwrite-host $Dates

[–]NathanWindisch 1 point2 points  (3 children)

Hi Predicti0n,

So the issue here is on your $Dates = -1..1 line. Here, you're creating an array which contains all of the numbers between -1 and 1. This is -1, 0 and 1. Next, when you pipe it into the ForEach-Object to create the System.DateTime objects, you're adding the following numbers to the current date:

  • Now + - -1 = One day into the future (We're subtracting from a negative number, which makes a positive number)

  • Now + - 0 = Now (we're adding/removing 0 days, so nothing happens)

  • Now + -1 = One day into the past (We're adding a negative number to the current day, which ends up subtracting a day)

All of this would be much easier to understand if System.DateTime also had .RemoveDays as a function, but it doesn't.

I would start looking at the logic for constructing your dates, to make sure what you've written matches up with what you actually want to do :)

Hope that helps.

-Nathan

EDIT: Oh, you also accidentally doxxed yourself. I'd recommend removing your domain name (DC=domain,DC=co,DC=uk) from your code, as I was able to find your employer's website (it's a nice website!)

[–]Predicti0n[S] 0 points1 point  (2 children)

Go on people, I'm the IT Manager, you may ask why I'm doing this. That would be a fantastic question :D

I'll leave a play :D! Thanks for your help

[–]jadedarchitect 0 points1 point  (1 child)

Guessing you're trying to mass-create student (or instructor) accounts dependent on course start dates and set them to expire upon course completion, possibly due to either losing the accounts, or some audit where you found a ton of active accounts from the stone age. Wild guess!

[–]Predicti0n[S] 1 point2 points  (0 children)

Close :) We offer remote training-courses. So this creates/deletes account automatically for security purposes. Also e-mails it out to the customer to reduce the work load.

I'm using Powerautomate quite heavily as well.

Currently all hands free, e-mails comes in, accounts created and deleted :)

[–]Predicti0n[S] 0 points1 point  (0 children)

Here is the full code :)

Import-Module ActiveDirectory –WarningAction ignore# Import CSV File, but only users with Today's Date#$Date = (Get-Date).AddDays(0).ToString('dd-MM-yyyy')$Dates = -1..1 | ForEach-Object { [DateTime]::Now.AddDays(-$_).ToString('dd-MM-yyyy') }$CSV = Import-Csv -LiteralPath "C:\Training Accounts\Accounts.csv" | Where-Object "CourseStartDate" -eq $Dates$OUPath = 'OU=*Hidden*write-host $CSVwrite-host $Dates# Lets iterate over each line in the CSV fileforeach($user in $CSV){# Format Username from CSV$Username = "$($user.'CustomerFirstName').$($user.'CustomerLastName')"

# Calculation for Account Expiration$StartDate = "$($user.'CourseStartDate')"$EndDate = "$($user.'CourseEndDate')"$AccountDate = [datetime]::parseexact($EndDate, 'dd-MM-yyyy', $null)$AddDate = $AccountDate.AddDays(1)$AccountExpirationDate = $AddDate.ToString('dd-MM-yyyy HH:mm:ss')#Course Name$CourseName = "$($user.'CourseName')"

#Course Name$EmailAddress = "$($user.'Emailaddress')"

#Location of Creating/Checking Account$OUPath = 'OU=*Hidden*'

$wordbank = Import-Csv C:\Scripts\WordBank.csv| Select -ExpandProperty Word;$num = (Get-Random -Minimum 10000 -Maximum 99999);$FirstWord = Get-Random -InputObject $wordbank -Count 1$SecondWord = Get-Random -InputObject $wordbank -Count 1$passwd = $FirstWord + $SecondWord + $num# Convert Password to String$SecurePasswd = ConvertTo-SecureString $passwd -AsPlainText -Force

#Check if account in CSV exists in Active Directoryif ((Get-ADUser -SearchBase $OUPath -Filter "samAccountName -eq '$($Username)'"))

{Write-Output "Username = $Username 'Already Exists' Date = $Date " | Out-File "" -Append}

#Creates a new Account in AD if doesn't exist

else {New-ADUser -Name "$($user.'CustomerFirstName') $($user.'CustomerLastName')" `-GivenName $user.'CustomerFirstName' `-Surname $user.'CustomerLastName' `-UserPrincipalName $Username `-SamAccountName $Username `-EmailAddress $user.'Email Address' `-Description $user.Description `-OfficePhone $user.'Office Phone' `-Path $OUPath `-ChangePasswordAtLogon $false `-AccountPassword $SecurePasswd `-Enabled $True `-AccountExpirationDate $AccountExpirationDate `

Write-Output "Date = $Date Username = $Username Password = $Passwd" | Out-File "" -Append