Error The term Enable-Mailbox is not recognized as the name of a cmdlet, function, script file, or operable program.

I'm trying to update a C# command console application to use Exchange 2013. I used snapins before. I am getting this error on the following code.  What am I doing wrong?

The term 'Enable-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.

  public static bool EnableEmailAddress(Employee user)
        {
            bool succeed = true;
            String url = @"http://server/powershell?serializationLevel=Full";
            System.Uri uri = new Uri(url);
            Console.WriteLine(url);
     


            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();

            PowerShell powershell = PowerShell.Create();
            PSCommand command = new PSCommand();
            command.AddCommand("New-PSSession");


            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", uri);

       
            command.AddParameter("Authentication", "Kerberos");
         

            powershell.Commands = command;

            try
            {
                runspace.Open();

                powershell.Runspace = runspace;

                Collection<PSSession> result = powershell.Invoke<PSSession>();

                foreach (ErrorRecord current in powershell.Streams.Error)
                    Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                                                                                " | InnerException: " + current.Exception.InnerException);

                if (result.Count != 1)
                    throw new System.Exception("Unexpected number of Remote Runspace connections returned.");

                powershell = PowerShell.Create();

                command = new PSCommand();
                command.AddCommand("Enable-Mailbox");
                command.AddParameter("identity", user.adAccount.user.DistinguishedName);
                command.AddParameter("Alias", user.adAccount.user.SamAccountName);
                command.AddParameter("database", user.mailboxDatabasePath);
                powershell.Commands = command;
                powershell.Runspace = runspace;
                powershell.Invoke();

          


                Collection<PSObject> results = new Collection<PSObject>();
                results = powershell.Invoke();

                if (results == null || results.Count < 0)
                {
                    succeed = false;//false if fail
                }

           
            }

Open in new window

NickMalloySystems LeadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systechadminConsultantCommented:
Are you running the Exhange Shell as an administrator ? and also make sure that you have proper rights.
0
NickMalloySystems LeadAuthor Commented:
The script runs under our administrator account.

I did change things to see if I can get this to work. Mainly based on sample online

New error which I don't know how to correct.

Unexpected number of Remote Runspace connections returned.

My problem is I am not a powershell guy and I don't work with exchange much.

 bool succeed = true;
            String url = @"http://server/powershell?serializationLevel=Full";
            System.Uri uri = new Uri(url);
            Console.WriteLine(url);
         


            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();

            PowerShell powershell = PowerShell.Create();
            PSCommand command = new PSCommand();
            command.AddCommand("New-PSSession");


            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", uri);

            // command.AddParameter("Credential", creds);
            command.AddParameter("Authentication", "Kerberos");
            PSSessionOption sessionOption = new PSSessionOption();
            sessionOption.SkipCACheck = true;
            sessionOption.SkipCNCheck = true;
            sessionOption.SkipRevocationCheck = true;
            command.AddParameter("SessionOption", sessionOption);

            powershell.Commands = command;

            try
            {
                runspace.Open();

                powershell.Runspace = runspace;

                Collection<PSSession> result = powershell.Invoke<PSSession>();

                foreach (ErrorRecord current in powershell.Streams.Error)
                    Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                                                                                " | InnerException: " + current.Exception.InnerException);

                if (result.Count != 1)
                    throw new System.Exception("Unexpected number of Remote Runspace connections returned.");

                // Set the runspace as a local variable on the runspace
                powershell = PowerShell.Create();
                command = new PSCommand();
                command.AddCommand("Set-Variable");
                command.AddParameter("Name", "ra");
                command.AddParameter("Value", result[0]);
                powershell.Commands = command;
                powershell.Runspace = runspace;

                powershell.Invoke();

                // First import the cmdlets in the current runspace (using Import-PSSession)
                powershell = PowerShell.Create();
                command = new PSCommand();
                //command.AddScript("Set-ExecutionPolicy -Unrestricted");
                command.AddScript("Import-PSSession -Session $ra");
                powershell.Commands = command;
                powershell.Runspace = runspace;
                powershell.Invoke();

                // Now run get-ExchangeServer
                powershell = PowerShell.Create();
                command = new PSCommand();

        
                command.AddCommand("Enable-Mailbox");
                command.AddParameter("identity", user.adAccount.user.DistinguishedName);
                command.AddParameter("Alias", user.adAccount.user.SamAccountName);
                command.AddParameter("database", user.mailboxDatabasePath);
                powershell.Commands = command;
                powershell.Runspace = runspace;
                powershell.Invoke();

             


                Collection<PSObject> results = new Collection<PSObject>();
                results = powershell.Invoke();

                if (results == null || results.Count < 0)
                {
                    succeed = false;//false if fail
                }

        
            }

Open in new window

0
NickMalloySystems LeadAuthor Commented:
according to my administrator it is creating the mailboxes correctly, but my code is blowing the error.

Unexpected number of Remote Runspace connections returned.
0
NickMalloySystems LeadAuthor Commented:
needed to add Dispose to correct the error.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NickMalloySystems LeadAuthor Commented:
adding .dispose to the powershell corrected the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.