Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C执行powershell命令_C#_Powershell_C# 3.0 - Fatal编程技术网

C# C执行powershell命令

C# C执行powershell命令,c#,powershell,c#-3.0,C#,Powershell,C# 3.0,我需要在C中执行以下powershell命令: 获取netconnectionprofile 如果我从PowerShell运行该命令,它将返回一条记录: 但是如果我运行下面的代码,它将返回一个空列表 public void CheckWLANNetWork() { using (PowerShell PowerShellInstance = PowerShell.Create()) { // use "AddScript" to add th

我需要在C中执行以下powershell命令: 获取netconnectionprofile 如果我从PowerShell运行该命令,它将返回一条记录:

但是如果我运行下面的代码,它将返回一个空列表

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
    }


}
尽管如此,例如,如果我使用Get进程运行上述方法,它运行得很好。 我正在运行X86应用程序,我的电脑是x64。我无法在x64上运行我的应用程序,因为其他工具仅在x86上运行。这里有解决办法吗

编辑:

按照建议,我调用了PowerShellInstance.Streams.Error;
返回的错误是:提供程序加载失败。

您的C代码在什么上下文下运行而不工作?它是在Visual Studio中运行,还是在IIS中的网站上运行,或者可能是在Windows服务或计划任务中运行?Visual Studio,单元测试。它是wpf appWhat版本的Windows?您可以尝试使用whoami命令查看它在哪个帐户下运行。然后检查该帐户的权限。因为中的Windows.Networking.Connectivity在.NET Framework 4.5或更高版本中不可用。它在通用应用程序中可用,是.NET核心的一部分