无法在 C# 中运行禁用邮箱 Powershell

2024-03-27

我正在尝试用 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/ https://blogs.msdn.microsoft.com/wushuai/2016/09/18/access-exchange-online-by-powershell-in-c/

在下面的代码块中,当我调用时,我得到了积极的结果Get-Mailbox, Get-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);
                    }
                }

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

如果用户尝试运行Disable-Mailbox-cmdlet 没有足够的权限来禁用邮箱(RBAC https://blogs.technet.microsoft.com/heyscriptingguy/2012/01/13/use-powershell-and-rbac-to-control-access-to-exchange-server-cmdlets/),会发出此非特定错误消息。

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

科技网文章:了解基于角色的访问控制 https://technet.microsoft.com/en-us/library/dd298183(v=exchg.160).aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 C# 中运行禁用邮箱 Powershell 的相关文章

随机推荐