使用Get-NetConnectionProfile和Set-NetConnectionProfile更改NetworkCategory

时间:2017-02-27 14:50:44

标签: windows powershell

我正在尝试编写一个查找所有活动接口的脚本,并将其网络类别设置为Private。理想情况下,我只会将当前设置的接口设置为Public,但如果我尝试设置已设置的连接,我不会想象会导致任何问题。以下是我对此任务的谦虚尝试。

get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select netconnectionid, name InterfaceIndex, netconnectionstatus | ForEach-Object Set-NetConnectionProfile -interfaceindex {$_.InterfaceIndex} -NetworkCategory Private

这是我收到的错误。

ForEach-Object : Input name "Set-NetConnectionProfile" cannot be resolved to a method. At line:1 char:140 + ... ionstatus | ForEach-Object Set-NetConnectionProfile -interfaceindex { ...+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : InvalidArgument: (@{netconnection...ectionstatus=2}:PSObject) [ForEach-Object], PSArgumentException
+ FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand

我在Windows 10上运行Powershell 5.0

1 个答案:

答案 0 :(得分:0)

就在我面前......

before_request

这可以通过过滤原始的get命令而不是管道到where:

来简化
Get-NetConnectionProfile | Where-Object { $_.NetworkCategory -match "Public" } | Set-NetConnectionProfile -NetworkCategory Private