如何获取Powershell交换管理运行空间的输出

拜洛克

新成员
已加入
2020年12月21日
留言内容
1
编程经验
Beginner
嗨,大家好,

我想在交换管理外壳中执行一些powershell命令,并发现以下代码已删除该代码:

C#:
private static Runspace OpenExchangeManagementRunspace(string url,AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.Kerberos,string userName = null,string password = null)
        {
            Runspace runspace = null;

            WSManConnectionInfo shellConnection = null;

            PSCredential shellCred = null;

            int keylenght = 32;
            byte[] intBytes = BitConverter.GetBytes(keylenght);
            Array.Reverse(intBytes);
            byte[] keybyte = intBytes;

            // Username and password is optional when using Kerberos authentication
            if (!string.IsNullOrWhiteSpace(userName) &&
                !string.IsNullOrWhiteSpace(password))
            {
                var securePassword = ConvertToSecureString(password,keybyte);
                shellCred = new PSCredential(userName, securePassword);
            }

            shellConnection = new WSManConnectionInfo(new Uri(url), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", shellCred);

            shellConnection.AuthenticationMechanism = authenticationMechanism;

            // This will take some time
            runspace = RunspaceFactory.CreateRunspace(shellConnection);
            runspace.Open();
          
            return runspace;
        }

C#:
private void button1_Click_1(object sender, EventArgs e)
        {
            ////stackoverflow.com/questions/57326768/how-can-i-start-a-script-in-exchange-management-shell-from-c
            using (Runspace remoteRunspace = OpenExchangeManagementRunspace("http://domain/powershell?serializationLevel=Full;clientApplication=MyAppName"))
            using (PowerShell shell = PowerShell.Create())
            {
                shell.Runspace = remoteRunspace;
                Pipeline pipeline = remoteRunspace.CreatePipeline();
                pipeline.Commands.AddScript("Get-MailboxFolderStatistics user");
                //pipeline.Commands.Add("Out-String");


                Collection<PSObject> results = pipeline.Invoke();
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject pSObject in results)
                    stringBuilder.AppendLine(pSObject.ToString());
                textBox1.Text = stringBuilder.ToString();

            }

我改变了第二个,让我得到了外壳的输出,但是我在这里做错了。如果powershell输出很小,我有时会在应用程序中得到输出的一小部分(这意味着至少要执行命令),但是会创建较大的输出"nonsense" like this

C#:
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
......


You can see //pipeline.Commands.Add("Out-String"); in the second code block. I've seen many people using "Out-String"以类似的代码,但启用它使我:
System.Management.Automation.RemoteException:"Out-String"找不到为cmdlet,函数或脚本文件。

而且,我不明白为什么以后将输出放入PSObject Collection中时也应该将输出转换为字符串。

Do I have to add 外弦? If so anybody got an Idea why I get that 未找到 错误?
还是我的方法完全错误?


抱歉,任何愚蠢的问题和肮脏的代码C#对我来说都是相当新的,与C#结合使用Powershell是全新的。
 
最佳 底部