-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppSecEzine-pbi_MakeJSON.ps1
113 lines (107 loc) · 4.76 KB
/
AppSecEzine-pbi_MakeJSON.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#requires -version 7.4
<#
.SYNOPSIS
Makes JSON file from AppSecEzine
.DESCRIPTION
This script makes a JSON file that is used to build a PowerBI report for AppSecEzine. Original content location: https://github.com/Simpsonpt/AppSecEzine
.INPUTS
Reads AppSecEzine from .\AppSecEzine
.OUTPUTS
JSON file stored in .\AppSecEzine-pbi.json
.NOTES
Version: 1.0
Author: vcudachi
Creation Date: 2024-0909@1900
License: Creative Commons Attribution NonCommercial ShareAlike (CC-NC-SA: https://creativecommons.org/licenses/by-nc-sa/3.0/)
GitHub: https://github.com/vcudachi/AppSecEzine-pbi
.EXAMPLE
#Powershell:
git clone https://github.com/vcudachi/AppSecEzine-pbi.git
cd AppSecEzine-pbi
git clone 'https://github.com/Simpsonpt/AppSecEzine.git'
. .\AppSecEzine-pbi_MakeJSON.ps1
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
If (!(Test-Path .\AppSecEzine -PathType Container -ErrorAction SilentlyContinue)) {
Write-Host 'AppSecEzine is not found. Read README.md for details.' -ForegroundColor RED
Start-Sleep -s 5
}
Else {
$Ezines = Get-ChildItem -LiteralPath .\AppSecEzine\Ezines -File -ErrorAction SilentlyContinue | Sort-Object { Try { [int]$_.Name.Substring(0, $_.Name.IndexOf('-')) }Catch {}; }
$EzineObjects = [System.Collections.Generic.List[PSCustomObject]]::New()
#Extract metadata
$Pattern = '^#+\s+Week:\s+(?<Week>\d+)\s+\|\s+Month:\s+(?<Month>\w+)\s+\|\s+Year:\s+(?<Year>\d+)\s+\|\s+Release\s+Date:\s+(?<ReleaseDate>\S+)\s+\|\s+Edition:\s+#*(?<Edition>\d+)º*\s+#+$'
ForEach ($Ezine in $Ezines) {
$EzineContent = Get-Content $Ezine.FullName -Encoding UTF8
If ($EzineContent[6] -match $Pattern) {
$Week = [int]$matches.Week
$Month = $matches.Month
$Year = [int]$matches.Year
$ReleaseDate = $matches.ReleaseDate
$Edition = [int]$matches.Edition
}
Else {
#This script needs to be adjusted
Write-Error -Message "Ezine content not match pattern. Skipping '$($Ezine.Name)'"
Continue
}
$Section = $null
#Parsing
$SmallPattern = '^(?<Prop>\w+):\s+(?<Val>.+)$'
$EzineContent | ForEach-Object {
Switch ($_) {
''' ╔╦╗┬ ┬┌─┐┌┬┐ ╔═╗┌─┐┌─┐' { $Section = 'MustSee' }
''' ╦ ╦┌─┐┌─┐┬┌─' { $Section = 'Hack' }
''' ╔═╗┌─┐┌─┐┬ ┬┬─┐┬┌┬┐┬ ┬' { $Section = 'Security' }
''' ╔═╗┬ ┬┌┐┌' { $Section = 'Fun' }
}
$uri = $null
If ($Section) {
If ($_ -eq [String]::Empty) {
If ($Flag) {
$EzineObjects.Add($EzineObject)
}
$EzineObject = [PSCustomObject]@{
Week = $Week
Month = $Month
Year = $Year
ReleaseDate = $ReleaseDate
Edition = $Edition
Section = $Section
}
$Flag = 0
}
ElseIf ($_ -match $SmallPattern) {
If ($matches.Prop -in 'URL', 'Description') {
$Prop = $matches.Prop
$Val = $matches.Val
If ($Prop -eq 'URL') {
$Val = $Val -replace '\s.+$', '' #repair bad URLs
Try { $uri = [uri]$Val } Catch { $uri = $null }
}
}
Else {
$Prop = 'Other'
$Val = $matches.Val
}
#The following is not good, but it works
If ($Prop -in $EzineObject.psobject.Members.Name) {
$EzineObject.$Prop = $EzineObject.$Prop, $Val -join "`n"
If ($uri) {
$EzineObject.WebSite = $EzineObject.WebSite, $uri.Host -join "`n"
}
}
Else {
$EzineObject | Add-Member -MemberType NoteProperty -Name $Prop -Value $Val
If ($uri) {
$EzineObject | Add-Member -MemberType NoteProperty -Name 'WebSite' -Value $uri.Host
}
}
$Flag = 1
}
}
}
}
}
#Output JSON file
$EzineObjects | ConvertTo-Json | Set-Content -LiteralPath .\AppSecEzine-pbi.json -Force -Encoding UTF8