【问题标题】:Unable to run Disable-Mailbox Powershell in C#无法在 C# 中运行禁用邮箱 Powershell
【发布时间】:2018-05-08 12:53:00
【问题描述】:

我正在尝试在 C# 中重现 Powershell 的以下工作块。 我们正在一个 Exchange2010 实例上进行连接。

$ExURI = "http://ExchangeUrl/PowerShell/"

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExURI -Authentication Kerberos
$userName = "patatem"

Import-PSSession $Session -AllowClobber -EA SilentlyContinue | Out-Null

Get-Recipient $userName

Disable-Mailbox -Identity $userName -Confirm:$False
#enable-mailbox -identity $userName -Alias $userName -database "AnExchangeDatabase"

remove-PSSession $Session  

我一直按照此处引用的步骤进行操作:https://blogs.msdn.microsoft.com/wushuai/2016/09/18/access-exchange-online-by-powershell-in-c/

在下面的代码块中,当我调用Get-MailboxGet-Recipient 时,我得到了肯定的结果。

调用Disable-Mailbox时出现以下错误

“禁用邮箱”一词未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并 再试一次。

为什么它能识别Get-Mailbox而不识别Disable-Mailbox

(我尝试添加另一段代码来执行 Powershell 的导入会话部分,但这并没有改变任何东西。)

   public void EnableCommand(string identity, string database)
            {
                if (!string.IsNullOrWhiteSpace(identity))
                {
                    using (var runspace = RunspaceFactory.CreateRunspace())
                    {
                        runspace.Open();
                        var powershell = PowerShell.Create();

                        var command = new PSCommand();
                        command.AddCommand("New-PSSession");
                        command.AddParameter("ConfigurationName", "Microsoft.Exchange");
                        command.AddParameter("ConnectionUri", new Uri(Constants.DefaultOutLookUrl));
                        command.AddParameter("Authentication", "Kerberos");
                        powershell.Commands = command;

                        powershell.Runspace = runspace;
                        var result = powershell.Invoke();
                        if (powershell.Streams.Error.Count > 0 || result.Count != 1)
                        {
                            throw new Exception("Fail to establish the connection");
                        }

                        powershell = PowerShell.Create();
                        command = new PSCommand();
                        command.AddCommand("Invoke-Command");
                        command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Get-Mailbox"));
                        command.AddParameter("Session", result[0]);
                        powershell.Commands = command;
                        powershell.Runspace = runspace;
                        var mailBoxes = powershell.Invoke();

// This will give me a result
                        var returnValue = new StringBuilder();
                        foreach (var item in mailBoxes)
                        {
                            returnValue.AppendLine(item.ToString());
                        }


                        // check the other output streams (for example, the error stream)
                        if (powershell.Streams.Error.Count > 0)
                        {
                            returnValue.AppendLine($"{powershell.Streams.Error.Count} errors: ");
                            foreach (var err in powershell.Streams.Error)
                            {
                                returnValue.AppendLine($"{err.ToString()}");
                            }
                        }

// This will also work
                        powershell = PowerShell.Create();
                        command = new PSCommand();
                        command.AddCommand("Invoke-Command");
                        command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Get-Recipient SomeEmail"));
                        command.AddParameter("Session", result[0]);
                        powershell.Commands = command;
                        powershell.Runspace = runspace;
                        var mailBoxes2 = powershell.Invoke();

                        foreach (var item in mailBoxes2)
                        {
                            returnValue.AppendLine(item.ToString());
                        }


                        if (powershell.Streams.Error.Count > 0)
                        {
                            returnValue.AppendLine($"{powershell.Streams.Error.Count} errors: ");
                            foreach (var err in powershell.Streams.Error)
                            {
                              returnValue.AppendLine($"{err.ToString()}");
                            }
                        }

// this will give me The term 'Disable-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

                        powershell = PowerShell.Create();
                        command = new PSCommand();
                        command.AddCommand("Invoke-Command");
                        command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Disable-Mailbox -Identity patatem -Confirm:$False"));
                        command.AddParameter("Session", result[0]);
                        powershell.Commands = command;
                        powershell.Runspace = runspace;
                        var mailBoxes3 = powershell.Invoke();

                        foreach (var item in mailBoxes3)
                        {
                            returnValue.AppendLine(item.ToString());
                        }

                        // check the other output streams (for example, the error stream)
                        if (powershell.Streams.Error.Count > 0)
                        {
                            returnValue.AppendLine($"{powershell.Streams.Error.Count} errors: ");
                            foreach (var err in powershell.Streams.Error)
                            {
                              returnValue.AppendLine($"{err.ToString()}");
                            }
                        }

                        Console.WriteLine(returnValue);
                    }
                }

【问题讨论】:

  • 我猜是权限不足造成的。
  • 不应该给我一个权限类型错误吗?单独运行 powershell 效果很好。
  • AFAIK 如果您没有运行 cmdlet 的权限,您也会收到此消息。
  • 嗯,这似乎提供了更好的结果。我切换了帐户以运行 c#(具有更多权限),但我什么也没有。很高兴知道如果您没有权限,您会收到该错误。谢谢,马特斯诺。把它作为答案,我会给你。

标签: c# powershell exchange-server


【解决方案1】:

“禁用邮箱”一词未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并 再试一次。

如果尝试运行 Disable-Mailbox-cmdlet 的用户没有足够的权限来禁用邮箱 (RBAC),则会发出此非特定错误消息。

以足够的权限运行您的代码,它应该可以工作。

TechNet 文章:Understanding Role Based Access Control

【讨论】:

  • 这没有任何意义。该错误消息意味着 cmdlet 未导入/根本不存在。
  • @TheIncorrigible1 错误消息通常没有应有的帮助。也许 cmdlet 甚至没有足够的权限加载。
  • 一旦@MatSnow 给了我这个提示,我切换了具有权限的帐户,错误立即消失了。
猜你喜欢
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2018-02-09
相关资源
最近更新 更多