Verifying Windows Firewall is Enabled
The following example shows a verification of the status of the Windows Firewall.
Option Explicit 'Create Shell object Dim objShell set objShell = CreateObject("Shell.Application") 'Declare Firewall variables Dim fwMgr Dim profile 'Verify that the SharedAccess service is running. If it isn't, then start it. If objShell.IsServiceRunning("SharedAccess") = FALSE Then objShell.ServiceStart "SharedAccess", TRUE 'Sleep 1 second to make sure the service is started ' before trying to create the objects below. If there ' is no sleep, then the script is too fast and the ' firewall objects can't be created. WScript.Sleep 1000 End If WScript.Echo("SharedAccess is running: " & objShell.IsServiceRunning("SharedAccess")) ' Firewall objects have to be created after making sure ' the service is running. If the service isn't running, ' the script will fail. ' Create the firewall manager object. Set fwMgr = CreateObject("HNetCfg.FwMgr") ' Get the current profile for the local firewall policy. Set profile = fwMgr.LocalPolicy.CurrentProfile 'Verify that the Firewall is enabled. If it isn't, then enable it. If profile.FirewallEnabled = FALSE Then profile.FirewallEnabled = TRUE End If WScript.Echo("Firewall Enabled: " & profile.FirewallEnabled) WScript.Echo("Firewall Exceptions Not Allowed: " & profile.ExceptionsNotAllowed)
Send comments about this topic to Microsoft
Build date: 2/14/2012
<#
.SYNOPSIS
This script gets the status of the host firewall
and ensures the firewall IS running!
.DESCRIPTION
This script gets the status and displays it to the
console. The script also turns on the firewall if it's
currently off. It's a simpler script than in MSDN for VBScript!
.NOTES
File Name : Get-FirewallStatus.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/aa366442%28VS.85%29.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-FirewallStatus.ps1
Firewall Enabled : True
Firewall Exceptions Not Allowed: False
#>
##
# Start Script
##
# Create the firewall manager object.
$fwMgr = New-Object -com HNetCfg.FwMgr
# Get the current profile for the local firewall policy.
$profile = $fwMgr.LocalPolicy.CurrentProfile
# Verify that the Firewall is enabled. If it isn't, then enable it.
if (!$profile.FirewallEnabled)
{profile.FirewallEnabled = $TRUE}
# Display details
"Firewall Enabled : {0}" -f $profile.FirewallEnabled
"Firewall Exceptions Not Allowed: {0}" -f $profile.ExceptionsNotAllowed
# End Script
- 3/22/2010
- Thomas Lee
- 3/6/2011
- markspansel