Home > Software engineering >  Powershell Continue where I left off in a loop
Powershell Continue where I left off in a loop

Time:01-26

Hi I have a script that gets all Users from a set of Office Location. I then run a script to get all servers where the user is logging on, and get the windows-event to check if the user has logged on to the computer or not. This is working fine, The script takes a long time sometimes its over 800 users to check on 100 servers. So the script will stop. I need a way to restart the script, but not start all over from the top of my $USERS variable.. I export the values of users logged inn into a csv file. I import this file and read the last entry user. I then want the script to continue from that user entry index .

So I would like to have a way to check user then set the index to start from lets say user number 3 then prosses the rest 4 - end.

$lastuser = (import-csv -path "Path to csv file").UserName[-1]
Ray
Tod
Burt # Lets say the script terminates here, I want it to continue from here and then procsess the rest
Ali
Mohamed
Annie.....

CodePudding user response:

Try following to get index of the last user:

$users = @("Ray","Tod","Burt","Ali")
$lastUser = "Burt"
$userList = New-Object System.Collections.Generic.List[string]
$userList.AddRange([string[]]$users)
$userList
$index = $userList.IndexOf($lastUser)
$index
  • Related