C#simple Powershell脚本返回错误:System.Management.Automation.Runspaces.PSSnapInException

C#simple Powershell脚本返回错误:System.Management.Automation.Runspaces.PSSnapInException,c#,winforms,powershell,C#,Winforms,Powershell,我有一个简单的脚本,在加载windows窗体时运行。它获取用户的登录名,这是应用程序中其他功能所必需的。除此之外,该应用程序运行良好。在我的开发机器上,我没有任何问题,命令运行得很好。然而,当我在测试机中运行应用程序时,它会抛出下面的错误显示 我不是powershell用户,对它也没有什么经验,但我甚至不相信我在使用管理单元。有人能帮我减轻这个错误吗 **************异常文本*************System.Management.Automation.Runspaces.PSS

我有一个简单的脚本,在加载windows窗体时运行。它获取用户的登录名,这是应用程序中其他功能所必需的。除此之外,该应用程序运行良好。在我的开发机器上,我没有任何问题,命令运行得很好。然而,当我在测试机中运行应用程序时,它会抛出下面的错误显示

我不是powershell用户,对它也没有什么经验,但我甚至不相信我在使用管理单元。有人能帮我减轻这个错误吗

**************异常文本*************System.Management.Automation.Runspaces.PSSnapInException:无法加载 Microsoft.PowerShell.Diagnostics中的Windows PowerShell管理单元,因为 出现以下错误:无法加载文件或程序集 “Microsoft.PowerShell.Commands”或其依赖项之一。系统 找不到指定的文件。在 System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadMsHsnapinaAssembly(PSSnapInInfo mshsnapinInfo)网址: System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo)网址: System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIns(集合`1 mshsnapininfo,PSConsoleLoadException&warning)位于 System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.CreateDefaultConfiguration() 在 System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost 主机)在中的HPSM_Assistant.HPSMAssistant.ADuser()处 C:\Users\user\Documents\Code\HPSMAssistant\HPSMAssistant\Form1.cs:line 592在中的HPSM_Assistant.HPSMAssistant.LoadTemplateId()处 C:\Users\user\Documents\Code\HPSMAssistant\HPSMAssistant\Form1.cs:line 396在HPSM_Assistant.HPSMAAssistant.Form1_加载(对象发送器, 事件参数(e)在 C:\Users\user\Documents\Code\HPSMAssistant\HPSMAssistant\Form1.cs:line 208 at System.Windows.Forms.Form.OnLoad(EventArgs e)at 位于的System.Windows.Forms.Form.OnCreateControl() System.Windows.Forms.Control.CreateControl(布尔值可修改)
位于System.Windows.Forms.Control.CreateControl()的 System.Windows.Forms.Control.WmShowWindow(Message&m)位于 System.Windows.Forms.Control.WndProc(Message&m)位于 System.Windows.Forms.Form.WmShowWindow(消息和m)位于 System.Windows.Forms.NativeWindow.Callback(IntPtr-hWnd,Int32-msg, IntPtr wparam、IntPtr lparam)

此外,System.Management.Automation-程序集版本:3.0.0.0-Win32版本:10.0.10586.0也可以正确加载。(它是通过Nuget安装在c#app中的。)

以下是我使用的代码:

  private string ADuser()// create Powershell runspace and run script to Get AD current username
    {

        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser");
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript("$env:UserName");
        pipeline.Commands.Add("Out-String");
        Collection<PSObject> results = pipeline.Invoke();
        runspace.Close();
        //Create string to output
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }
        return stringBuilder.ToString();
private string ADuser()//创建Powershell运行空间并运行脚本以获取AD当前用户名
{
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
RunspaceInvoke scriptInvoker=新的RunspaceInvoke(运行空间);
调用(“设置ExecutionPolicy Unrestricted-Scope CurrentUser”);
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(“$env:UserName”);
pipeline.Commands.Add(“输出字符串”);
收集结果=pipeline.Invoke();
runspace.Close();
//创建要输出的字符串
StringBuilder StringBuilder=新的StringBuilder();
foreach(结果中的PSObject对象)
{
stringBuilder.AppendLine(obj.ToString());
}
返回stringBuilder.ToString();

是否安装了powershell,并且您是否能够从powershell命令提示符下运行powershell命令?是否安装了powershell。我可以在计算机上成功运行env:UserName。您在测试计算机上安装了什么版本的PS?在powershell提示符下运行“$PSVersionTable”。两台计算机的版本相同(通过公司软件商店安装)PS3.0版PSCompatibleVersions{1.0、2.0、3.0}您好,您是否尝试运行以识别加载失败的任何程序集?此外,您可以检查应用程序看到的yor
$env:PSModulePath
变量,以确保其指向正确的方向。是否安装了powershell,并且您是否能够从powershell命令提示符运行powershell命令?是powershell已安装已计数。我可以在计算机上成功运行env:UserName。您在测试计算机上安装了什么版本的PS?在PowerShell提示符下运行“$PSVersionTable”。两台计算机的版本相同(通过公司软件商店安装)PS3.0版PSCompatibleVersions{1.0、2.0、3.0}您好,您是否尝试运行以识别加载失败的任何程序集?此外,您可以检查应用程序看到的yor
$env:PSModulePath
变量,以确保它指向正确的方向。