Beispiel #1
0
 private void HandleWarningsErrors(PipelineReader <object> pipelineerrors)
 {
     foreach (string warning in hostinterface.Warnings)
     {
         PSAlert.Add(warning, psmethods.PSAlert.AlertType.Warning);
     }
     if (pipelineerrors.Count > 0)
     {
         Collection <object> errors = pipelineerrors.ReadToEnd();
         foreach (object error in errors)
         {
             PSAlert.Add(error.ToString(), psmethods.PSAlert.AlertType.Error);
         }
     }
     hostinterface.ClearWarnings();
 }
Beispiel #2
0
        /// <summary>
        /// invoke the powershell pipeline
        /// </summary>
        /// <param name="commands">a collection of commands to execute</param>
        /// <returns>collection of objects with the result
        /// if no command is passed in return null
        /// if no output from the invoke return an empty collection</returns>
        public ICollection <PSObject> InvokePipeline(Collection <Command> commands)
        {
            const string MethodName = "InvokePipeline";

            Debug.WriteLine(MethodName + ":entry", ClassName);

            IList errors = null;

            if (commands == null || commands.Count == 0)
            {
                throw new ArgumentException("Commands argument is null or empty");
            }

            // make sure the output is set
            errors = null;
            Collection <PSObject> results;

            // create the pipeline
            Pipeline pipe = this.runSpace.CreatePipeline();

            // add the commands to the pipeline
            foreach (Command item in commands)
            {
                pipe.Commands.Add(item);
            }

            // run the pipeline if we have something to execute
            results = pipe.Invoke();
            PipelineReader <object> reader = pipe.Error;

            errors = (IList)reader.ReadToEnd();

            // check for errors
            CheckErrors(errors);

            // an empty collection instead of null when we have executed
            if (results == null)
            {
                Debug.WriteLine("NO result returned");
                results = new Collection <PSObject>();
            }

            Debug.WriteLine(MethodName + ":exit", ClassName);
            return(results);
        }
Beispiel #3
0
        /// <summary>
        /// Determines the version of the Exchange server.
        /// </summary>
        /// <remarks>As the remote management functionality is not utilized, the Exchange powershell snap-in must be registered
        /// on the local computer. Different snap-in is used to manage Exchange 2007 and 2010, hence the server version is determined by the
        /// registered snap-in.
        /// </remarks>
        /// <returns>The version of the Exchange server to manage.</returns>
        /// <exception cref="ConnectorException">Thrown when the version cannot be determined.</exception>
        private ExchangeVersion GetExchangeServerVersion()
        {
            const string MethodName = "GetServerVersion";

            Debug.WriteLine(MethodName + ":entry", ClassName);

            const string ExchangeSnapinNamePrefix = "Microsoft.Exchange.Management.PowerShell.";

            ExchangeVersion?version = null;

            using (var runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();

                using (var pipeline = runspace.CreatePipeline())
                {
                    var getSnapinsCommand = new Command("Get-PSSnapin");
                    getSnapinsCommand.Parameters.Add("Registered");

                    pipeline.Commands.Add(getSnapinsCommand);

                    var snapinList = pipeline.Invoke();

                    PipelineReader <object> reader = pipeline.Error;
                    CheckErrors((IList)reader.ReadToEnd());

                    runspace.Close();

                    if ((snapinList == null) || (snapinList.Count == 0))
                    {
                        Debug.WriteLine("No snap-in returned");
                        throw new ConnectorException(_messageCatalog.Format("ex_NoPowerShellSnapins", "There are no registered PowerShell snap-ins."));
                    }

                    foreach (var snapin in snapinList)
                    {
                        if ((snapin.Properties["Name"] != null) &&
                            (snapin.Properties["Name"].Value != null) &&
                            (snapin.Properties["Name"].Value.ToString().StartsWith(ExchangeSnapinNamePrefix,
                                                                                   StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var snapinName = snapin.Properties["Name"].Value.ToString();
                            switch (snapinName.Substring(ExchangeSnapinNamePrefix.Length))
                            {
                            case "Admin":
                                //Microsoft.Exchange.Management.PowerShell.Admin snap-in is used to manage Exchange 2007
                                version = ExchangeVersion.E2007;
                                break;

                            case "E2010":
                                //Microsoft.Exchange.Management.PowerShell.E2010 snap-in is used to manage Exchange 2010
                                version = ExchangeVersion.E2010;
                                break;
                            }
                        }
                    }
                }
            }

            if (!version.HasValue)
            {
                throw new ConnectorException(_messageCatalog.Format("ex_NoSupportedExchangeSnapin",
                                                                    "There is no supported Exchange PowerShell snap-in registered."));
            }

            Debug.WriteLine(MethodName + ":exit", ClassName);
            return(version.Value);
        }
Beispiel #4
0
 private void HandleWarningsErrors(PipelineReader<object> pipelineerrors)
 {
     foreach (string warning in hostinterface.Warnings)
     {
         PSAlert.Add(warning, psmethods.PSAlert.AlertType.Warning);
     }
     if (pipelineerrors.Count > 0)
     {
         Collection<object> errors = pipelineerrors.ReadToEnd();
         foreach (object error in errors)
         {
             PSAlert.Add(error.ToString(), psmethods.PSAlert.AlertType.Error);
         }
     }
     hostinterface.ClearWarnings();
 }