Tuesday, 23 March 2010

Get-FirewallStatus.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the status of the host firewall 
  4.     and ensures the firewall IS running! 
  5. .DESCRIPTION 
  6.     This script gets the status and displays it to the 
  7.     console. The script also turns on the firewall if it's 
  8.     currently off. It's a simpler script than in MSDN for VBScript! 
  9. .NOTES 
  10.     File Name  : Get-FirewallStatus.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2010/03/get-firewallstatusps1.html 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/aa366442%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-FirewallStatus.ps1 
  20.     Firewall Enabled               : True 
  21.     Firewall Exceptions Not Allowed: False 
  22. #> 
  23.  
  24. ## 
  25. # Start Script 
  26. ## 
  27.   
  28. # Create the firewall manager object. 
  29. $fwMgr = New-Object -com HNetCfg.FwMgr 
  30.   
  31. # Get the current profile for the local firewall policy. 
  32. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  33.   
  34. # Verify that the Firewall is enabled. If it isn't, then enable it. 
  35. if (!$profile.FirewallEnabled)  
  36.     {$profile.FirewallEnabled = $TRUE
  37.  
  38. # Display details 
  39. "Firewall Enabled               : {0}" -f $profile.FirewallEnabled 
  40. "Firewall Exceptions Not Allowed: {0}" -f $profile.ExceptionsNotAllowed 
  41. # End Script 

2 comments:

Pat Richard, MCSE MVP said...

Creating an instance of the COM component with CLSID {304CE942-6E39-40D8-943A-B913C40C9CD4} from the IClassFactory failed due to the following error: 800706d9.
At :line:29 char:19
+ $fwMgr = New-Object <<<< -com HNetCfg.FwMgr

The term 'profile.FirewallEnabled' is not recognized as the name of a cmdlet, function, script file, or operable progra
m. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\administrator.INNERVATION\Desktop\Get-FirewallStatus.ps1:36 char:29
+ {profile.FirewallEnabled <<<< = $TRUE}
+ CategoryInfo : ObjectNotFound: (profile.FirewallEnabled:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Thomas said...

The first error is a problem with DCOM at your end - not sure of the cause. Second error looks like a typo and is now fixed.

Ping me off line if the DCOM issue persists.