简体   繁体   中英

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. 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'. If i'm trying to run c# program not from VS, but running compiled exe i'm getting next output:

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 = "{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")

I think every record in this output is for the interface (i have 4 interfaces).

If i'm working in PowerShell console - nothing bad and everything works as expected. If i'm trying to execute, for example: ExecutePowerShellCommand("Get-Process"); - no exceptions and i'm getting the result. Not the same as in the PS console, but without exception in 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?

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") . And remember, you need to redirect output to Out-String command. Don't use AddStatement between this two commands. 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. Debugger just informing me about them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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