{
    "header": {
        "version": 1,
        "type": "record",
        "approw-key": "SRP_HTTP_FRAMEWORK_HTTP_LOGS_ARCHIVE_SCRIPT"
    },
    "body": {
        "record1": {
            "<1>": "",
            "<2>": "function Archive-SRPHTTPLogs {",
            "<3>": "param(",
            "<4>": "    [string]$OlderThanDateStr = \"\",",
            "<5>": "    [string]$LogPathSource = \"C:\\httplogs\",",
            "<6>": "    [string]$LogPathDest = \"c:\\httplogs\\archive\"",
            "<7>": ")",
            "<8>": "$progresspreference=\"SilentlyContinue\";",
            "<9>": "",
            "<10>": "#Uncomment this to simulate an error",
            "<11>": "#Blah $Failed;",
            "<12>": "",
            "<13>": "if($OlderThanDateStr -ne \"\") {",
            "<14>": "    #Convert the supplied date to an object",
            "<15>": "    #Throws error if not a valid date",
            "<16>": "",
            "<17>": "    try {",
            "<18>": "        $OlderThanDate = [datetime]$OlderThanDateStr;",
            "<19>": "    } catch {",
            "<20>": "        throw $_;",
            "<21>": "        return;",
            "<22>": "    }",
            "<23>": "}",
            "<24>": "",
            "<25>": "#What files in $LogPathSource are considered to be log files?",
            "<26>": "$LogExtension = \"*.log\";",
            "<27>": "",
            "<28>": "#Check the organized folder exists",
            "<29>": "New-Item -Path $LogPathDest -ItemType \"directory\" -Force | Out-Null;",
            "<30>": "",
            "<31>": "#Get all the files to be organized",
            "<32>": "$LogFiles = Get-ChildItem -Path $LogPathSource -Filter $LogExtension;",
            "<33>": "",
            "<34>": "#Store a hash table of dates from file names",
            "<35>": "$LogDates = @{};",
            "<36>": "",
            "<37>": "#Loop through all the files and organize them by date",
            "<38>": "ForEach($LogFile in $LogFiles) {",
            "<39>": "",
            "<40>": "    $FileName = $LogFile.Name;",
            "<41>": "",
            "<42>": "\t#Doesn't work on Server 2008",
            "<43>": "    #$FileNameDate = [regex]::Match($FileName,'^(.*?)_').captures.groups[1].value;",
            "<44>": "\t",
            "<45>": "\t#Works in Server 2008",
            "<46>": "\t$FileNameDate = $FileName.Split('_')[0];",
            "<47>": "",
            "<48>": "    $FileNameDateObj = $Null;",
            "<49>": "",
            "<50>": "    try {",
            "<51>": "        $FileNameDateObj = [datetime]$FileNameDate;",
            "<52>": "    } catch {",
            "<53>": "        #This was not a valid date so skip",
            "<54>": "    }",
            "<55>": "",
            "<56>": "    if ($FileNameDateObj -ne $Null) {",
            "<57>": "        #This filename has a valid date",
            "<58>": "",
            "<59>": "        #Is it within our range?",
            "<60>": "        if($FileNameDateObj -le $OlderThanDate) {",
            "<61>": "            #Yes, the file name is old enough to archive",
            "<62>": "",
            "<63>": "\t        #Have we seen this date before?",
            "<64>": "            if ($LogDates[$FileNameDate] -eq $null) {",
            "<65>": "\t\t        #No, so create an array list and store it in the hash table under the date key",
            "<66>": "                #Write-Host \"Found logs for new date: $FileNameDate\";",
            "<67>": "                [System.Collections.ArrayList]$NewArray = @($FileName);",
            "<68>": "                $LogDates[$FileNameDate] = \t$NewArray;",
            "<69>": "            } else {",
            "<70>": "\t\t        # Yes, so add it to the array of filenames for that date",
            "<71>": "                # Write-Host \"Adding $FileNameDate\";",
            "<72>": "                $LogDates[$FileNameDate].Add($FileName) | Out-Null;",
            "<73>": "            }",
            "<74>": "",
            "<75>": "        } else {",
            "<76>": "            #No, the file name is newer than our cut-off date",
            "<77>": "        }",
            "<78>": "",
            "<79>": "    } else {",
            "<80>": "        #The file name did not have a valid date entry in the filename so skip",
            "<81>": "    }",
            "<82>": "",
            "<83>": "}",
            "<84>": "",
            "<85>": "#Loop through the hashtable of all the dates seen",
            "<86>": "#and process the arraylist of files for that date",
            "<87>": "ForEach($LogDate In $LogDates.keys) {",
            "<88>": "",
            "<89>": "    #Write-Host \"Processing log dates $LogDate\";",
            "<90>": "",
            "<91>": "    $LogFolderForDate = $LogPathDest + \"\\\" + $LogDate;",
            "<92>": "",
            "<93>": "\t#Make sure the organization folder exists",
            "<94>": "    New-Item -Path $LogFolderForDate -ItemType \"directory\" -Force | Out-Null;",
            "<95>": "",
            "<96>": "    $LogFilesForDate = $LogDates[$LogDate];",
            "<97>": "",
            "<98>": "\t#Loop through all the files for that date",
            "<99>": "    ForEach ($LogFileName in $LogFilesForDate) {",
            "<100>": "",
            "<101>": "        $Src = $LogPathSource + \"\\\" + $LogFileName;",
            "<102>": "        $Dst = $LogFolderForDate + \"\\\" + $LogFileName;",
            "<103>": "",
            "<104>": "        #Write-Host \"Moving $Src to $Dst\";",
            "<105>": "        Move-Item -Path $Src -Destination $Dst -Force;",
            "<106>": "",
            "<107>": "    }",
            "<108>": "",
            "<109>": "}",
            "<110>": "",
            "<111>": "#Loop through the hashtable of all the dates seen",
            "<112>": "#and compress the files that were organized into folders",
            "<113>": "ForEach($LogDate In $LogDates.keys) {",
            "<114>": "",
            "<115>": "    #Figure out which archive date we want to compress",
            "<116>": "    $LogFolderForDate = $LogPathDest + \"\\\" + $LogDate;",
            "<117>": "",
            "<118>": "    #The name of the zip file where the archived date should reside",
            "<119>": "    $LogFolderZipFile = $LogPathDest + \"\\\" + $LogDate + \".zip\"",
            "<120>": "",
            "<121>": "    try {",
            "<122>": "        #Compress it, fail if any error",
            "<123>": "        Compress-Archive -Path $($LogFolderForDate + \"\\\" + $LogExtension) -DestinationPath $LogFolderZipFile -ErrorAction Stop",
            "<124>": "",
            "<125>": "        #Remove the source log files",
            "<126>": "        Remove-Item -Path $LogFolderForDate -Recurse -Force",
            "<127>": "    } catch {",
            "<128>": "        throw $_;",
            "<129>": "        return",
            "<130>": "    }",
            "<131>": "    ",
            "<132>": "}",
            "<133>": "",
            "<134>": "",
            "<135>": "",
            "<136>": "} #End Archive-SRPHTTPLogs",
            "<137>": "",
            "<138>": "try {",
            "<139>": "    Archive-SRPHTTPLogs -OlderThanDate '{{ArchiveOlderThanDate}}' -LogPathSource '{{ArchiveLogsFromFolder}}' -LogPathDest '{{ArchiveStorageFolder}}';",
            "<140>": "    Return \"OK\";",
            "<141>": "} catch {",
            "<142>": "    Return \"ERROR`n\" + $_.ToString() ;",
            "<143>": "}"
        }
    }
}