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

Sql 使用PowerShell更改Windows防火墙设置,但不显示输出,sql,powershell,firewall,Sql,Powershell,Firewall,我使用PowerShell脚本安装SQL Express,然后安装SQL Server Management Studio,最后编辑Windows防火墙设置以允许远程连接到数据库。对于防火墙更改,我正在运行的一行是: New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow Write-Host 'Windows Firew

我使用PowerShell脚本安装SQL Express,然后安装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产生更好的性能

在这两种情况下,从性能角度来看,这可能是微不足道的。您应该避免在所有情况下使用Out Null,因为这样做总是比较慢

通常不建议使用Write-Host,但因为我不知道您是如何调用或执行代码的,所以我将不使用它。如果在PowerShell控制台中执行此操作,只需将带引号的文本保留在一行即可

以下是比较三种方法的一些性能测试:

输出空值:

[无效]:

$null:


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

$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产生更好的性能

在这两种情况下,从性能角度来看,这可能是微不足道的。您应该避免在所有情况下使用Out Null,因为这样做总是比较慢

通常不建议使用Write-Host,但因为我不知道您是如何调用或执行代码的,所以我将不使用它。如果在PowerShell控制台中执行此操作,只需将带引号的文本保留在一行即可

以下是比较三种方法的一些性能测试:

输出空值:

[无效]:

$null:


运行cmdlet。从Pipeout到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'}

您可以将此拨入您的场景,但您知道我在做什么。

运行cmdlet。从Pipeout到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'}

您可以将此拨入您的场景中,但您知道我在做什么。

请返回并展开您的别名/速记内容好吗?//在执行演示代码时,我强烈建议您使用完整的cmdlet名称和参数名称。您同时使用了%和select,并且在select Object cmdlet中未使用-Property。对于那些正在学习的人和通常可读=可理解=可维护的想法来说,相当明确是值得的。[grin]我承认我太懒了,不能在ForEach对象调用中使用-Process参数名。。。[blush]拜托,你能回去扩展一下你的别名/速记吗?//在执行演示代码时,我强烈建议您使用完整的cmdlet名称和参数名称。您同时使用了%和select,并且在select Object cmdlet中未使用-Property。对于那些正在学习的人和通常可读=可理解=可维护的想法来说,相当明确是值得的。[grin]我承认我太懒了,不能在ForEach对象调用中使用-Process参数名。。。[脸红]
$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$null = $list.add($_)} } | Select-Object -Property  ticks,totalmilliseconds

  Ticks TotalMilliseconds
  ----- -----------------
1269874          126.9874
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'}