Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Created March 27, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0w3rsh3ll/d6b29c70325d80123552 to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/d6b29c70325d80123552 to your computer and use it in GitHub Desktop.
Enum Enabled {
True
False
}
Enum Action {
Allow
Block
NotConfigured
}
[DscResource()]
class ClassFirewallProfile {
[DscProperty(Key)]
[string]$Name
[DscProperty(Mandatory)]
[Enabled]$Enabled
[DscProperty(Mandatory)]
[Action]$DefaultInboundAction
[DscProperty(Mandatory)]
[Action]$DefaultOutboundAction
[ClassFirewallProfile] Get() {
$p = Get-NetFirewallProfile -Name $this.Name -ErrorAction SilentlyContinue
$r = @{
Name = [String]$p.Name
Enabled = [String]$p.Enabled
DefaultInboundAction = [String]$p.DefaultInboundAction
DefaultOutboundAction = [String]$p.DefaultOutboundAction
}
return $r
}
[void] Set() {
Write-verbose "Changing firewall profile $($this.Name)"
Set-NetFirewallProfile -Enabled "$($this.Enabled)"`
-Name "$($this.Name)"`
-DefaultInboundAction "$($this.DefaultInboundAction)"`
-DefaultOutboundAction "$($this.DefaultOutboundAction)"
Write-Verbose -Message "Successfully set the firewall profile $($this.Name)"
}
[bool] Test() {
$p = Get-NetFirewallProfile -Name $this.Name -ErrorAction SilentlyContinue
$bool = $true
if(($this.Enabled) -ne ($p.Enabled).ToString()) {
Write-Verbose "Firewall profile $($this.Name) is not $($this.Enabled)"
$bool = $false
}
if($this.DefaultInboundAction -ne ($p.DefaultInboundAction).ToString()) {
Write-Verbose ("Firewall profile {0} DefaultInboundAction is not {1} but is set to {2}" -f $($this.Name),
$($this.DefaultInboundAction),$($p.DefaultInboundAction))
$bool = $false
}
if($this.DefaultOutboundAction -ne ($p.DefaultOutboundAction).ToString()) {
Write-Verbose ("Firewall profile {0} DefaultOutboundAction is not {1} but is set to {2}" -f $($this.Name),
$($this.DefaultOutboundAction),$($p.DefaultOutboundAction))
$bool = $false
}
return $bool
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment