Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

439
Vistas
How to use Where-Object in .NET PowerShell Automation?

I have this pipeline, which works in a PowerShell console:

Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne 'Disconnected' }

However, when I attempt it in .NET using System.Management.Automation it fails with this exception:

System.Management.Automation.CommandNotFoundException
  HResult=0x80131501
  Message=The term 'Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne 'Disconnected' }' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  Source=System.Management.Automation
  StackTrace:
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.Runspaces.Pipeline.Invoke()
   at Duplexer.PowerShell.Engine.ExecutePipeline(PSCommand Command) in D:\Dev\Projects\Duplexer\Duplexer\Code\PowerShell\Engine.vb:line 80
   at Duplexer.Main.GetSettings() in D:\Dev\Projects\Duplexer\Duplexer\Main.vb:line 84
   at Duplexer.Tests.PowerShellTests.Can_Get_NIC_Settings() in D:\Dev\Projects\Duplexer\Duplexer.Tests\PowerShellTests.vb:line 6

Here's my .NET code:

Public Sub GetSettings()
  Dim oPipeline As PSCommand
  Dim oResult As Collection(Of PSObject)

  oPipeline = New PSCommand
  oPipeline.
    AddCommand(PowerShell.Commands.GetNetIPConfiguration).
    AddCommand(PowerShell.Commands.WhereObject & " { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne 'Disconnected' }")

  Using oPowerShell As New PowerShell.Engine
    oPowerShell.ImportModules(PowerShell.Modules.NetAdapter, PowerShell.Modules.NetTCPIP)
    oResult = oPowerShell.ExecutePipeline(oPipeline)
  End Using
End Sub

How do I configure this PowerShell command for use in .NET Automation?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to provide the parameter argument (the scriptblock following Where-Object) separately. Change this line:

AddCommand(PowerShell.Commands.WhereObject & " { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne 'Disconnected' }")

to:

AddCommand(PowerShell.Commands.WhereObject).
AddArgument(ScriptBlock.Create("$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne 'Disconnected'"))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda