Ejemplo n.º 1
0
        /// <summary>
        /// Capture the return results for any commands that return lists
        /// </summary>
        /// <param name="action">The command passed</param>
        /// <param name="outputFromScript">The raw text from the powershell script</param>
        /// <returns>An array of processed lines</returns>
        internal static SharePointDeploymentStatus[] ProcessPowerShellOutput(SharePointRemoteAction action, string outputFromScript)
        {
            var results = new List <SharePointDeploymentStatus>();

            switch (action)
            {
            case SharePointRemoteAction.GetFeature:
            case SharePointRemoteAction.GetSolution:
                var lines = outputFromScript.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                SharePointDeploymentStatus newItem = null;
                foreach (var line in lines)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        var sections = line.Split(':');
                        if (sections.Length == 2)
                        {
                            switch (sections[0].Trim())
                            {
                            case "DisplayName":
                                newItem          = new SharePointDeploymentStatus();
                                newItem.Name     = sections[1].Trim();
                                newItem.Deployed = true;         // set a default as features don't pass this
                                break;

                            case "Id":
                                newItem.Id = Guid.Parse(sections[1].Trim());
                                results.Add(newItem);         // we need to make sure the ID is the last in the list
                                break;

                            case "Deployed":
                                newItem.Deployed = bool.Parse(sections[1].Trim());
                                break;
                            }
                        }
                    }
                }

                break;
            }

            return(results.ToArray());
        }
        /// <summary>
        /// Capture the return results for any commands that return lists
        /// </summary>
        /// <param name="action">The command passed</param>
        /// <param name="outputFromScript">The raw text from the powershell script</param>
        /// <returns>An array of processed lines</returns>
        internal static SharePointDeploymentStatus[] ProcessPowerShellOutput(SharePointRemoteAction action, string outputFromScript)
        {
            var results = new List<SharePointDeploymentStatus>();

            switch (action)
            {
                case SharePointRemoteAction.GetFeature:
                case SharePointRemoteAction.GetSolution:
                    var lines = outputFromScript.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    SharePointDeploymentStatus newItem = null;
                    foreach (var line in lines)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            var sections = line.Split(':');
                            if (sections.Length == 2)
                            {
                                switch (sections[0].Trim())
                                {
                                    case "DisplayName":
                                        newItem = new SharePointDeploymentStatus();
                                        newItem.Name = sections[1].Trim();
                                        newItem.Deployed = true; // set a default as features don't pass this
                                        break;
                                    case "Id":
                                        newItem.Id = Guid.Parse(sections[1].Trim());
                                        results.Add(newItem); // we need to make sure the ID is the last in the list
                                        break;
                                    case "Deployed":
                                        newItem.Deployed = bool.Parse(sections[1].Trim());
                                        break;
                                }
                            }
                        }
                    }

                    break;
            }

            return results.ToArray();
        }
Ejemplo n.º 3
0
        internal static string GeneratePowerShellScript(
            string serverName,
            SharePointRemoteAction action,
            string compatabilityLevel,
            string wspName,
            string siteUrl,
            string wspLiteralPath,
            string featureName,
            bool gacDeployment,
            bool force,
            string otherParameters,
            bool useCredSSP,
            string domain,
            string userName,
            string password)
        {
            string command = " Add-PsSnapin Microsoft.SharePoint.PowerShell; ";

            switch (action)
            {
            case SharePointRemoteAction.AddSolution:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "Add-SPSolution -LiteralPath '{0}'", wspLiteralPath);
                break;

            case SharePointRemoteAction.InstallSolution:

                command = command.AppendFormat(CultureInfo.InvariantCulture, "Install-SPSolution –Identity {0} ", wspName);
                if (string.IsNullOrEmpty(compatabilityLevel) == false)
                {
                    command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                }

                if (string.IsNullOrEmpty(siteUrl) == false)
                {
                    command = command.AppendFormat(" –WebApplication {0}", siteUrl);
                }

                if (gacDeployment)
                {
                    command += " -GACDeployment";
                }

                if (force)
                {
                    command += " -Force";
                }

                break;

            case SharePointRemoteAction.UpdateSolution:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "Update-SPSolution –Identity {0} –LiteralPath '{1}'", wspName, wspLiteralPath);
                if (gacDeployment)
                {
                    command += " -GACDeployment";
                }

                if (force)
                {
                    command += " -Force";
                }

                break;

            case SharePointRemoteAction.UninstallSolution:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "Uninstall-SPSolution –Identity {0} -Confirm:$false", wspName);
                if (string.IsNullOrEmpty(compatabilityLevel) == false)
                {
                    command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                }

                if (string.IsNullOrEmpty(siteUrl) == false)
                {
                    command = command.AppendFormat(" –WebApplication {0}", siteUrl);
                }

                break;

            case SharePointRemoteAction.RemoveSolution:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "Remove-SPSolution –Identity {0} -Confirm:$false", wspName);
                break;

            case SharePointRemoteAction.EnableFeature:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "enable-spfeature –Identity {0} ", featureName);
                if (string.IsNullOrEmpty(compatabilityLevel) == false)
                {
                    command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                }

                if (string.IsNullOrEmpty(siteUrl) == false)
                {
                    command = command.AppendFormat(" -Url {0}", siteUrl);
                }

                if (force)
                {
                    command += " -Force";
                }

                break;

            case SharePointRemoteAction.DisableFeature:
                command = command.AppendFormat(CultureInfo.InvariantCulture, "disable-spfeature –Identity {0} -Confirm:$false", featureName.Replace(" ", "_"));
                if (string.IsNullOrEmpty(siteUrl) == false)
                {
                    command = command.AppendFormat(" -Url {0}", siteUrl);
                }

                if (force)
                {
                    command += " -Force";
                }

                break;

            case SharePointRemoteAction.GetSolution:
                if (string.IsNullOrEmpty(wspName))
                {
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution");
                }
                else
                {
                    try
                    {
                        var guid = Guid.Parse(wspName);
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution | where {{$_.id -eq '{0}'}}", wspName);
                    }
                    catch (FormatException)
                    {
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution | where {{$_.name -eq '{0}'}}", wspName);
                    }
                }

                command += " | fl -property Displayname, Deployed, Id ;";
                break;

            case SharePointRemoteAction.GetFeature:
                if (string.IsNullOrEmpty(featureName))
                {
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature");
                }
                else
                {
                    try
                    {
                        var guid = Guid.Parse(featureName);
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature | where {{$_.id -eq '{0}'}}", featureName);
                    }
                    catch (FormatException)
                    {
                        // we need to replace spaces with underscore
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature | where {{$_.displayname -eq '{0}'}}", featureName.Replace(' ', '_'));
                    }
                }

                // we now need to add the handling to make sure the format is consistant both locally and remotely
                // using the plus operator to make sure no {0} confusion
                command += " | fl -property Displayname, Id ;";

                break;

            default:
                throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "Unknown SharePointAction [{0}] specified", action));
            }

            if (string.IsNullOrEmpty(otherParameters) == false)
            {
                command = command.AppendFormat(" {0}", otherParameters);
            }

            if (useCredSSP)
            {
                return(string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} -Authentication CredSSP -credential $pp {{{1}}}", serverName, command));
            }

            if (!string.IsNullOrEmpty(domain) &&
                !string.IsNullOrEmpty(userName))
            {
                return(string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} -credential $pp {{{1}}}", serverName, command));
            }

            return(string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} {{{1}}}", serverName, command));
        }
        internal static string GeneratePowerShellScript(
            string serverName,
            SharePointRemoteAction action,
            string compatabilityLevel,
            string wspName,
            string siteUrl,
            string wspLiteralPath,
            string featureName,
            bool gacDeployment,
            bool force,
            string otherParameters,
            bool useCredSSP,
            string domain,
            string userName,
            string password)
        {   
            string command = " Add-PsSnapin Microsoft.SharePoint.PowerShell; ";

            switch (action)
            {
                case SharePointRemoteAction.AddSolution:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "Add-SPSolution -LiteralPath '{0}'", wspLiteralPath);
                    break;
                case SharePointRemoteAction.InstallSolution:

                    command = command.AppendFormat(CultureInfo.InvariantCulture, "Install-SPSolution –Identity {0} ", wspName);
                    if (string.IsNullOrEmpty(compatabilityLevel) == false)
                    {
                        command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                    }

                    if (string.IsNullOrEmpty(siteUrl) == false)
                    {
                        command = command.AppendFormat(" –WebApplication {0}", siteUrl);
                    }

                    if (gacDeployment)
                    {
                        command += " -GACDeployment";
                    }

                    if (force)
                    {
                        command += " -Force";
                    }

                    break;
                case SharePointRemoteAction.UpdateSolution:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "Update-SPSolution –Identity {0} –LiteralPath '{1}'", wspName, wspLiteralPath);
                    if (gacDeployment)
                    {
                        command += " -GACDeployment";
                    }

                    if (force)
                    {
                        command += " -Force";
                    }

                    break;
                case SharePointRemoteAction.UninstallSolution:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "Uninstall-SPSolution –Identity {0} -Confirm:$false", wspName);
                    if (string.IsNullOrEmpty(compatabilityLevel) == false)
                    {
                        command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                    }

                    if (string.IsNullOrEmpty(siteUrl) == false)
                    {
                        command = command.AppendFormat(" –WebApplication {0}", siteUrl);
                    }

                    break;
                case SharePointRemoteAction.RemoveSolution:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "Remove-SPSolution –Identity {0} -Confirm:$false", wspName);
                    break;
                case SharePointRemoteAction.EnableFeature:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "enable-spfeature –Identity {0} ", featureName);
                    if (string.IsNullOrEmpty(compatabilityLevel) == false)
                    {
                        command = command.AppendFormat(" -CompatibilityLevel {0}", compatabilityLevel);
                    }

                    if (string.IsNullOrEmpty(siteUrl) == false)
                    {
                        command = command.AppendFormat(" -Url {0}", siteUrl);
                    }

                    if (force)
                    {
                        command += " -Force";
                    }

                    break;
                case SharePointRemoteAction.DisableFeature:
                    command = command.AppendFormat(CultureInfo.InvariantCulture, "disable-spfeature –Identity {0} -Confirm:$false", featureName.Replace(" ", "_"));
                    if (string.IsNullOrEmpty(siteUrl) == false)
                    {
                        command = command.AppendFormat(" -Url {0}", siteUrl);
                    }

                    if (force)
                    {
                        command += " -Force";
                    }

                    break;
                case SharePointRemoteAction.GetSolution:
                    if (string.IsNullOrEmpty(wspName))
                    {
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution");
                    }
                    else
                    {
                        try
                        {
                            var guid = Guid.Parse(wspName);
                            command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution | where {{$_.id -eq '{0}'}}", wspName);
                        }
                        catch (FormatException)
                        {
                            command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spsolution | where {{$_.name -eq '{0}'}}", wspName);
                        }
                    }

                    command += " | fl -property Displayname, Deployed, Id ;";
                    break;
                case SharePointRemoteAction.GetFeature:
                    if (string.IsNullOrEmpty(featureName))
                    {
                        command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature");
                    }
                    else
                    {
                        try
                        {
                            var guid = Guid.Parse(featureName);
                            command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature | where {{$_.id -eq '{0}'}}", featureName);
                        }
                        catch (FormatException)
                        {
                            // we need to replace spaces with underscore
                            command = command.AppendFormat(CultureInfo.InvariantCulture, "get-spfeature | where {{$_.displayname -eq '{0}'}}", featureName.Replace(' ', '_'));
                        }
                    }

                    // we now need to add the handling to make sure the format is consistant both locally and remotely
                    // using the plus operator to make sure no {0} confusion
                    command += " | fl -property Displayname, Id ;";

                    break;
                default:
                    throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "Unknown SharePointAction [{0}] specified", action));
            }

            if (string.IsNullOrEmpty(otherParameters) == false)
            {
                command = command.AppendFormat(" {0}", otherParameters);
            }                     

            if (useCredSSP)
            {
                return string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} -Authentication CredSSP -credential $pp {{{1}}}", serverName, command);
            }

            if (!string.IsNullOrEmpty(domain) &&
                !string.IsNullOrEmpty(userName))
            {
                return string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} -credential $pp {{{1}}}", serverName, command);
            }

            return string.Format(CultureInfo.InvariantCulture, " invoke-command -computername {0} {{{1}}}", serverName, command);
        }