0

I need to execute the following powershell command in C# : get-netconnectionprofile if i run the command from PowerShell it returns one record : enter image description here

but if I run the following code it returns an empty list.

public void CheckWLANNetWork()
{
    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
        // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
        PowerShellInstance.AddCommand("get-netconnectionprofile"); //

        var test = PowerShellInstance.Invoke(); //here test it is an empty list
    }


}

Although, if I run the above method with "Get-Process", for example, it runs just fine. I am running a X86 app, and my pc is x64. I cannot run my application on x64 due to other tools which runs only on x86. Is there a solution here?

EDIT:

As suggested, I called PowerShellInstance.Streams.Error; The error returned is : "Provider load failure".

Community
  • 1
  • 1
Dragos Stoica
  • 1,753
  • 1
  • 21
  • 42
  • 1
    What context is your C# code running under (and not working)? Is it running in Visual Studio, or on a website in IIS, or perhaps a Windows Service or Scheduled Task? – Kev Sep 10 '15 at 13:29
  • Visual Studio, a unit test. It is a wpf app – Dragos Stoica Sep 10 '15 at 13:31
  • 1
    What version of Windows? – Kev Sep 10 '15 at 13:35
  • I am using Windows 8.1 – Dragos Stoica Sep 10 '15 at 13:35
  • Why use Powershell? https://msdn.microsoft.com/en-us/library/windows.networking.connectivity.networkinformation.getinternetconnectionprofile.aspx – GreenEyedAndy Sep 10 '15 at 13:42
  • 1
    You could try the `whoami` command to see what account it is running under. And then check permissions for that account. – Steve Wellens Sep 10 '15 at 13:45
  • 1
    Because Windows.Networking.Connectivity in not available in .NET Framework 4.5 or higher. It is usable in universal apps, part of .NET CORE – Dragos Stoica Sep 10 '15 at 13:46
  • Steve Wellens, I have admin privileges on my pc. its not a matter of rights I think. – Dragos Stoica Sep 10 '15 at 13:47
  • Are you sure you're not accidentally swallowing an exception here? – Kev Sep 10 '15 at 14:34
  • I don`t know in the C# code I receive only an empty list, no error. – Dragos Stoica Sep 10 '15 at 14:48
  • Does PowerShellInstance.Streams.Error contain any errors? If so post them. Also have you tried manually Importing the NetConnection module? – skukx Sep 10 '15 at 18:12
  • `Because Windows.Networking.Connectivity in not available in .NET Framework 4.5 or higher. It is usable in universal apps, part of .NET CORE` Isn't it conceivable that the powershell cmdlet relies on that underlying functionality, and therefore won't work in your universal app? [I am reminded of this question](http://stackoverflow.com/q/32315616/3905079). – briantist Sep 10 '15 at 18:42
  • Skuks and Kev you are right, its throwing an error, i will update my question, ty – Dragos Stoica Sep 11 '15 at 06:22

0 Answers0