Dumb Basic Question

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Anthony Stringer Anthony Stringer 11 months ago.

  • Author
  • #40757
    Profile photo of Kelly Dyjur
    Kelly Dyjur

    I am using Import-Csv to read lines from an input file, but I also want to remove lines where the analyst is too lazy to format the date correctly. So for each line in this "watchlist" (looking for account lockouts) that the script reads in, I want to inspect for manadatory fields and a properly formatted date. We don't want analysts leaving stale entries in this list.

    -Here are my headers in the CSV

    -Here's my code:

    $regex = "^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$"
    $watchlist = Import-Csv -Path xxxxxxxxxxxxxxxxx\watchlist.csv
    foreach ($item in $watchlist){
        #Default is that record is spoiled i.e. missing a mandatory field
        #or has improperly formatted date
        #Check to see that line has sAMAccountName, Analyst, Removal Date
        #If so redeem record
        If ($item.sAMAccountName -and $item.Analyst -and $item.removal_date -and ($item.removal_date -match $regex))
            $watchlist = $item.sAMAccountName + "," + $item.Incident + "," + $item.Analyst + "," + $item.Removal_Date
            } else {
            $recycleList =+ $Item

    Essentially I want to build a watchlist variable and this populates what was there, and a recycle list where the entries are removed and the analyst is emailed (I can write all the code for this).

    My problem comes when I go to add the "lines" together to make the new csv. I'm sorry that most of this comes out like caveman language.

    I have to give a shout out to Don Jones whose TechNet article that I read yesterday got me going on how to evaluate the date using regular expressions.

    • This topic was modified 11 months ago by Profile photo of Kelly Dyjur Kelly Dyjur. Reason: tags wrong
  • #40763
    Profile photo of Jack Neff
    Jack Neff

    You're overthinking it bud. You don't have to piece the $item object back together after you've validated it. Just shoot it back out to a new csv file with export-csv -append.

  • #40783
    Profile photo of Anthony Stringer
    Anthony Stringer

    not tested, but this may be an option for you

    $watchlist = Import-Csv -Path xxxxxxxxxxxxxxxxx\watchlist.csv
    $wrongdate = $watchlist | % {
        try {
            Get-Date $_.removal_date
        } catch {
            Write-Output $_

You must be logged in to reply to this topic.