Voici une autre façon de créer des ports sur le Pare-feu, avec l'avantage que le système vous demandera toutes les options relatives aux entrées/sorties, au protocole, à l'autorisation/au refus, etc. de façon dynamique.
$rulename = Read-Host -Prompt "Enter rule name: "
$portNumber = Read-Host -Prompt "Enter Port Number: "
$protchoice = $Host.UI.PromptForChoice('Protocol Type','Enter Protocol (TCP/UDP): ',('&TCP','&UDP'),0)
if($protchoice -eq 0)
{
$protchoice02 = 'TCP'
$dirChoice = $Host.UI.PromptForChoice('Traffic Flow','Enter Traffic Flow direction (inbound/outbound): ',('&inbound','&outbound'),0)
if($dirChoice -eq 0)
{
$dirChoices = 'Inbound'
$allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
if($allowdenyChoice -eq '0')
{
$allowdenyChoices = 'Allow'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
else
{
$allowedenyChoices = 'Deny'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
}
else
{
$dirChoices = 'Outbound'
$allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
if($allowdenyChoice -eq '0')
{
$allowdenyChoices = 'Allow'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
else
{
$allowedenyChoices = 'Deny'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
}
}
else
{
$protchoice02 = 'UDP'
$dirChoice = $Host.UI.PromptForChoice('Traffic Flow','Enter Traffic Flow direction (inbound/outbound): ',('&inbound','&outbound'),0)
if($dirChoice -eq 0)
{
$dirChoices = 'Inbound'
$allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
if($allowdenyChoice -eq '0')
{
$allowdenyChoices = 'Allow'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
else
{
$allowedenyChoices = 'Deny'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
}
else
{
$dirChoices = 'Outbound'
$allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
if($allowdenyChoice -eq '0')
{
$allowdenyChoices = 'Allow'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
else
{
$allowedenyChoices = 'Deny'
New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
}
}
}