简体   繁体   中英

Powershell - Advice: Comparing/Storing data between script runs

This is what I am trying to accomplish (the below is a ps1 script run on a schedule through windows task scheduler):

I want to get all locked accounts from a DB with a query (I can do this and it works)

Compare these returned results to those found locked in a previous run of this script (using a file in some format that stores these results)

For each locked account, add to the tally in the file (existing id if it present, else make a new entry for that id)

For each object in the file that IS NOT present in the returned locked accounts from the original query to the DB, remove them from the file (reset the tally/delete that object entry)

Check if the tally for any object has reached a predetermined threshold then do something if it has.

My issue is I have no idea how I should store the object array from the initial DB query (csv? json? plain text?).

Has anyone done anything like this before with Powershell? Is there a simple way to store/retrieve/manipulate this data?

I've tried with a plain text file in the format of having each line a recorded locked account and then putting them into an array then going on to compare the two arrays.. but that gets messy/complicated with multiple foreach loops inside eachother.

Any help would be greatly appreciated :)

I always encourage my coworkers to use SQL to store all script data, In our case we have hundreds of scripts doing all kinds of things. Flat file databases are easy to break if for instance its being read or written to at the same time as something else and it gets locked.

You have to evaluate your logic and assess if anything might mess with the file and break the script when it runs. If its all good then CSV files are fine. Otherwise i'd use SQL. Its pretty easy to write and read to SQL from powershell.

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