forked from loxia01/PSInternetConnectionSharing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSInternetConnectionSharing.psm1
More file actions
354 lines (290 loc) · 13.1 KB
/
PSInternetConnectionSharing.psm1
File metadata and controls
354 lines (290 loc) · 13.1 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
function Set-Ics
{
<#
.SYNOPSIS
Enables Internet Connection Sharing (ICS) for a specified network connection pair.
.DESCRIPTION
Set-Ics lets you share the internet connection of a network connection (called the public
connection) with another network connection (called the private connection).
The specified network connections must exist beforehand. In order to be able to set ICS,
the function will first disable ICS for any existing network connections.
.PARAMETER PublicConnectionName
The name of the network connection that internet connection will be shared from.
.PARAMETER PrivateConnectionName
The name of the network connection that internet connection will be shared with.
.PARAMETER PassThru
If this parameter is specified Set-ICS returns an output with the set connections.
Optional. By default Set-ICS does not generate any output.
.PARAMETER WhatIf
Shows what would happen if the function runs. The function is not run.
.PARAMETER Confirm
Prompts you for confirmation before each change the function makes.
.EXAMPLE
Set-Ics -PublicConnectionName 'Ethernet' -PrivateConnectionName 'VM Host-Only Network'
# Sets ICS for the specified public and private connections.
.EXAMPLE
Set-Ics Ethernet 'VM Host-Only Network'
# Sets ICS for the specified public and private connections.
.EXAMPLE
Set-Ics Ethernet 'VM Host-Only Network' -PassThru
# Sets ICS for the specified public and private connections and generates an output.
.INPUTS
Set-ICS does not take pipeline input.
.OUTPUTS
Default is no output. If parameter PassThru is specified Set-ICS returns a PSCustomObject.
.NOTES
Set-Ics requires elevated permissions. Use the Run as administrator option when starting PowerShell.
Testing for administrator rights is done in the beginning of function.
.LINK
Online version: https://github.com/loxia01/PSInternetConnectionSharing#set-ics
Get-Ics
Disable-Ics
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[string]$PublicConnectionName,
[Parameter(Mandatory)]
[string]$PrivateConnectionName,
[switch]$PassThru
)
begin
{
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator'))
{
Write-Error "This function requires administrator rights. Restart PowerShell using the Run as Administrator option."`
-Category PermissionDenied -ErrorAction Stop
}
regsvr32 /s hnetcfg.dll
$netShare = New-Object -ComObject HNetCfg.HNetShare
$connectionsProps = $netShare.EnumEveryConnection | ForEach-Object {
$netShare.NetConnectionProps.Invoke($_) } | Where-Object Status -NE $null
if ($connectionsProps.Name -notcontains $PublicConnectionName)
{
Write-Error "'${PublicConnectionName}' is not a valid network connection name." -Category InvalidArgument -ErrorAction Stop
}
if ($connectionsProps.Name -notcontains $PrivateConnectionName)
{
Write-Error "'${PrivateConnectionName}' is not a valid network connection name." -Category InvalidArgument -ErrorAction Stop
}
if ($connectionsProps.Name.Where({$_.Name -eq $PrivateConnectionName}).Status -eq 0)
{
Write-Error "Private connection '${PrivateConnectionName}' must be enabled to set ICS." -Category NotEnabled -ErrorAction Stop
}
$publicConnectionName = $connectionsProps.Name.Where({$_ -eq $PublicConnectionName})
$privateConnectionName = $connectionsProps.Name.Where({$_ -eq $PrivateConnectionName})
}
process
{
$publicConnection = $netShare.EnumEveryConnection | Where-Object {$netShare.NetConnectionProps.Invoke($_).Name -eq $publicConnectionName}
$publicConnectionConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($publicConnection)
$privateConnection = $netShare.EnumEveryConnection | Where-Object {$netShare.NetConnectionProps.Invoke($_).Name -eq $privateConnectionName}
$privateConnectionConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection)
if (-not (($publicConnectionConfig.SharingEnabled -eq $true -and $publicConnectionConfig.SharingConnectionType -eq 0) -and
($privateConnectionConfig.SharingEnabled -eq $true -and $privateConnectionConfig.SharingConnectionType -eq 1)))
{
foreach ($connectionName in $connectionsProps.Name)
{
$connection = $netShare.EnumEveryConnection | Where-Object {$netShare.NetConnectionProps.Invoke($_).Name -eq $connectionName}
$connectionConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($connection)
if ($connectionConfig.SharingEnabled -eq $true)
{
if ($PSCmdlet.ShouldProcess($connectionName, "DisableICS")) { $connectionConfig.DisableSharing() }
}
}
if ($PSCmdlet.ShouldProcess($publicConnectionName)) { $publicConnectionConfig.EnableSharing(0) }
if ($PSCmdlet.ShouldProcess($privateConnectionName)) { $privateConnectionConfig.EnableSharing(1) }
}
}
end
{
if ($PassThru -and ($WhatIfPreference -eq $false)) { Get-Ics -ConnectionNames $publicConnectionName, $privateConnectionName }
}
}
function Get-Ics
{
<#
.SYNOPSIS
Retrieves status of Internet Connection Sharing (ICS) for all network connections,
or optionally for the specified network connections.
.DESCRIPTION
Retrieves status of Internet Connection Sharing (ICS) for all network connections,
or optionally for the specified network connections. Output is in the form of a PSCustomObject.
.PARAMETER ConnectionNames
Name(s) of the network connection(s) to get ICS status for. Optional.
.PARAMETER HideDisabled
By default Get-Ics lists ICS status for all network connections if parameter ConnectionNames is omitted.
When adding parameter HideDisabled, Get-Ics only lists connections where ICS is enabled.
.EXAMPLE
Get-Ics
# Gets status for all network connections.
.EXAMPLE
Get-Ics -HideDisabled
# Gets status for all network connections with ICS enabled.
.EXAMPLE
Get-Ics -ConnectionNames Ethernet, Ethernet2, 'VM Host-Only Network'
# Gets status for the specified network connections.
.EXAMPLE
Get-Ics Ethernet, Ethernet2, 'VM Host-Only Network'
# Gets status for the specified network connections.
.INPUTS
Get-ICS does not take pipeline input.
.OUTPUTS
PSCustomObject.
.NOTES
Get-Ics requires elevated permissions. Use the Run as administrator option when starting PowerShell.
Testing for administrator rights is done in the beginning of function.
.LINK
Online version: https://github.com/loxia01/PSInternetConnectionSharing#get-ics
Set-Ics
Disable-Ics
#>
[CmdletBinding()]
param (
[string[]]$ConnectionNames,
[switch]$HideDisabled
)
begin
{
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator'))
{
Write-Error "This function requires administrator rights. Restart PowerShell using the Run as Administrator option."`
-Category PermissionDenied -ErrorAction Stop
}
regsvr32 /s hnetcfg.dll
$netShare = New-Object -ComObject HNetCfg.HNetShare
if ((Get-PSCallStack)[1].Command -notmatch '(Disable|Set)-Ics')
{
$connectionsProps = $netShare.EnumEveryConnection | ForEach-Object {
$netShare.NetConnectionProps.Invoke($_) } | Where-Object Status -NE $null
if ($ConnectionNames)
{
foreach ($ConnectionName in $ConnectionNames)
{
if ($connectionsProps.Name -notcontains $ConnectionName)
{
Write-Error "'${ConnectionName}' is not a valid network connection name." -Category InvalidArgument -ErrorAction Stop
}
}
$connectionNames = $connectionsProps.Name.Where({$_ -in $ConnectionNames})
}
else
{
$connectionNames = $connectionsProps.Name
}
}
else
{
if (-not $ConnectionNames) { $connectionNames = $connectionsProps.Name }
}
}
process
{
$output = foreach ($connectionName in $connectionNames)
{
$connection = $netShare.EnumEveryConnection | Where-Object {$netShare.NetConnectionProps.Invoke($_).Name -eq $connectionName}
$connectionConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($connection)
if ($connectionConfig.SharingEnabled -eq $false)
{
[pscustomobject]@{NetworkConnectionName=$connectionName + " "; StatusICS='Disabled'; ConnectionType=$null}
}
if ($connectionConfig.SharingEnabled -eq $true -and $connectionConfig.SharingConnectionType -eq 0)
{
[pscustomobject]@{NetworkConnectionName=$connectionName + " "; StatusICS='Enabled'; ConnectionType='Public'}
}
if ($connectionConfig.SharingEnabled -eq $true -and $connectionConfig.SharingConnectionType -eq 1)
{
[pscustomobject]@{NetworkConnectionName=$connectionName + " "; StatusICS='Enabled'; ConnectionType='Private'}
}
}
}
end
{
if ($HideDisabled)
{
$output | Sort-Object ConnectionType -Descending | Where-Object StatusICS -NE Disabled
}
elseif ($output.ConnectionType -match '.+')
{
$output | Sort-Object StatusICS, ConnectionType -Descending
}
else
{
$output | Select-Object NetworkConnectionName, StatusICS
}
}
}
function Disable-Ics
{
<#
.SYNOPSIS
Disables Internet Connection Sharing (ICS) for all network connections.
.DESCRIPTION
Disable-Ics checks for if ICS is enabled for any network connections and, if so,
disables ICS for those connections.
.PARAMETER PassThru
If this parameter is specified Disable-ICS returns an output with the disabled connections.
Optional. By default Disable-ICS does not generate any output.
.PARAMETER WhatIf
Shows what would happen if the function runs. The function is not run.
.PARAMETER Confirm
Prompts you for confirmation before each change the function makes.
.EXAMPLE
Disable-Ics
# Disables ICS for all connections.
.EXAMPLE
Disable-Ics -PassThru
# Disables ICS for all connections and generates an output.
.INPUTS
Disable-ICS does not take pipeline input.
.OUTPUTS
Default is no output. If parameter PassThru is specified Disable-ICS returns a PSCustomObject.
.NOTES
Disable-Ics requires elevated permissions. Use the Run as administrator option when starting PowerShell.
Testing for administrator rights is done in the beginning of function.
.LINK
Online version: https://github.com/loxia01/PSInternetConnectionSharing#disable-ics
Set-Ics
Get-Ics
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[switch]$PassThru
)
begin
{
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator'))
{
Write-Error "This function requires administrator rights. Restart PowerShell using the Run as Administrator option."`
-Category PermissionDenied -ErrorAction Stop
}
regsvr32 /s hnetcfg.dll
$netShare = New-Object -ComObject HNetCfg.HNetShare
$connectionsProps = $netShare.EnumEveryConnection | ForEach-Object {
$netShare.NetConnectionProps.Invoke($_) } | Where-Object Status -NE $null
}
process
{
$disabledNames = @()
foreach ($connectionName in $connectionsProps.Name)
{
$connection = $netShare.EnumEveryConnection | Where-Object {$netShare.NetConnectionProps.Invoke($_).Name -eq $connectionName}
$connectionConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($connection)
if ($connectionConfig.SharingEnabled -eq $true)
{
if ($PSCmdlet.ShouldProcess($connectionName))
{
$connectionConfig.DisableSharing()
$disabledNames += $connectionName
}
}
}
}
end
{
if ($PassThru -and ($WhatIfPreference -eq $false)) { Get-Ics $disabledNames }
}
}