예제 #1
0
        internal Collection<PSObject> InvokeCommand(PSCommand command, ExchContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("Context", "Parametr not defined. [" + _log.Name + "]");
            }

            PSCredential _credential = GetPSCredential(context);

            WSManConnectionInfo _connection = new WSManConnectionInfo(
                new Uri(context.Uri),
                "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
                _credential);
            _connection.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
            _connection.MaximumConnectionRedirectionCount = 3;
            _connection.SkipCACheck = true;
            _connection.SkipCNCheck = true;
            

            using (Runspace _runspace = RunspaceFactory.CreateRunspace(_connection))
            {
                _runspace.Open();
                //_runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "Continue");

                using (PowerShell _powershell = PowerShell.Create())
                {

                    _powershell.Commands = command;

                    try
                    {

                        //RunspaceInvoke _invoker = new RunspaceInvoke(_runspace);
                        //Collection<PSObject> _commandResults = _invoker.Invoke(command.ToString());

                        _powershell.Runspace = _runspace;
                        Collection<PSObject> _commandResults = _powershell.Invoke();
                        CheckErrors(_powershell.Streams.Error);
                        return _commandResults;
                        //foreach (PSObject _result in _commandResults)
                        //{
                        //    Console.WriteLine(_result.ToString());
                        //}

                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message + ". [" + _log.Name + "]");
                    }
                    
                }
            }

        }
예제 #2
0
        internal PSCredential GetPSCredential(ExchContext context)
        {
            if (!string.IsNullOrEmpty(context.Login) && !string.IsNullOrEmpty(context.Password))
            {
                string _user = context.Name + @"\" + context.Login;
                SecureString _password = new SecureString();

                foreach (char x in context.Password)
                {
                    _password.AppendChar(x);
                }

                return new PSCredential(_user, _password);
            }
            else
            {
                throw new ArgumentNullException("Login or Password", "Parametr not set. [" + _log.Name + "]");
            }
        }
예제 #3
0
        public string GetDatabase(ExchContext context, string mailboxType)
        {
            if (context == null)
            {
                throw new Exception("Exchange context not defined. [" + _log.Name + "]");
            }

            if (context.Database == null)
            {
                throw new Exception("Mailbox databases not defined in this context <" + context.Name + ">. [" + _log.Name + "]");
            }

            if (string.IsNullOrEmpty(mailboxType))
            {
                throw new Exception("Mailbox type not defined. [" + _log.Name + "]");
            }

            if (context.Database.ContainsKey(mailboxType))
            {
                return context.Database[mailboxType];
            }

            throw new Exception("No database defined for this mailbox type <" + mailboxType + ">. [" + _log.Name + "]");
        }