0

I currently have a PS script that is used for remediation of breached user accounts in a hybrid O365/On-prem AD environment. The script currently works as intended, enabling MFA, logging, resetting passwords, removing mailbox rules, etc.

There are times when we must remediate more than just a few accounts. That being said I am trying to set up the script to go line by line in a CSV file with headers to pull the users UPN name and execute the script for each UPN name in a csv.

Currently the script cant take and run multiple inputs for each function at once time so the script would need to re-run again. Additionally, the script pulls cmdlets and etc for each time it runs, being able to speed up the script by say keeping the initial session open would be a bonus.

Ideally, the result would be the script continue to run without requiring me to enter another UPN.

1

1 Answer 1

1

After using import-csv you can loop through each item with a foreach.

$myCSV = import-csv $theCSVfile

foreach($row in $myCSV){
    myPSscript($row.UPNname)
}

Or to update the $row.UPNname field with new data from your PS script:

$myCSV = import-csv $theCSVfile

foreach($row in $myCSV){
    $newUPNname = myPSscript($row.UPNname)
    $row.UPNname = $newUPNname
}
export-csv $myCSV

If you are working with more than just the UPNname, you will need to pass those fields or the entire record ($row) of data.

$myCSV = import-csv $theCSVfile
$mynewCSV = @()
foreach($row in $myCSV){
    $newRow = myPSscript($row)
    $mynewCSV += $newRow
}
export-csv $mynewCSV
9
  • Would I need to modify the script itself to continue back tot he first function to continue running? so it does not simply end? May 10, 2019 at 15:09
  • No. The foreach will call an instance of your script for each row in the data.
    – Brian
    May 10, 2019 at 15:27
  • Gotcha, I will give it a shot. May 10, 2019 at 16:23
  • that works, just as your said. if I echo $upn it will output the contents of the csv file. But the rest of my script wont do anything with them. May 10, 2019 at 20:49
  • If you are manipulating the data and want to write it back to the CSV you will need to pass it back and forth or build this code into your main script and write the data out with export-csv I will update the answer with a few more starting points.
    – Brian
    May 10, 2019 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .