Get-NetConnectionProfile | Set-NetConnectionProfile

This topic contains 2 replies, has 2 voices, and was last updated by  Michael MacKenna 9 months, 3 weeks ago.

  • Author
    Posts
  • #65320

    Michael MacKenna
    Participant

    I am attempting to write a script that finds active interface connections and sets any that are set to Public to Private. Here is my very humble first attempt at this idea.

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

    When I run this command I get a number of errors.

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

    I tried the usual Google searching and searched this forum for scripts containing to Set-NetConnectionProfile to see if I could get some ideas but I came up empty. I was hoping someone here might be able to provide some guidance.
    Thanks!

  • #65326

    Daniel Krebs
    Moderator

    Hi Michael,

    You'll need to wrap Set-NetConnectionProfile in a scriptblock {} and remove the script block around of the InterfaceIndex argument.

    Here is a formatted example:

    Get-WmiObject Win32_NetworkAdapter -Filter "NetConnectionStatus = 2" | 
      ForEach-Object -Process { Set-NetConnectionProfile -Interfaceindex $_.InterfaceIndex -NetworkCategory Private }
    

You must be logged in to reply to this topic.