-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yubico_CSV_Conversion_Tool_For_Duo.ps1
105 lines (85 loc) · 5.04 KB
/
Yubico_CSV_Conversion_Tool_For_Duo.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
<#---Yubico CSV Conversion Tool for Duo---
AUTHOR
Chris Streeks
https://github.com/chris-streeks/Yubico_CSV_Conversion_Tool_For_Duo
DESCRIPTION
Allows you to quickly convert a standard YubiOTP format CSV file as issued by the Yubico Production Team
or generated by the Yubico Personalization Tool into a format that Duo is able to readily import.
This script is non-destructive and will just generate a new file named <selectedfilename>_ConvertedForDuo with
no modifications to the source material.
#>
clear-host
Write-Host "Yubico CSV Conversion Tool for Duo" -ForegroundColor Cyan
write-host "-`n# Step 1: Select the CSV file"
$psversion = $host.version #used to tailor script based on powershell age
$ostype = [System.Environment]::OSVersion.Platform #used to tailor script based on operating system
#For Windows
if ($ostype -eq "Win32NT"){
read-host "Press [Enter] to choose the .CSV file you'd like to convert"
## Using the Windows GUI to grab the CSV file path from the user
Add-Type -AssemblyName System.Windows.Forms
$pathToCSV = New-Object System.Windows.Forms.OpenFileDialog
$pathToCSV.Filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|Excel Worksheet (*.xls)|*.xls|All Files (*.*)|*.*"
$pathToCSV.SupportMultiDottedExtensions = $true;
if($pathToCSV.ShowDialog() -eq 'Ok'){
$pathToCSV = $pathToCSV.filename
}
else{
exit;
}
}
#For all other operating systems that aren't Windows
else{
$pathToCSV = read-host "Enter the full path to the .CSV file you'd like to convert"
if (!(test-path $pathToCSV)){
write-host "Error: I couldn't find a proper CSV file here."
read-host "Press [Enter] to exit]"
}
}
#Pulling the parent folder of the source CSV file into a variable
$CSVFolderPath = Split-Path -Path $pathToCSV
#Pulling the filename of the source CSV file (sans extension), adding the "converted for duo" bit, then readding the extension.
$ConvertedCSVFilename = [System.IO.Path]::GetFileNameWithoutExtension("$pathtocsv")
$ConvertedCSVFilename = $ConvertedCSVFilename+"_ConvertedForDuo"+".csv"
#Merging the newly generated filename back to its parent path
$ConvertedFullCSVFilepath = Join-Path -path $CSVFolderPath -childpath $ConvertedCSVFilename
write-host "# Step 2: Pre-Conversion Review"
write-host "- Selected CSV filepath: $pathToCSV"
write-host "- Output CSV filepath: $ConvertedFullCSVFilepath"
read-host "`nPress [Enter] to begin"
write-host "Generating converted CSV..."
#Ensuring a converted file does not exist already
if (test-path $ConvertedFullCSVFilepath){
Write-host "Error: Oops. It appears this converted file already exists:`n - $ConvertedFullCSVFilepath`nExiting..."
exit
}
else
{
if ($psversion -ge "7.4.0"){
Import-Csv $pathToCSV -Header SerialNumber,PublicID,PrivateID,SecretKey,AccessCode,Timestamp | Select-Object PublicID,PrivateID,SecretKey | Export-Csv -Path $ConvertedFullCSVFilepath -NoTypeInformation -UseQuotes AsNeeded -NoHeader
}
elseif ($psversion -gt "7.0.0"){
#The '-NoHeader' parameter was added into Powershell 7.4. If this script is being run on an older version of PS, we'll need to do this more manually
$tempfile = '.\Yubico_CSV_Format_To_Duo_CSV_Format_Tempfile'
Import-Csv $pathToCSV -Header SerialNumber,PublicID,PrivateID,SecretKey,AccessCode,Timestamp | Select-Object PublicID,PrivateID,SecretKey | Export-Csv -Path $tempfile -NoTypeInformation -UseQuotes AsNeeded
#We need to remove the header from this CSV file by trimming the first line.
Get-Content $tempfile | select-object -skip 1 | Set-Content $ConvertedFullCSVFilepath
#Now that we're done with the temporary file, let's remove it.
Remove-Item -Force $tempfile
}
else{
#The '-NoHeader' parameter was added into Powershell 7.4. If this script is being run on an older version of PS, we'll need to do this more manually
#The '-UseQuotes' parameter was added into Powershell 7.0. If this script being run on an older version of PS, we'll need to do this more manually.
$tempfile = '.\Yubico_CSV_Format_To_Duo_CSV_Format_Tempfile'
$tempfile2 = '.\Yubico_CSV_Format_To_Duo_CSV_Format_Tempfile2'
Import-Csv $pathToCSV -Header SerialNumber,PublicID,PrivateID,SecretKey,AccessCode,Timestamp | Select-Object PublicID,PrivateID,SecretKey | Export-Csv -Path $tempfile -NoTypeInformation
#We now have our newly trimmed CSV file, but we need to remove all quotes from the file.
Get-Content $tempfile | ForEach-Object {$_ -replace '"',''} | Out-File $tempfile2
#Finally, we need to remove the header from this CSV file by trimming the first line.
Get-Content $tempfile2 | select-object -skip 1 | Set-Content $ConvertedFullCSVFilepath
#Now that we're done with the temporary files, let's remove them
Remove-Item -Force $tempfile
Remove-Item -Force $tempfile2
}
write-host "...Done!"
}