wificonnectionsbackup/wificonnectionbackup_v2.ps1

56 lines
1.8 KiB
PowerShell

# Create output folder if it doesn't exist
$folderPath = "C:\Tech"
if (-Not (Test-Path -Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath
}
# Define the API endpoint
$apiEndpoint = "http://10.0.10.26:8000/wifi_credentials/add"
# Define headers
$headers = @{
"Content-Type" = "application/json"
}
# Get WiFi profiles
$wifiProfiles = netsh wlan show profiles | Select-String "All User Profile" | ForEach-Object { ($_ -split ":")[1].Trim() }
# Initialize array for WiFi data
$wifiData = @()
foreach ($profile in $wifiProfiles) {
# Extract PSK if available
$profileData = netsh wlan show profile name="$profile" key=clear | Select-String "Key Content" | ForEach-Object { ($_ -split ":")[1].Trim() }
# Only add profiles with a PSK to the array
if ($profileData) {
# Create JSON payload
$payload = @{
ssid = $profile
preshared_key = $profileData
}
# Send data to the Flask API
try {
$response = Invoke-RestMethod -Uri $apiEndpoint -Method Post -Body ($payload | ConvertTo-Json -Depth 10) -Headers $headers
Write-Output "Success: SSID '$profile' added. Message: $($response.message)"
} catch {
Write-Output "Error: Could not add SSID '$profile'. Error: $($_.Exception.Message)"
}
# Add to local data array
$wifiData += [PSCustomObject]@{
SSID = $profile
PSK = $profileData
}
}
}
# Export data to CSV
$csvPath = Join-Path -Path $folderPath -ChildPath "wifi_data.csv"
$wifiData | Export-Csv -Path $csvPath -NoTypeInformation
# Export data to JSON
$jsonPath = Join-Path -Path $folderPath -ChildPath "wifi_data.json"
$wifiData | ConvertTo-Json -Depth 10 | Out-File -FilePath $jsonPath