-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-SelectUniquePairsCSV.ps1
More file actions
91 lines (78 loc) · 3.39 KB
/
08-SelectUniquePairsCSV.ps1
File metadata and controls
91 lines (78 loc) · 3.39 KB
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
# Bitpusher
# \`._,'/
# (_- -_)
# \o/
# The Digital
# Fox
# @VinceVulpes
# https://theTechRelay.com
# https://github.com/bitpusher2k
#
# SelectUniquePairsCSV.ps1 - By Bitpusher/The Digital Fox
# v3.1 last updated 2025-07-26
# Imports a CSV, prompts for the names of two columns in the CSV, and exports
# a new CSV containing unique pairs of values from those two columns.
# Useful for manually finding patterns in logs, correlating things like IPs with Sessions.
#
# Usage:
# powershell -executionpolicy bypass -f .\SelectUniquePairsCSV.ps1 -inputFile "Path\to\input\log.csv" -outputFile "Path\to\output\file.csv" -IPcolumn "IP Column Name" -InfoSource "IP service to use" -APIKey "API key if required for service"
#
# Use with DropShim.bat to allow drag-and-drop processing of CSV files (logs, etc.) with an IP column, either singly or in bulk.
#
#comp #m365 #security #bec #script #logs #IP #csv #unique #sort #rows #irscript #powershell
#Requires -Version 5.1
param(
[string[]]$inputFiles = @("UALexport.csv"),
[string]$outputFile = "UALexport_Processed.csv",
[string]$FirstColumn,
[string]$SecondColumn,
[string]$scriptName = "SelectUniquePairsCSV",
[string]$Priority = "Normal",
[int]$RandMax = "500",
[string]$DebugPreference = "SilentlyContinue",
[string]$VerbosePreference = "SilentlyContinue",
[string]$InformationPreference = "Continue",
[string]$logFileFolderPath = "C:\temp\log",
[string]$ComputerName = $env:computername,
[string]$ScriptUserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name,
[string]$logFilePrefix = "$scriptName" + "_" + "$ComputerName" + "_",
[string]$logFileDateFormat = "yyyyMMdd_HHmmss",
[int]$logFileRetentionDays = 30
)
$RowCount = 0
$LookupCount = 0
$total = [Diagnostics.StopWatch]::StartNew()
foreach ($inputFile in $inputfiles) {
# Load spreadsheet
Write-Output "`nLoading $inputFile..."
$Spreadsheet = Import-Csv -Path "$inputFile"
$Headers = $Spreadsheet | Get-Member -MemberType NoteProperty | Select-Object Name
Write-Output "`nColumn headers found in CSV:"
$Headers.Name
Write-Output "Enter two column headers to pull from CSV and filter for unique combinations."
if (!$FirstColumn) {
$FirstColumn = Read-Host "`nFirst column"
}
if (!$SecondColumn) {
$SecondColumn = Read-Host "Second column"
}
Write-Output "`nColumns '$FirstColumn' and '$SecondColumn' selected..."
if ($Headers.Name -notcontains $FirstColumn) {
Write-Output "`n$FirstColumn column not found in CSV - exiting."
exit
}
if ($Headers.Name -notcontains $SecondColumn) {
Write-Output "`n$SecondColumn column not found in CSV - exiting."
exit
}
$Selection = $Spreadsheet | Select-Object $FirstColumn,$SecondColumn
$UniqueRows = $Selection | Sort-Object $FirstColumn,$SecondColumn -Unique
# Export updated spreadsheet data to CSV file
[string]$outputFolder = Split-Path -Path $inputFile -Parent
[string]$outputFile = (Get-Item $inputFile).BaseName
[string]$outputPath = $outputFolder + "\" + $outputFile + "_$($FirstColumn)_$($SecondColumn)_UniqueRows.csv"
$UniqueRows | Export-Csv -Path "$outputPath" -NoTypeInformation
}
Write-Output "`nTotal time for script execution: $($total.elapsed.totalseconds)"
Write-Output "Script complete!"
exit