Want help to fix terrible script

I inherited this from a co-worker and want to make it better. Our HR fills out a form that gets converted to a CSV we use to create new ADUsers. It’s currently looking for a CSV in a specific location and if there is one it imports the user to AD. We’ve got it running every 2 minutes as a scheduled task, so if HR adds a few people all at once it can break with multiple named CSVs.

$strFileName="C:Pathfile.csv" $strFileName1="C:Pathfile (1).csv" ... $strFileName10="C:Pathfile (10).csv" If (Test-Path $strFileName) {Import-Csv $strFileName | foreach-object {New-ADUser -Name $_.DisplayName <etc> } Import-csv $strFileName | Foreach-Object {add-adgroupmember -Identity $_.Group -Members $_.Username} Remove-Item $strFileName } ElseIf(Test-Path $strFileName1) <repeats 10 times for each $strFileName> 

What I think I’d like is something like

foreach($file in $path){ <do stuff> Remove-Item $file} 

Am I on the right path? Also is there a better way to add them to the group they need to be in than what it’s doing?

submitted by /u/xgnarf
[link] [comments]

Leave a Reply