-
Notifications
You must be signed in to change notification settings - Fork 7
/
PC_WifiInfo.ps1
100 lines (87 loc) · 3.81 KB
/
PC_WifiInfo.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
Add-Type -AssemblyName System.Windows.Forms
function Get-WifiPassword {
[CmdletBinding()]
param ()
begin {
try {
# Export all Wifi profiles and collect their XML file paths
$ExportPath = New-Item -Path $HOME -Name ('GetWifiPassword_' + (New-Guid).Guid) -ItemType Directory
$CurrentPath = (Get-Location).Path
Set-Location $ExportPath
netsh wlan export profile key=clear
$XmlFilePaths = Get-ChildItem -Path $ExportPath -File
}
catch {
Write-Error "Failed to export Wifi profiles: $($_.Exception.Message)"
return
}
}
process {
$form = New-Object System.Windows.Forms.Form
$form.Text = "Profils WiFi"
$form.Size = New-Object System.Drawing.Size(600, 400)
$form.StartPosition = "CenterScreen"
$iconPath = Join-Path -Path $PSScriptRoot -ChildPath "tools\res\wifi.ico"
$icon = New-Object System.Drawing.Icon($iconPath)
$form.Icon = $icon
$tableLayoutPanel = New-Object System.Windows.Forms.TableLayoutPanel
$tableLayoutPanel.Dock = 'Fill'
$tableLayoutPanel.ColumnCount = 2
$tableLayoutPanel.ColumnStyles.Add((New-Object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 50)))
$tableLayoutPanel.ColumnStyles.Add((New-Object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 50)))
$wifiInfo = @() # Array to store Wifi information
foreach ($XmlFilePath in $XmlFilePaths) {
try {
# Read the XML file and extract the Wifi profile name and password
# $Xml = [xml](Get-Content -Path $XmlFilePath.FullName)
($Xml = [xml]::new()).Load((Convert-Path -LiteralPath $XmlFilePath.FullName))
if ($Xml.WLANProfile -and $Xml.WLANProfile.Name -and $Xml.WLANProfile.MSM.Security.SharedKey.KeyMaterial) {
$name = $Xml.WLANProfile.Name
Write-Host "$name"
$password = $Xml.WLANProfile.MSM.Security.SharedKey.KeyMaterial
$succeed = $true
if ($succeed) {
$button = New-Object System.Windows.Forms.Button
$button.Text = $name
$button.Tag = @{
SSID = $name
Password = $password
}
$button.Width = 250
$button.Add_Click({
$clickedButton = $this
$form.Tag = $clickedButton.Tag
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Close()
})
$tableLayoutPanel.Controls.Add($button)
}
$wifiInfo += [PSCustomObject]@{
Name = $name
Password = $password
Succeed = $succeed
}
}
else {
$succeed = $false
}
}
catch {
Write-Error "Failed to read Wifi profile from '$XmlFilePath': $($_.Exception.Message)"
}
}
$form.Controls.Add($tableLayoutPanel)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
return $form.Tag
}
else {
return $null
}
}
end {
Set-Location $CurrentPath
# Remove-Item $ExportPath -Confirm:$false -Recurse
}
}
Get-WifiPassword