-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-WLanProfiles.ps1
163 lines (126 loc) · 4.71 KB
/
Get-WLanProfiles.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function Get-WLANProfile
{
<#
.SYNOPSIS
Get WLAN profiles, include password as SecureString or as plain text
.DESCRIPTION
Get WLAN profiles on your local system. Must be admin to get clear text key content.
.EXAMPLE
Get-WLANProfile
.EXAMPLE
Get-WLANProfile -ShowPassword
#>
[CmdletBinding()]
param(
[Parameter(Position=0, ValueFromPipeLine=$true, HelpMessage='List of computers or sessions to run the script against.')]
[Alias('cn')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(HelpMessage='Indicates that the password appears in plain text')]
[Switch]$ShowPassword
)
Begin
{
$resultObjects = @();
$scriptBlock = {
[CmdletBinding()]
param(
[Parameter(
Position=0,
HelpMessage='Indicates that the password appears in plain text')]
[Switch]$ShowPassword
)
$WLAN_Names = @();
$WLAN_Profiles = @();
$TextInfo = (Get-Culture).TextInfo;
# Get all WLAN Profiles from netsh
$Netsh_WLANProfiles = (netsh WLAN show profiles);
# Filter result and get the wlan profile names
# Matches GPO and non GPO profiles.
foreach($Line in $Netsh_WLANProfiles)
{
if($Line -match "^\s+")
{
if($Line.Contains(":"))
{
$WLAN_Names += $Line.Split(':')[1].Trim();
}
else
{
$WLAN_Names += $Line.Trim();
};
};
};
# Get details from every wlan profile, using the name (ssid/password/authentification/etc.)
foreach($WLAN_Name in $WLAN_Names)
{
Write-Verbose "Command: netsh WLAN show profiles name=$WLAN_Name key=clear"
$hashTable = @{};
$Netsh_WLANProfile = (netsh WLAN show profiles name="$WLAN_Name" key=clear);
$section = "";
foreach($Line in $Netsh_WLANProfile)
{
Write-Verbose $Line;
switch($Line.Trim())
{
'Profile information' { $section = 'Profile'; break;};
'Connectivity settings' { $section = 'Connectivity'; break;};
'Security settings' { $section = 'Security'; break;};
'Cost settings' { $section = 'Cost'; break;};
};
if([string]::IsNullOrEmpty($Line)) {$section = [string]::Empty; };
if($Line.Contains(':'))
{
$Line = $Line.Trim() -replace '\x00','';
$kvp = $Line.Trim().Split(":",[System.StringSplitOptions]::RemoveEmptyEntries) | %{$_.Trim()};
if($kvp -and $kvp.Count -eq 2)
{
$camelCase = $TextInfo.ToTitleCase($kvp[0].Trim().ToLower()).Replace(' ','')
if($camelCase -eq "Applied")
{
$hashTable[ "$($camelCase)"] = $kvp[1].Trim();
}
elseif($camelCase -eq "KeyContent")
{
if($ShowPassword.IsPresent) { $hashTable[ "$($section)_$($camelCase)"] = $kvp[1].Trim(); }
else { $hashTable[ "$($section)_$($camelCase)"] = "*".PadLeft(8,'*'); }
}
else
{
$hashTable[ "$($section)_$($camelCase)"] = $kvp[1].Trim();
};
}; #END: if Key value pair count
}; #END: if isKeyValuePair candidate
}; #END: foreach line in profile
# Built the custom PSObject
$WLAN_Profiles += New-Object PSCustomObject -Property $hashTable;
}; #END: Foreach profile
return $WLAN_Profiles;
}; #END ScriptBlock
}; #END: BEGIN
Process
{
if($ComputerName)
{
foreach($obj in $ComputerName)
{
# LOCAL INVOKATION
if($obj.GetType().Name -eq "String" -and $obj.ToUpper() -eq $env:COMPUTERNAME.ToUpper())
{
$resultObjects += Invoke-Command $scriptBlock -ArgumentList $ShowPassword
}
# REMOTE SESSION
elseif($obj.GetType().Name -eq "PSSession")
{
$resultObjects += Invoke-Command -Session $obj -ScriptBlock $scriptBlock -ArgumentList $ShowPassword
}
# REMOTE HOST NAME
elseif ($obj.GetType().Name -eq "String")
{
$resultObjects += Invoke-Command -ComputerName $obj -ScriptBlock $scriptBlock -ArgumentList $ShowPassword
}
}
}
}; #END: Process block
End{ return $resultObjects; };
}; #END
Get-WLANProfile