-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTervisNetSecurity.psm1
92 lines (85 loc) · 3.1 KB
/
TervisNetSecurity.psm1
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
$ModulePath = if ($PSScriptRoot) {
$PSScriptRoot
} else {
(Get-Module -ListAvailable TervisNetSecurity).ModuleBase
}
. $ModulePath\NetSecurityDefinition.ps1
function Get-TervisNetFirewallGroup {
param (
$Name
)
$FirewallRuleGroups | where Name -EQ $Name
}
function Enable-TervisNetFirewallRuleGroup {
param (
$Name,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]$ComputerName,
$Credential = [System.Management.Automation.PSCredential]::Empty
)
process {
$FirewallRuleNames = $Name |
Get-TervisNetFirewallGroupRule |
Sort-Object -Unique
Enable-NetFirewallRule -ComputerName $ComputerName -Credential $Credential -Name $FirewallRuleNames
}
}
function Get-TervisNetFirewallGroupRule {
param (
[Parameter(Mandatory, ValueFromPipeline)]$Name
)
process {
$FirewallGroup = Get-TervisNetFirewallGroup -Name $Name
if ($FirewallGroup.FirewallRuleGroupsToImport) {
$FirewallGroup.FirewallRuleGroupsToImport | Get-TervisNetFirewallGroupRule
}
$FirewallGroup.FirewallRule
}
}
function Enable-NetFirewallRule {
param (
[Parameter(Mandatory)]$ComputerName,
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory)]$Name
)
Invoke-Command -ComputerName $ComputerName -Credential $Credential {
$Using:Name | foreach {
Get-NetFirewallRule -Name $_ |
Set-NetFirewallRule -Enabled True
}
}
}
function Compare-NetFirewallRuleBetweenComputers {
param (
$ReferenceComputer,
$DifferenceComputer
)
$ReferenceRules = invoke-command -ScriptBlock {get-netfirewallrule} -ComputerName $ReferenceComputer
$DifferenceRules = invoke-command -ScriptBlock {get-netfirewallrule} -ComputerName $DifferenceComputer
Compare-Object -ReferenceObject $ReferenceRules -DifferenceObject $DifferenceRules -Property Name,Enabled
}
function New-TervisFirewallRule {
param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]$ComputerName,
[Parameter(Mandatory)]$DisplayName,
[Parameter(Mandatory)]$LocalPort,
[Parameter(Mandatory)]$Name,
[Parameter(Mandatory)]$Group,
[ValidateSet("Inbound","Outbound")]$Direction = "Inbound",
[ValidateSet("Allow","Block","NotConfigured")]$Action = "Allow",
$Protocol = "TCP",
$RemoteAddress,
[Switch]$Force
)
begin {
$FirewallSplatVariable = New-SplatVariable -Invocation $MyInvocation -Variables (Get-Variable) -ExcludeProperty ComputerName,Force
}
process {
$CimSession = New-CimSession -ComputerName $ComputerName
$FirewallRule = Get-NetFirewallRule -DisplayName $DisplayName -ErrorAction SilentlyContinue -CimSession $CimSession
if (-not $FirewallRule -or $Force) {
Remove-NetFirewallRule -Name $Name -ErrorAction SilentlyContinue -CimSession $CimSession
New-NetFirewallRule @FirewallSplatVariable -CimSession $CimSession
}
Remove-CimSession -CimSession $CimSession
}
}