.SYNOPSIS
Displays the Windows Firewall state for Domain, Private, and Public profiles on local or remote computer.
.DESCRIPTION
Use Get-FirewallState to show current Firewall state that is presented on the Windows Firewall with Advanced Security Properties page, with the tabs for Domain, Private, and Public profiles.
.PARAMETER HOSTNAME
Specifies the remote or local computer name.
When using HOSTNAME parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and is then closed.
.EXAMPLE
Get-FirewallState -HOSTNAME SERVER01
Description
———–
The script will establish remote connection to SERVER01 machine and display the Firewall state for all profiles:
1 2 3 |
FirewallDomain FirewallPrivate FirewallPublic -------------- --------------- -------------- ON ON ON |
Code:
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 |
Function Get-FirewallState { [CmdletBinding()] Param ([Parameter(Mandatory = $true)][string]$HOSTNAME) $ErrorActionPreference = "Stop" 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 FirewallDomain, FirewallPrivate, FirewallPublic } Catch { Write-Host ($_.Exception.Message -split ' For')[0] -ForegroundColor Red } } |