简体   繁体   English

Visual Studio 2017 C#启动Powershell脚本

[英]Visual Studio 2017 C# launching powershell script

Hope this is the right place to ask new to this site :) 希望这是向本网站提出新要求的合适地方:)

I created a GUI in visual studio for new user creation. 我在Visual Studio中创建了一个用于创建新用户的GUI。 Part of the code then calls a Powershell script I have been using for user creation. 然后,部分代码调用了我一直用于创建用户的Powershell脚本。

So the GUI all works I can add the users to a CSV file it creates and then launches the Powershell script. 因此,GUI都可以正常工作,我可以将用户添加到它创建的CSV文件中,然后启动Powershell脚本。

The problem I get is that part of the Powershell script loads the exchange 2007 snapin. 我得到的问题是Powershell脚本的一部分加载了Exchange 2007管理单元。 For some reason when running the script via the GUI I have created it always comes up with an error saying it is unable to find the plugin. 出于某种原因,通过我创建的GUI运行脚本时,总是会出现一条错误消息,指出无法找到插件。

So basically the code for launching the Powershell is as follows. 因此,基本上,用于启动Powershell的代码如下。 Now I am not a coder at all so I could be going about it the wrong way and it might be a bit of a mess hehe :) 现在我根本不是编码人员,所以我可能会以错误的方式进行操作,可能有点混乱:)

Process p = new Process();

p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);

// Redirect the output stream of the child process.
p.StartInfo.CreateNoWindow = false; (false for testing)
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false; //(false for testing)
p.StartInfo.RedirectStandardOutput = false; //(false for testing)
p.StartInfo.RedirectStandardError = false; //(false for testing)
p.StartInfo.LoadUserProfile = true;
p.StartInfo.FileName = "This points to a file on a network share" @"\\server\folder\file";

p.Start();
p.BeginOutputReadLine();

The above happens after a button click and will be outputting to a text box within the GUI once I have it all working. 上面的操作在单击按钮后发生,一旦我一切正常,它就会输出到GUI中的文本框。 But for now i have that side of things disabled so i can see the errors in Powershell. 但是现在我禁用了这方面的功能,以便可以看到Powershell中的错误。

So the error message in Powershell is as follows 因此,Powershell中的错误消息如下

'Microsoft.exchange.management.powershell.admin' is not installed on this computer 此计算机上未安装'Microsoft.exchange.management.powershell.admin'

But again if I run the script manually without the GUI it works fine. 但是如果我在没有GUI的情况下手动运行脚本,它仍然可以正常工作。 Its like the GUI is launching it in a closed environment that has no access to the computer its running on. 就像GUI在封闭的环境中启动它一样,该环境无法访问运行它的计算机。 I am prob missing something simple as I really have no idea about creating c# apps. 我很可能缺少一些简单的东西,因为我真的不知道创建c#应用程序。

OK I think I have solved the problem. 好的,我想我已经解决了问题。

I should have explained better on what I was doing to launch the Powershell script. 我应该对启动Powershell脚本的工作进行更好的解释。 Basically I was using the above code to launch a .bat file and doing it that way for whatever reason when that bat file ran it would not work and I would get the above error. 基本上,我使用上面的代码来启动.bat文件,并且无论运行该bat文件时出于何种原因,都无法正常运行,并且会收到上述错误。

So I used a little program to convert the bat file into an exe and set it to always run as admin. 因此,我使用了一个小程序将bat文件转换为exe并将其设置为始终以admin身份运行。 This resolved the issue and now the script runs correctly. 这样就解决了问题,现在脚本可以正确运行。

Not sure why but launch the bat file it did not have access to the local user profile so could not load the Powershell exchange snap-in as the details for that are stored in the local users registry. 不知道为什么,但是启动bat文件时,它无权访问本地用户配置文件,因此无法加载Powershell交换管理单元,因为其详细信息存储在本地用户注册表中。

So the solution was to just change the bat file that my GUI launched for the Powershell script to an EXE file. 因此,解决方案是将GUI为Powershell脚本启动的bat文件更改为EXE文件。 Not sure why this is but it works :) 不知道为什么会这样,但是它能起作用:)

Thanks Michael Smith 谢谢迈克尔·史密斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM