{"id":208130,"date":"2023-09-28T13:21:41","date_gmt":"2023-09-28T13:21:41","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/abilitare-disabilitare-i-profili-del-firewall-di-windows-powershell-2\/"},"modified":"2024-03-04T18:45:59","modified_gmt":"2024-03-04T18:45:59","slug":"abilitare-disabilitare-i-profili-del-firewall-di-windows-powershell-2","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/it\/script-hub\/abilitare-disabilitare-i-profili-del-firewall-di-windows-powershell-2\/","title":{"rendered":"Abilitare o disabilitare i profili del firewall di Windows con Powershell"},"content":{"rendered":"<p><span class=\"TextRun SCXW215913654 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW215913654 BCX0\">Garantire la sicurezza di reti e sistemi \u00e8 una pietra angolare dell&#8217;IT. Una delle difese principali in un ambiente Windows \u00e8 il firewall di Windows. Controllando il flusso del traffico in entrata e in uscita, agisce come un gatekeeper. In questo articolo parleremo in modo approfondito di uno <strong>script PowerShell che <\/strong><\/span><strong><span class=\"NormalTextRun SCXW215913654 BCX0\">fornisce<\/span><\/strong><span class=\"NormalTextRun SCXW215913654 BCX0\"><strong> un metodo semplificato per abilitare o disabilitare i profili del firewall di Windows<\/strong>, un&#8217;attivit\u00e0 cruciale per i professionisti IT.<\/span><\/span><\/p>\n<h2>Background<\/h2>\n<p><span class=\"NormalTextRun SCXW120388544 BCX0\">Lo script presentato \u00e8 stato progettato per abilitare o disabilitare i profili del firewall di Windows, ovvero Domain, Public, e Private. Questi profili <\/span><span class=\"NormalTextRun SCXW120388544 BCX0\">determinano<\/span><span class=\"NormalTextRun SCXW120388544 BCX0\"> le impostazioni e le regole applicate in base al tipo di rete a cui il computer \u00e8 collegato. Per i fornitori di servizi gestiti (MSP) e i professionisti IT, uno strumento in grado di passare rapidamente da uno di questi profili all\u2019altro \u00e8 prezioso. Questo script offre una rapida soluzione sia per la risoluzione dei problemi che per il rafforzamento della sicurezza e la configurazione della rete.<\/span><\/p>\n<h2>Lo script per disabilitare i profili del firewall di Windows<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 5.1\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    Enable or disable all Windows Firewall profiles(Domain, Public, Private).\r\n.DESCRIPTION\r\n    Enable or disable all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Disable\r\n    Disables all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Enable\r\n    Enables all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Enable -BlockAllInbound\r\n    Enables all Windows Firewall profiles(Domain, Public, Private).\r\n    Blocks all inbound traffic on the Domain, Public, Private profiles\r\n.OUTPUTS\r\n    String[]\r\n.OUTPUTS\r\n    PSCustomObject[]\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 10, Windows Server 2016\r\n    Release Notes:\r\n    Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n.COMPONENT\r\n    ProtocolSecurity\r\n#&gt;\r\n\r\n[CmdletBinding(DefaultParameterSetName = \"Enable\")]\r\nparam (\r\n    [Parameter(\r\n        Mandatory = $true,\r\n        ParameterSetName = \"Enable\"\r\n    )]\r\n    [Switch]\r\n    $Enable,\r\n    [Parameter(\r\n        Mandatory = $true,\r\n        ParameterSetName = \"Disable\"\r\n    )]\r\n    [Switch]\r\n    $Disable,\r\n    [Parameter(\r\n        ParameterSetName = \"Enable\"\r\n    )]\r\n    [Switch]\r\n    $BlockAllInbound\r\n)\r\n\r\nbegin {\r\n    function Test-IsElevated {\r\n        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()\r\n        $p = New-Object System.Security.Principal.WindowsPrincipal($id)\r\n        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)\r\n    }\r\n}\r\nprocess {\r\n    if (-not $(Test-IsElevated)) {\r\n        Write-Error -Message \"Access Denied. Please run with Administrator privileges.\"\r\n        exit 1\r\n    }\r\n    if ($(Get-Command \"Get-NetFirewallProfile\" -ErrorAction SilentlyContinue).Name -like \"Get-NetFirewallProfile\") {\r\n        # Use Get-NetFirewallProfile if available\r\n        try {\r\n            $NetFirewallSplat = @{\r\n                Profile     = @(\"Domain\", \"Public\", \"Private\")\r\n                Enabled     = $(if ($Enable) { \"True\" }elseif ($Disable) { \"False\" })\r\n                ErrorAction = \"Stop\"\r\n            }\r\n            if ($Enable -and $BlockAllInbound) {\r\n                $NetFirewallSplat.Add('DefaultInboundAction', 'Block')\r\n                $NetFirewallSplat.Add('DefaultOutboundAction', 'Allow')\r\n            }\r\n            Set-NetFirewallProfile @NetFirewallSplat\r\n            \r\n        }\r\n        catch {\r\n            Write-Error $_\r\n            Write-Host \"Failed to turn $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" }) the firewall.\"\r\n            exit 1\r\n        }\r\n        # Proof of work\r\n        Get-NetFirewallProfile -ErrorAction Stop | Format-Table Name, Enabled        \r\n    }\r\n    else {\r\n        # Fall back onto netsh\r\n        netsh.exe AdvFirewall set AllProfiles state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set DomainProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set PrivateProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set PublicProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        \r\n        if ($Enable -and $BlockAllInbound) {\r\n            try {\r\n                netsh.exe AdvFirewall set DomainProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n                netsh.exe AdvFirewall set PrivateProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n                netsh.exe AdvFirewall set PublicProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n            }\r\n            catch {\r\n                Write-Error $_\r\n                Write-Host \"Could not set Block All Inbound Traffic to 1\"\r\n            }\r\n        }\r\n        # Proof of work\r\n        netsh.exe AdvFirewall show AllProfiles state\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n    }\r\n}\r\nend {}<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p>Accedi a oltre 700 script nel Dojo di NinjaOne<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/it\/prova-gratuita\/\">Ottieni l&#8217;accesso<\/a><\/p>\n<\/div>\n<h2>Analisi dettagliata<\/h2>\n<p><span data-contrast=\"none\">Nella sua funzione principale, lo script per disabilitare i profili del firewall di Windows verifica la presenza di privilegi di amministratore, essenziali poich\u00e9 la modifica delle impostazioni del firewall di Windows richiede permessi elevati. Quindi verifica la presenza del cmdlet Get-NetFirewallProfile, un moderno comando PowerShell per la gestione dei profili del firewall di Windows.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">Se questo cmdlet \u00e8 disponibile, lo script lo utilizza per abilitare o disabilitare i profili del firewall di Windows specificati. L&#8217;opzione che consente di bloccare tutto il traffico in entrata, pur permettendo quello in uscita, aggiunge un ulteriore livello di sicurezza.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">In assenza del cmdlet Get-NetFirewallProfile, lo script torna al vecchio strumento a riga di comando netsh.exe.<\/span><\/p>\n<h2>Situazioni d&#8217;uso potenziali<\/h2>\n<p><span class=\"NormalTextRun SCXW210844645 BCX0\">Immagina una professionista IT, Jane, di una grande azienda. <\/span><span class=\"NormalTextRun SCXW210844645 BCX0\">L&#8217;azienda sta<\/span><span class=\"NormalTextRun SCXW210844645 BCX0\"> introducendo una nuova applicazione, ma durante i test viene notato che l&#8217;applicazione <\/span><span class=\"NormalTextRun SCXW210844645 BCX0\">non riesce<\/span><span class=\"NormalTextRun SCXW210844645 BCX0\"> a comunicare con il server. Sospettando un problema di <\/span><span class=\"NormalTextRun SCXW210844645 BCX0\">firewall<\/span><span class=\"NormalTextRun SCXW210844645 BCX0\">, Jane utilizza questo script per disabilitare i profili del <\/span><span class=\"NormalTextRun SCXW210844645 BCX0\"> firewall<\/span><span class=\"NormalTextRun SCXW210844645 BCX0\"> di Windows temporaneamente, testare l&#8217;applicazione e quindi riattivarli prontamente. Questa azione rapida aiuta a diagnosticare il problema senza dover ricorrere a un intervento manuale.<\/span><\/p>\n<h2>Confronti<\/h2>\n<p><span class=\"NormalTextRun SCXW70654975 BCX0\">Lo script <\/span><span class=\"NormalTextRun SCXW70654975 BCX0\">fornisce<\/span><span class=\"NormalTextRun SCXW70654975 BCX0\"> un approccio programmatico alla gestione dei profili del <\/span><span class=\"NormalTextRun SCXW70654975 BCX0\"> firewall<\/span><span class=\"NormalTextRun SCXW70654975 BCX0\"> di Windows. Le alternative sono la regolazione <\/span><span class=\"NormalTextRun ContextualSpellingAndGrammarErrorV2Themed SCXW70654975 BCX0\">manuale<\/span><span class=\"NormalTextRun SCXW70654975 BCX0\"> tramite la GUI del firewall di Windows o l&#8217;utilizzo degli Oggetti dei Criteri di gruppo (GPO) per i computer collegati al dominio. Tuttavia, a entrambi manca l&#8217;immediatezza offerta da questo script.<\/span><\/p>\n<h2>Domande frequenti<\/h2>\n<ul style=\"font-weight: 400;\">\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"3\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Posso eseguire questo script su qualsiasi macchina Windows?<\/span><br \/>\n<span data-contrast=\"none\">Lo script per disabilitare i profili del firewall di Windows \u00e8 progettato per Windows 10 e Windows Server 2016 e versioni successive.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"3\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Ho bisogno di permessi speciali per eseguire questo script?<\/span><br \/>\n<span data-contrast=\"none\">S\u00ec, sono necessari i privilegi di amministratore.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<\/ul>\n<h2>Implicazioni per la sicurezza<\/h2>\n<p><span class=\"NormalTextRun SCXW189245713 BCX0\">La possibilit\u00e0 di modificare rapidamente i profili del <\/span><span class=\"NormalTextRun SCXW189245713 BCX0\">firewall<\/span><span class=\"NormalTextRun SCXW189245713 BCX0\"> di Windows \u00e8 un&#8217;arma a doppio taglio. La loro disattivazione, anche momentanea, pu\u00f2 esporre i sistemi alle minacce. <\/span><span class=\"NormalTextRun SCXW189245713 BCX0\">\u00c8<\/span><span class=\"NormalTextRun SCXW189245713 BCX0\"> fondamentale comprendere le implicazioni per la sicurezza e garantire che i sistemi <\/span><span class=\"NormalTextRun SCXW189245713 BCX0\">rimangano<\/span><span class=\"NormalTextRun SCXW189245713 BCX0\"> protetti.<\/span><\/p>\n<h2>Raccomandazioni<\/h2>\n<ul>\n<li><span data-contrast=\"none\">Testa prima lo script per disabilitare i profili del firewall di Windows in un ambiente controllato.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li><span data-contrast=\"none\">Se stai disabilitando i profili del firewall di Windows per la diagnostica, riabilitali subito dopo.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li><span data-contrast=\"none\">Controlla regolarmente le regole del firewall di Windows per garantire l&#8217;allineamento con i criteri di sicurezza.<\/span><\/li>\n<\/ul>\n<h2>Considerazioni finali<\/h2>\n<p><span class=\"NormalTextRun SCXW176143587 BCX0\">La gestione dei profili del firewall di Windows \u00e8 essenziale per la sicurezza della rete e del sistema. Mentre strumenti come <\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW176143587 BCX0\">NinjaOne<\/span><span class=\"NormalTextRun SCXW176143587 BCX0\"> offrono <a href=\"https:\/\/www.ninjaone.com\/it\/\">soluzioni di gestione IT complete a tutti i livelli<\/a>, script come quello discusso sono preziosi per attivit\u00e0 specifiche. Come sempre, la comprensione del loro funzionamento e delle implicazioni garantisce un uso efficace e sicuro.<\/span><\/p>\n","protected":false},"author":35,"featured_media":207098,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4212],"use_cases":[4275],"class_list":["post-208130","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows","use_cases-configurazione-del-sistema"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub\/208130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/comments?post=208130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media\/207098"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media?parent=208130"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/operating_system?post=208130"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/use_cases?post=208130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}