Unable to run Disable-Mailbox Powershell in C#

ぐ巨炮叔叔 提交于 2020-01-04 03:56:13

问题


I'm trying to reproduce the following working chunk of Powershell in C#. We are connecting on an Exchange2010 instance.

$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  

I've been following the steps referenced here : https://blogs.msdn.microsoft.com/wushuai/2016/09/18/access-exchange-online-by-powershell-in-c/

In the following block of code, I get positive results when I call Get-Mailbox, Get-Recipient.

When calling Disable-Mailbox, I get the following error

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.

Why is it recognizing Get-Mailbox but not Disable-Mailbox?

(I've tried adding another chunk of code to do the Import session section of Powershell but that didn't change anything.)

   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);
                    }
                }

回答1:


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.

If the user attempting to run the Disable-Mailbox-cmdlet has insufficient permissions to disable a mailbox (RBAC), this nonspecific error message is issued.

Run your code with sufficient permissions and it should work.

TechNet article: Understanding Role Based Access Control



来源:https://stackoverflow.com/questions/47474426/unable-to-run-disable-mailbox-powershell-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!