使用PowerShell更改Windows防火墙设置,但不显示输出



我有一个用于安装SQL Express的PowerShell脚本,然后是SQL Server Management Studio,最后是编辑Windows防火墙设置以允许远程连接到数据库。对于防火墙更改,我正在运行的一条线是:

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窗口中显示出来?我知道我可以创建第二个脚本并提示该脚本在单独的窗口中运行,但是我正在尝试使用一个脚本来完成此操作。

以下将抑制命令输出并仍执行命令:

$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)

,无论哪种情况,这可能都可以忽略不计。您应该在所有情况下都避免使用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

[void]:

$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

运行cmdlet。注视到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'}

您可以将其拨入您的方案,但是您可以了解我在做什么。

最新更新