-
Notifications
You must be signed in to change notification settings - Fork 14
/
Close-Port.ps1
41 lines (38 loc) · 2.46 KB
/
Close-Port.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
function Close-Port
{
<#
.Synopsis
Opens ports on the Windows Firewall
.Description
Opens ports on the Windows Firewall on the local machine
.Example
Close-Port 500
.Link
Open-Port
#>
[OutputType([Nullable])]
param(
# The port numbers to close
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Uint32[]]$Port,
# The protocol to close. The Default is TCP.
[string[]]$Protocol = 'TCP'
)
process {
#region Initialize Profile
$firewall = New-Object -ComObject HNetCfg.FwMgr
$firewallProfile = $firewall.localpolicy.currentprofile
#endregion Initialize Profile
#region Close Each Port
foreach ($p in $port) {
$toClose = $firewallProfile.GloballyOpenPorts |
Where-Object { $_.Port -eq $p }
if ($protocol -eq 'TCP') {
$firewallProfile.GloballyOpenPorts.Remove($toClose, 6)
} elseif ($protocol -eq 'UDP') {
$firewallProfile.GloballyOpenPorts.Remove($toClose, 17)
}
}
#endregion Close Each Port
}
}