Author Posts

February 27, 2017 at 2:40 pm

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!

February 27, 2017 at 3:24 pm

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 }