/// <summary>Opens PowerShell runspace.</summary>
        /// <returns>The runspace.</returns>
        private Runspace OpenRunspace()
        {
            HostedSolutionLog.LogStart("OpenRunspace");

            if (runspaceConfiguration == null)
            {
                runspaceConfiguration = RunspaceConfiguration.Create();
                PSSnapInException exception;
                runspaceConfiguration.AddPSSnapIn(SharepointSnapInName, out exception);
                HostedSolutionLog.LogInfo("Sharepoint snapin loaded");

                if (exception != null)
                {
                    HostedSolutionLog.LogWarning("SnapIn error", exception);
                }
            }

            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);

            runspace.Open();
            runspace.SessionStateProxy.SetVariable("ConfirmPreference", "none");
            HostedSolutionLog.LogEnd("OpenRunspace");

            return(runspace);
        }
        /// <summary>Executes shell command.</summary>
        /// <param name="runspace">The runspace.</param>
        /// <param name="scripts">The scripts to be executed.</param>
        /// <param name="errors">The errors.</param>
        /// <returns>PSobjecs collection.</returns>
        private Collection <PSObject> ExecuteShellCommand(Runspace runspace, List <string> scripts, out object[] errors)
        {
            HostedSolutionLog.LogStart("ExecuteShellCommand");
            var errorList = new List <object>();
            Collection <PSObject> results;

            using (Pipeline pipeLine = runspace.CreatePipeline())
            {
                foreach (string script in scripts)
                {
                    pipeLine.Commands.AddScript(script);
                }

                results = pipeLine.Invoke();

                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Add(item);
                        string errorMessage = string.Format("Invoke error: {0}", item);
                        HostedSolutionLog.LogWarning(errorMessage);

                        throw new ArgumentException(scripts.First());
                    }
                }
            }

            errors = errorList.ToArray();
            HostedSolutionLog.LogEnd("ExecuteShellCommand");

            return(results);
        }
Пример #3
0
        private void DeleteUserInternal(string instanceId)
        {
            HostedSolutionLog.LogStart("DeleteUserInternal");
            try
            {
                if (string.IsNullOrEmpty(instanceId))
                {
                    throw new ArgumentException("instanceId");
                }

                using (ManagementObject userObject = GetUserByInstanceId(instanceId))
                {
                    if (userObject == null)
                    {
                        HostedSolutionLog.LogWarning("OCS user {0} not found", instanceId);
                    }
                    else
                    {
                        userObject.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("DeleteUserInternal", ex);
                throw;
            }
            HostedSolutionLog.LogEnd("DeleteUserInternal");
        }
Пример #4
0
        private void AddDomainInternal(string domainName)
        {
            HostedSolutionLog.LogStart("AddDomainInternal");
            HostedSolutionLog.DebugInfo("Domain Name: {0}", domainName);
            try
            {
                if (string.IsNullOrEmpty(domainName))
                {
                    throw new ArgumentException("domainName");
                }

                if (GetDomain(domainName) != null)
                {
                    HostedSolutionLog.LogWarning("OCS internal domain '{0}' already exists", domainName);
                }
                else
                {
                    using (ManagementObject newDomain = Wmi.CreateInstance("MSFT_SIPFederationInternalDomainData"))
                    {
                        newDomain["SupportedInternalDomain"] = domainName;
                        newDomain.Put();
                    }
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("AddDomainInternal", ex);
                throw;
            }
            HostedSolutionLog.LogEnd("AddDomainInternal");
        }
Пример #5
0
        private void DeleteDomainInternal(string domainName)
        {
            HostedSolutionLog.LogStart("DeleteDomainInternal");
            HostedSolutionLog.DebugInfo("Domain Name: {0}", domainName);
            try
            {
                if (string.IsNullOrEmpty(domainName))
                {
                    throw new ArgumentException("domainName");
                }

                using (ManagementObject domainObj = GetDomain(domainName))
                {
                    if (domainObj == null)
                    {
                        HostedSolutionLog.LogWarning("OCS internal domain '{0}' not found", domainName);
                    }
                    else
                    {
                        domainObj.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("DeleteDomainInternal", ex);
                throw;
            }
            HostedSolutionLog.LogEnd("DeleteDomainInternal");
        }
Пример #6
0
        /// <summary> Executes shell command.</summary>
        /// <param name="runspace"> The runspace.</param>
        /// <param name="command"> The command.</param>
        /// <param name="useDomainController"> True - if domain controller should be used.</param>
        /// <param name="errors"> Errors list.</param>
        /// <returns> The result.</returns>
        internal Collection <PSObject> ExecuteShellCommand(Runspace runspace, Command command, bool useDomainController, out object[] errors)
        {
            HostedSolutionLog.LogStart("ExecuteShellCommand");
            var errorList = new List <object>();

            if (useDomainController)
            {
                var dc = new CommandParameter("DomainController", PrimaryDomainController);
                if (!command.Parameters.Contains(dc))
                {
                    command.Parameters.Add(dc);
                }
            }

            HostedSolutionLog.DebugCommand(command);
            Collection <PSObject> results;
            Pipeline pipeLine = runspace.CreatePipeline();

            using (pipeLine)
            {
                pipeLine.Commands.Add(command);
                results = pipeLine.Invoke();

                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Add(item);
                        string errorMessage = string.Format("Invoke error: {0}", item);
                        HostedSolutionLog.LogWarning(errorMessage);
                    }
                }
            }

            errors = errorList.ToArray();
            HostedSolutionLog.LogEnd("ExecuteShellCommand");

            return(results);
        }
        internal Collection <PSObject> ExecuteShellCommand(Runspace runSpace, Command cmd, bool useDomainController, out object[] errors)
        {
            HostedSolutionLog.LogStart("ExecuteShellCommand");
            List <object> errorList = new List <object>();

            HostedSolutionLog.DebugCommand(cmd);
            Collection <PSObject> results = null;
            // Create a pipeline
            Pipeline pipeLine = runSpace.CreatePipeline();

            using (pipeLine)
            {
                // Add the command
                pipeLine.Commands.Add(cmd);
                // Execute the pipeline and save the objects returned.
                results = pipeLine.Invoke();

                // Log out any errors in the pipeline execution
                // NOTE: These errors are NOT thrown as exceptions!
                // Be sure to check this to ensure that no errors
                // happened while executing the command.
                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Add(item);
                        string errorMessage = string.Format("Invoke error: {0}", item);
                        HostedSolutionLog.LogWarning(errorMessage);
                    }
                }
            }
            pipeLine = null;
            errors   = errorList.ToArray();
            HostedSolutionLog.LogEnd("ExecuteShellCommand");
            return(results);
        }