c# - 在 C# 中交换 Powershell

标签 c# powershell exchange-server

我在 c# 中有一堆 powershell 命令,但是其中一个返回了 0 个结果,我无法弄明白,所以借助互联网的力量,我希望你们能得到答案。

我的运行电源的c#代码如下

internal static List<ExchangeMailboxes> ExchangeMailboxList(string snapIn)
        {
            List<ExchangeMailboxes> data = new List<ExchangeMailboxes>();
            StringBuilder stringBuild = new StringBuilder();
            stringBuild.AppendLine("$script:WarningPreference = 'SilentlyContinue'");
            stringBuild.AppendLine(
                "Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select DisplayName,@{name='TotalItemSize';expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}},@{name='TotalDeletedItemSize';expression={[math]::Round((($_.TotalDeletedItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}}");
            using (PowerShell inst = PowerShell.Create())
            {
                inst.AddScript("Add-PSSnapin " + snapIn)
                    .AddScript(stringBuild.ToString());
                Collection<PSObject> results = inst.Invoke();
                foreach (PSObject obj in results)
                {
                    data.Add(new ExchangeMailboxes()
                    {
                        Name = obj.Members["DisplayName"].Value.ToString(),
                        InboxSize = obj.Members["TotalItemSize"].Value.ToString(),
                        DeletedSize = obj.Members["TotalDeletedItemSize"].Value.ToString()
                    });
                }
            }
            return data;
        } 

我可以确认 snapin 正在正确加载,如果我手动运行 powershell 命令一切正常,我确认没有权限问题

这是原始格式的 powershell 命令

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select DisplayName,@{name='TotalItemSize';expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}},@{name='TotalDeletedItemSize';expression={[math]::Round((($_.TotalDeletedItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}}

最佳答案

与 Exchange 2010 及更新版本交互的推荐方式是打开一个 session http://servername/powershell使用 microsoft.exchange 配置:

$ExSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri ‘http://ExServer1.contoso.com/PowerShell/?SerializationLevel=Full’ -Credential $Credentials –Authentication Kerberos

我从未尝试过从 C# 代码进行远程处理,但我想这应该与您现在所做的没有任何不同(当然,除了 powershell 代码本身)。由于您在 Exchange 中与各种“大小”属性进行交互,因此仍然建议在本地安装管理工具,否则这些值无法正确序列化/反序列化(您会在 serverfault 上找到关于该主题的其他帖子)。

关于c# - 在 C# 中交换 Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31195976/

相关文章:

powershell - Powershell 中 Select-String 方法的 "line"由什么构成?

iphone - 使用 Exchange ActiveSync 将 iPhone 上的应用程序列入黑名单或白名单

outlook - 仅在本地保存邮件属性 (Outlook)

java - 如何使用 EWS 托管 API 和 ColdFusion 搜索收件箱?

c# - azure 存储队列中的最大出队计数

c# - 从 UWP 获取 TCP 服务器上的实时数据(更新)

powershell - 将文件夹中的每个 .flac 文件转换为 320kbps .mp3,而不丢失元数据

c# - 为什么 "new int[n] is object[]"是假的?为什么 "int[] is object[] arr"是模式错误?

c# - SslStream .read() 一旦数据包返回第一个字节

azure - Azure 自动化中的 Connect-MgGraph