Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Requires -Version 4.0
<#
.SYNOPSIS
Gets the Firewall State for for a local, remote or a piped list of machines
.DESCRIPTION
Will require PowerShell 4.0 or later
Gets Public, Private and Domain Profiles
.EXAMPLE
Gets the firewall state for all domain controllers
Get-ADDomainController -filter * | Get-FirewallState
.PARAMETER Hostname
The host to query
.NOTES
Author: Based on http://power-shell.com/2015/powershell-scripts/get-curent-firewall-status-utilizing-netsh/
I added pipeline support and the inclusion of the hostname in the results
#>
Function Get-FirewallState
{
[CmdletBinding()]
Param ([Parameter(Mandatory = $true, ValueFromPipelineByPropertyName)]
[Alias("Name")]
[string]$Hostname)
Begin{
$ErrorActionPreference = "Stop"
}
Process{
Try {
$FirewallBlock = {
$content = netsh advfirewall show allprofiles
If ($domprofile = $content | Select-String 'Domain Profile' -Context 2 | Out-String)
{ $domainpro = ($domprofile.Substring($domprofile.Length - 9)).Trim()}
Else { $domainpro = $null }
If ($priprofile = $content | Select-String 'Private Profile' -Context 2 | Out-String)
{ $privatepro = ($priprofile.Substring($priprofile.Length - 9)).Trim()}
Else { $privatepro = $null }
If ($pubprofile = $content | Select-String 'Public Profile' -Context 2 | Out-String)
{ $publicpro = ($pubprofile.Substring($pubprofile.Length - 9)).Trim()}
Else { $publicpro = $null }
$FirewallObject = New-Object PSObject
Add-Member -inputObject $FirewallObject -memberType NoteProperty -name "FirewallDomain" -value $domainpro
Add-Member -inputObject $FirewallObject -memberType NoteProperty -name "FirewallPrivate" -value $privatepro
Add-Member -inputObject $FirewallObject -memberType NoteProperty -name "FirewallPublic" -value $publicpro
$FirewallObject
}
Invoke-Command -computerName $HOSTNAME -command $FirewallBlock | Select-Object @{N="Hostname";E={$hostname}},FirewallDomain, FirewallPrivate, FirewallPublic
}
Catch
{
Write-Host ($_.Exception.Message -split ' For')[0] -ForegroundColor Red
}
}
End{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment