简体   繁体   English

PowerShell C#运行命令不起作用

[英]PowerShell C# run command not working

I have next code: 我有下一个代码:

private void ExecutePowerShellCommand(string command)
        {
            try
            {             
                Runspace rs = RunspaceFactory.CreateRunspace();
                rs.Open();
                using (PowerShell ps = PowerShell.Create())
                {
                    ps.Runspace = rs;
                    ps.AddCommand(command);
                    var psOutput = ps.Invoke();
                    LogPSHeader();
                    foreach (var item in psOutput)
                    {
                        if (item == null) continue;
                        LogPS(item.BaseObject.ToString());
                    }
                    if (ps.Streams.Error.Count > 0) LogPS("ERROR");
                    LogPSEnding();
                }
                //rs.Close();
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
        }

Next is how i'm trying to call this method: 接下来是我如何尝试调用此方法:

ExecutePowerShellCommand("Get-NetAdapter");

The problem that such code is not working and i don't know why. 这样的代码无法正常工作的问题,我也不知道为什么。 I want to have the same output as in console, but in my app. 我希望在控制台中具有与控制台相同的输出,但在我的应用程序中。 Every time i'm runnig code i'm getting different exceptions in debug attached mode, like: - Operation Cancelled Exception - Can't find 'Event.Format.ps1xml' file. 每次我使用Runnig代码时,都会在调试附加模式下遇到不同的异常,例如:-操作取消了异常-无法找到“ Event.Format.ps1xml”文件。 When i'm putting this file to the folder of his search i'm getting request for the next file etc. - Next he couldn't find some file (i don't remember the exactly name of the file) in the folder 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Modules\\Appx'. 当我将此文件放到他的搜索文件夹中时,我正在请求下一个文件等。-接下来,他在文件夹'中找不到一些文件(我不记得文件的确切名称)。 C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ Modules \\ Appx'。 If i'm trying to run c# program not from VS, but running compiled exe i'm getting next output: 如果我尝试不是从VS运行c#程序,而是运行已编译的exe,则会得到下一个输出:

MSFT_NetAdapter (CreationClassName = "MSFT_NetAdapter", DeviceID = "{4C67B2DB-572B-4CB8-9D0F-19AC7FE93B1B}", SystemCreationClassName = "CIM_NetworkPort", SystemName = "w-KerbiczkovYuI.e-lab.icl.kazan.ru") MSFT_NetAdapter(CreationClassName =“ MSFT_NetAdapter”,DeviceID =“ {4C67B2DB-572B-4CB8-9D0F-19AC7FE93B1B}”,SystemCreationClassName =“ CIM_NetworkPort”,SystemName =“ w-KerbiczkovYuI.e-lab.icl.kazan.ru”)

MSFT_NetAdapter (CreationClassName = "MSFT_NetAdapter", DeviceID = "{CBBC4607-17BB-4FFB-A93F-A08F17472C8A}", SystemCreationClassName = "CIM_NetworkPort", SystemName = "w-KerbiczkovYuI.e-lab.icl.kazan.ru") MSFT_NetAdapter(CreationClassName =“ MSFT_NetAdapter”,DeviceID =“ {CBBC4607-17BB-4FFB-A93F-A08F17472C8A}”,SystemCreationClassName =“ CIM_NetworkPort”,SystemName =“ w-KerbiczkovYuI.e-lab.icl.kazan.ru”)

MSFT_NetAdapter (CreationClassName = "MSFT_NetAdapter", DeviceID = "{31950F9C-DF49-49A7-8BF9-CBD36F99D7CB}", SystemCreationClassName = "CIM_NetworkPort", SystemName = "w-KerbiczkovYuI.e-lab.icl.kazan.ru") MSFT_NetAdapter(CreationClassName =“ MSFT_NetAdapter”,DeviceID =“ {31950F9C-DF49-49A7-8BF9-CBD36F99D7CB}”,SystemCreationClassName =“ CIM_NetworkPort”,SystemName =“ w-KerbiczkovYuI.e-lab.icl.kazan.ru”)

MSFT_NetAdapter (CreationClassName = "MSFT_NetAdapter", DeviceID = "{B03679B3-199A-43D8-9088-E919658424B1}", SystemCreationClassName = "CIM_NetworkPort", SystemName = "w-KerbiczkovYuI.e-lab.icl.kazan.ru") MSFT_NetAdapter(CreationClassName =“ MSFT_NetAdapter”,DeviceID =“ {B03679B3-199A-43D8-9088-E919658424B1}”,SystemCreationClassName =“ CIM_NetworkPort”,SystemName =“ w-KerbiczkovYuI.e-lab.icl.kazan.ru”)

I think every record in this output is for the interface (i have 4 interfaces). 我认为此输出中的每个记录都是针对接口的(我有4个接口)。

If i'm working in PowerShell console - nothing bad and everything works as expected. 如果我在PowerShell控制台中工作-没什么不好,一切都按预期进行。 If i'm trying to execute, for example: ExecutePowerShellCommand("Get-Process"); 如果我要执行,例如: ExecutePowerShellCommand("Get-Process"); - no exceptions and i'm getting the result. -没有例外,我得到了结果。 Not the same as in the PS console, but without exception in VS. 与PS控制台不同,但VS中无一例外。

What i'm doing wrong and how can i get expected result and have the same output as in the PowerShell console in my app? 我在做错什么,如何获得预期的结果并具有与我的应用程序中的PowerShell控制台相同的输出?

Sorry for the stupid question. 很抱歉这个愚蠢的问题。 I have found an answer. 我找到了答案。 I wanted to close the question but i can't and would answer here. 我想结束问题,但我不能,将在这里回答。 To have a good output as in the PS console i need to add command at the end of statement: ps.AddCommand("Out-String") . 为了在PS控制台中获得良好的输出,我需要在语句末尾添加命令: ps.AddCommand("Out-String") And remember, you need to redirect output to Out-String command. 记住,您需要将输出重定向到Out-String命令。 Don't use AddStatement between this two commands. 在这两个命令之间不要使用AddStatement Second part of my question are strange exceptions. 我的问题的第二部分是奇怪的例外。 I still don't fully understand the nature of them, but as i understand they are internal powershell exceptions and catched by powershell class because i'm not catching any of this exception. 我仍然不完全了解它们的性质,但是据我所知,它们是内部Powershell异常,并被Powershell类捕获,因为我没有捕获任何此类异常。 Debugger just informing me about them. 调试器只是通知我有关它们的信息。

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

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