简体   繁体   中英

Processing a list with Powershell on a Scheduled Task

I'm a newbie to powershell, so I'm not sure if I am over thinking what I want to do, hopefully somebody can help me out.

I want to create a Windows scheduled task that calls my powershell script every 4 hours, no problems there. The script needs to process a list of ID's that range from 1 to 140,(They skip a few here and there which makes this harder) the script is just calling another external program that takes about 2 hours per ID, we give it 4 hours to be safe. So upon the powershell script loading, it needs to read from a file the last scan it did, I can do that using

    $lastscan = Get-Content'lastScan.txt'

Then the script needs to find the next object in the list, which may skip a number here or there so it's not as simple as just incrementing the last scan run. Once it has the next ID it will kick off the scan and save the new value to the text file named "lastScan.txt" and exit. The only thing I need help with, is how to find the next scan in my list based on the lastscan run. The list is seperated by newline, but I could edit it if it makes it easier.

    1
    5
    6
    9
    etc, etc, ...

Assuming you have a string array that was populated with Get-Content and assuming the array did not contain duplicates, you could use the Array.IndexOf method to get the index of the current id and then return the next element in the array. Example:

$x = "1","2","3"
$currentId = "2"
$nextIndex = [array]::IndexOf($x, "2") + 1
$nextId = $x[$nextIndex] #nextId should now be "3"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM