使用 PowerShell 更改 Windows 防火墙设置但不显示输出
Change Windows Firewall Settings With PowerShell But Do Not Show Output
我有一个 PowerShell 脚本用于安装 SQL Express,然后是 SQL Server Management Studio,最后编辑 Windows 防火墙设置以允许远程连接到数据库。对于防火墙更改,我 运行ning 的其中一行是:
New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'
理想情况下,我希望输出只是:
Windows Firewall configured to allow incoming connections on TCP port 1433
相反,我得到:
Caption :
Description :
ElementName : MSSQL ENGINE TCP
InstanceID : {752a3c18-298f-4639-a462-4cc5205b1016}
CommonName :
PolicyKeywords :
Enabled : True
PolicyDecisionStrategy : 2
PolicyRoles :
ConditionListType : 3
CreationClassName : MSFT|FW|FirewallRule|{752a3c18-298f-4639-a462-
4cc5205b1016}
ExecutionStrategy : 2
Mandatory :
PolicyRuleName :
Priority :
RuleUsage :
SequencedActions : 3
SystemCreationClassName :
SystemName :
Action : Allow
Direction : Inbound
DisplayGroup :
DisplayName : MSSQL ENGINE TCP
EdgeTraversalPolicy : Block
EnforcementStatus : NotApplicable
LocalOnlyMapping : False
LooseSourceMapping : False
Owner :
Platforms : {}
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
PrimaryStatus : OK
Profiles : 0
RuleGroup :
Status : The rule was parsed successfully from the store. (65536)
StatusCode : 65536
PSComputerName :
Name : {752a3c18-298f-4639-a462-4cc5205b1016}
ID : {752a3c18-298f-4639-a462-4cc5205b1016}
Group :
Platform : {}
LSM : False
Profile : Any
Windows Firewall configured to allow incoming connections on TCP port 1433
是否有一种简单的方法可以消除 PowerShell 中出现的所有多余内容 window?我知道我可以创建第二个脚本并在单独的 window 中提示 运行,但我正在尝试使用单个脚本来完成此操作。
以下将抑制命令输出并仍然执行命令:
$null = New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'
当输出保存到 $null
时,输出被删除。
您也可以转换为 [void]
,在某些情况下,这可能会比分配给 $null
产生更好的性能。
[void](New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow)
这两种情况都可以忽略不计 performance-wise。您应该 避免在所有情况下使用 Out-Null
,因为那样总是比较慢。
通常不建议使用 Write-Host
,但由于我不知道您是如何调用或执行代码的,所以我将其保留在那里。如果您是在 PowerShell 控制台中执行此操作,则只需将引用的文本单独放在一行即可。
以下是比较这三种方法的一些性能测试:
Out-Null:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$list.add($_) | Out-Null} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
6354765 635.4765
[无效]:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {[void]$list.add($_)} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
1323269 132.3269
$null:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$null = $list.add($_)} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
1269874 126.9874
运行 命令。管道输出到 Out-Null
。如果先前的命令成功,则显示消息。
New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow | Out-Null
if($?){Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'}
您可以将它拨到您的场景中,但您知道我在做什么。
我有一个 PowerShell 脚本用于安装 SQL Express,然后是 SQL Server Management Studio,最后编辑 Windows 防火墙设置以允许远程连接到数据库。对于防火墙更改,我 运行ning 的其中一行是:
New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'
理想情况下,我希望输出只是:
Windows Firewall configured to allow incoming connections on TCP port 1433
相反,我得到:
Caption :
Description :
ElementName : MSSQL ENGINE TCP
InstanceID : {752a3c18-298f-4639-a462-4cc5205b1016}
CommonName :
PolicyKeywords :
Enabled : True
PolicyDecisionStrategy : 2
PolicyRoles :
ConditionListType : 3
CreationClassName : MSFT|FW|FirewallRule|{752a3c18-298f-4639-a462-
4cc5205b1016}
ExecutionStrategy : 2
Mandatory :
PolicyRuleName :
Priority :
RuleUsage :
SequencedActions : 3
SystemCreationClassName :
SystemName :
Action : Allow
Direction : Inbound
DisplayGroup :
DisplayName : MSSQL ENGINE TCP
EdgeTraversalPolicy : Block
EnforcementStatus : NotApplicable
LocalOnlyMapping : False
LooseSourceMapping : False
Owner :
Platforms : {}
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
PrimaryStatus : OK
Profiles : 0
RuleGroup :
Status : The rule was parsed successfully from the store. (65536)
StatusCode : 65536
PSComputerName :
Name : {752a3c18-298f-4639-a462-4cc5205b1016}
ID : {752a3c18-298f-4639-a462-4cc5205b1016}
Group :
Platform : {}
LSM : False
Profile : Any
Windows Firewall configured to allow incoming connections on TCP port 1433
是否有一种简单的方法可以消除 PowerShell 中出现的所有多余内容 window?我知道我可以创建第二个脚本并在单独的 window 中提示 运行,但我正在尝试使用单个脚本来完成此操作。
以下将抑制命令输出并仍然执行命令:
$null = New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'
当输出保存到 $null
时,输出被删除。
您也可以转换为 [void]
,在某些情况下,这可能会比分配给 $null
产生更好的性能。
[void](New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow)
这两种情况都可以忽略不计 performance-wise。您应该 避免在所有情况下使用 Out-Null
,因为那样总是比较慢。
通常不建议使用 Write-Host
,但由于我不知道您是如何调用或执行代码的,所以我将其保留在那里。如果您是在 PowerShell 控制台中执行此操作,则只需将引用的文本单独放在一行即可。
以下是比较这三种方法的一些性能测试:
Out-Null:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$list.add($_) | Out-Null} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
6354765 635.4765
[无效]:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {[void]$list.add($_)} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
1323269 132.3269
$null:
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$null = $list.add($_)} } | Select-Object -Property ticks,totalmilliseconds
Ticks TotalMilliseconds
----- -----------------
1269874 126.9874
运行 命令。管道输出到 Out-Null
。如果先前的命令成功,则显示消息。
New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow | Out-Null
if($?){Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'}
您可以将它拨到您的场景中,但您知道我在做什么。