【问题标题】:Use Get-NetConnectionProfile and Set-NetConnectionProfile to change NetworkCategory使用 Get-NetConnectionProfile 和 Set-NetConnectionProfile 更改 NetworkCategory
【发布时间】:2017-02-27 14:50:44
【问题描述】:

我正在尝试编写一个脚本来查找所有活动接口并将其网络类别设置为专用。理想情况下,我只会将当前设置为 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

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    就在我面前……

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

    这可以通过在原始 get 命令中过滤而不是管道到 where 来简化:

     Get-NetConnectionProfile -NetworkCategory "Public" | Set-NetConnectionProfile -NetworkCategory Private
    

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多