Home > Uncategorized > SharePoint Powershell Script Generator for SPSolution Deployment

SharePoint Powershell Script Generator for SPSolution Deployment

Here is an easy way to deploy .wsp files on your servers,

Script Generator UI

Steps:

  • Start the application (it will ask for administrator privileges).
  • Browse for the .wsp solution file.
  • Enter the solution identity (default to .wsp file name).
  • Enter the feature name.
  • Enter the web application url to activate the feature on (default to http://currentServerName:80/).
  • Decide whether to deploy the solution to all applications (default to all applications).
  • Hit the Generate Batch Files button!

This will open the folder that contains the .wsp file, and show you the generated .ps1 file that contains the Powershell commands, and the .bat file that shall run the .ps1 file, and automatically runs the batch file.

Generated Script Files

If you are into editing and deploying the same .wsp file many times, the procedure of deploying will just be 1 click of hitting the Generate Batch Files button.

Here is the application source code for whom of you guys interested,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SharePointPowerShellScriptGenerator
{
    public partial class FormMain : Form
    {
        #region Constructor

        public FormMain()
        {
            InitializeComponent();

            openFileDialogSolutionWsp.AutoUpgradeEnabled = true;
            openFileDialogSolutionWsp.CheckFileExists = true;
            openFileDialogSolutionWsp.CheckPathExists = true;
            openFileDialogSolutionWsp.DefaultExt = ".wsp";
            openFileDialogSolutionWsp.Multiselect = false;
            openFileDialogSolutionWsp.RestoreDirectory = true;
            openFileDialogSolutionWsp.SupportMultiDottedExtensions = true;
            openFileDialogSolutionWsp.Filter = "SharePoint Solution Package (*.wsp)|*.wsp";
            openFileDialogSolutionWsp.FilterIndex = 0;
            openFileDialogSolutionWsp.FileName = "SharePoint_Solution_Package.wsp";

            WebApplicationUrl = string.Format("http://{0}:80/", Environment.MachineName);
            for (int i = 0; i < checkedListBoxActions.Items.Count; i++)
            {
                checkedListBoxActions.SetItemChecked(i, true);
            }
        }

        #endregion

        #region Properties

        public string SolutionIdentity { get { return textBoxIdentity.Text.Trim(); } set { textBoxIdentity.Text = value; } }
        public string SolutionPath { get { return textBoxSolutionPath.Text.Trim(); } set { textBoxSolutionPath.Text = value; } }
        public string WebApplicationUrl { get { return textBoxWebApplicationUrl.Text.Trim(); } set { textBoxWebApplicationUrl.Text = value; } }
        public string FeatureName { get { return textBoxFeatureName.Text.Trim(); } set { textBoxFeatureName.Text = value; } }
        public string DirectoryPath { get; set; }

        #endregion

        #region Methods

        private void GeneratePowershellScript(string generatedFileName)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("Add-PsSnapin Microsoft.SharePoint.PowerShell"));
            sb.AppendLine();
            sb.AppendLine(string.Format(@"$SiteUrl=""{0}""", WebApplicationUrl));
            sb.AppendLine(string.Format(@"$SolutionName=""{0}""", SolutionIdentity));
            sb.AppendLine(string.Format(@"$SolutionPath=""{0}""", SolutionPath));
            sb.AppendLine(string.Format(@"$FeatureName=""{0}""", FeatureName));
            sb.AppendLine();
            sb.AppendLine("$FeatureID= $(Get-SPFeature -limit all | ? {($_.displayname -eq $FeatureName)}).Id");
            sb.AppendLine(string.Format("$SingleSiteCollection = Get-SPSite $SiteUrl"));
            sb.AppendLine();
            sb.AppendLine();

            // STARTING SHAREPOINT ADMINISTRATING SERVICE
            sb.AppendLine(@"$AdminServiceName = ""SPAdminV4""");
            sb.AppendLine("$IsAdminServiceWasRunning = $true;");
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine(@"if ($(Get-Service $AdminServiceName).Status -eq ""Stopped"")");
            sb.AppendLine("{");
            sb.AppendLine("    $IsAdminServiceWasRunning = $false;");
            sb.AppendLine("    Start-Service $AdminServiceName");
            sb.AppendLine(@"   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'");
            sb.AppendLine("}");
            sb.AppendLine();

            // DISABLE FEATURE
            if (checkedListBoxActions.GetItemChecked(0))
            {
                sb.AppendLine(@"Write-Host 'DEACTIVATING FEATURE ...'");
                sb.AppendLine();
                sb.AppendLine("Disable-SPFeature $FeatureName -Url $SiteUrl -Confirm:$false");
                sb.AppendLine(@"Write-Host 'FEATURE HAS BEEN DEACTIVATED SUCCESSFULLY.'");
                sb.AppendLine();
            }

            // UNINSTALL SOLUTION
            if (checkedListBoxActions.GetItemChecked(1))
            {
                sb.AppendLine(@"Write-Host 'UNINSTALLING SOLUTION ...'");
                sb.AppendLine();
                sb.AppendLine("$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}");
                sb.AppendLine();
                sb.AppendLine("if ($Solution -ne $null)");
                sb.AppendLine("{");
                sb.AppendLine("    if($Solution.ContainsWebApplicationResource)");
                sb.AppendLine("    {");
                sb.AppendLine("        Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false");
                sb.AppendLine("    }");
                sb.AppendLine("    else");
                sb.AppendLine("    {");
                sb.AppendLine("        Uninstall-SPSolution $SolutionName -Confirm:$false");
                sb.AppendLine("    }");
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("while ($Solution.JobExists)");
                sb.AppendLine("{");
                sb.AppendLine("    Start-Sleep 2");
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine(@"Write-Host 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'");
                sb.AppendLine();
            }

            // REMOVE SOLUTION
            if (checkedListBoxActions.GetItemChecked(2))
            {
                sb.AppendLine(@"Write-Host 'REMOVING SOLUTION ...'");
                sb.AppendLine();
                sb.AppendLine("if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)");
                sb.AppendLine("{");
                sb.AppendLine("    Remove-SPSolution $SolutionName -Confirm:$false");
                sb.AppendLine();
                sb.AppendLine(@"Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'");
                sb.AppendLine("}");
                sb.AppendLine();
            }

            // ADD SOLUTION
            if (checkedListBoxActions.GetItemChecked(3))
            {
                sb.AppendLine(@"Write-Host 'ADDING SOLUTION ...'");
                sb.AppendLine();
                sb.AppendLine("Add-SPSolution $SolutionPath  | Out-Null");
                sb.AppendLine();
                sb.AppendLine(@"Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'");
                sb.AppendLine();
            }

            // INSTALL SOLUTION
            if (checkedListBoxActions.GetItemChecked(4))
            {
                sb.AppendLine(@"Write-Host 'DEPLOYING SOLUTION ...'");
                sb.AppendLine();
                sb.AppendLine("$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}");
                sb.AppendLine();
                sb.AppendLine("if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))");
                sb.AppendLine("{");

                if (checkBoxDeployToAll.Checked)
                {
                    sb.AppendLine("Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Confirm:$false");
                }
                else
                {
                    sb.AppendLine("Install-SPSolution $SolutionName -WebApplication $SiteUrl -GACDeployment -Confirm:$false");
                }
                
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                sb.AppendLine("Install-SPSolution $SolutionName -GACDeployment -Confirm:$false");
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("while ($Solution.Deployed -eq $false)");
                sb.AppendLine("{");
                sb.AppendLine("    Start-Sleep 2");
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine(@"Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'");
                sb.AppendLine();
            }

            // ENABLE FEATURE
            if (checkedListBoxActions.GetItemChecked(5))
            {
                sb.AppendLine(@"Write-Host 'ACTIVATING FEATURE ...'");
                sb.AppendLine();
                sb.AppendLine("if ($FeatureName -ne $null)");
                sb.AppendLine("{");
                sb.AppendLine("    Enable-SPFeature $FeatureName -Url $SiteUrl");
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine(@"Write-Host 'FEATURE HAS BEEN ACTIVATED SUCCESSFULLY.'");
                sb.AppendLine();
            }

            // SHAREPOINT ADMINISTRATING SERVICE
            sb.AppendLine();
            sb.AppendLine("if (-not $IsAdminServiceWasRunning)");
            sb.AppendLine("{");
            sb.AppendLine("    Stop-Service $AdminServiceName");
            sb.AppendLine("}");
            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("Remove-PsSnapin Microsoft.SharePoint.PowerShell");
            sb.AppendLine();
            sb.AppendLine("Echo Finish");

            File.WriteAllText(generatedFileName, sb.ToString(), Encoding.UTF8);
        }

        private void GenerateBatchFile(string powershellFileName, string generatedBatchFileName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("@echo off");
            sb.AppendLine(string.Format("Set DeploymentPackageFolder={0}", DirectoryPath));
            sb.AppendLine("cd %DeploymentPackageFolder%");
            sb.AppendLine(string.Format(@"PowerShell -file .\{0}", powershellFileName));
            sb.AppendLine("pause");
            
            File.WriteAllText(generatedBatchFileName, sb.ToString());
        }

        #endregion

        #region UI Events

        private void checkedListBoxActions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (checkedListBoxActions.SelectedIndex != 0) return;

            bool selected = checkedListBoxActions.GetItemChecked(checkedListBoxActions.SelectedIndex);

            checkedListBoxActions.SetItemChecked(1, selected);
            checkedListBoxActions.SetItemChecked(2, selected);
        }

        private void buttonSolutionBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialogSolutionWsp.ShowDialog() != DialogResult.OK) return;

            SolutionPath = openFileDialogSolutionWsp.FileName;
            SolutionIdentity = openFileDialogSolutionWsp.SafeFileName;

            DirectoryPath = openFileDialogSolutionWsp.FileName.Remove(openFileDialogSolutionWsp.FileName.IndexOf(openFileDialogSolutionWsp.SafeFileName));
        }

        private void buttonGenerateScript_Click(object sender, EventArgs e)
        {
            try
            {
                GeneratePowershellScript(DirectoryPath + "script.txt");
                System.Diagnostics.Process.Start(DirectoryPath + "script.txt");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text);
            }
        }

        private void buttonGenerateBatch_Click(object sender, EventArgs e)
        {
            try
            {
                string powershellFileName = "PowershellDeploy.ps1";
                string batchFileName = "InstallerBatch.bat";
                GeneratePowershellScript(DirectoryPath + powershellFileName);
                GenerateBatchFile(powershellFileName, DirectoryPath + batchFileName);

                System.Diagnostics.Process.Start(DirectoryPath);
                System.Diagnostics.Process.Start(DirectoryPath + batchFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text);
            }
        }

        #endregion
    }
}
Advertisements
Categories: Uncategorized
  1. Derek Heasley
    November 9, 2010 at 7:28 pm

    I’d love to try this out. Your links ot source and project are broken.

  2. ken zheng
    November 11, 2010 at 12:14 am

    Hi Islam:

    The links to the source code has broken. Could you please update and I am keen to download the source code and use.

    Thanks

    Ken

  3. January 19, 2011 at 7:03 pm

    I am having the same problem, where can we find thie solution?

    • January 19, 2011 at 10:05 pm

      I’m having some problems hosting the solution files, I can temporary send it by email

  4. January 20, 2011 at 1:15 pm

    I really hate posting my emails on blogs. Can you see my email behind the posts?

  5. Net Deployment
    February 25, 2011 at 10:14 am

    Hello,
    Link broken, could you send me the Source code?

    Thanks

    • February 25, 2011 at 10:29 am

      I’m sorry yes the link is broken, I have sent it to your email

  6. Techneer
    March 4, 2011 at 2:10 pm

    Can you send me a copy too..

  7. Adeyemi Adelakun
    May 5, 2011 at 9:04 am

    Nice one!

    Link broken, please send the installer and source code.

  8. SalCarl
    May 17, 2011 at 12:03 pm

    Hi Islam could you please send me the source code. I would really like to try it.

    Thank you.
    Sal

  9. Dillon
    June 1, 2011 at 11:39 pm

    Can you provide me the source code? Thanks.

  10. Dejan Dujak
    June 27, 2011 at 10:45 am

    Hi Islam,

    Link is broken. Can I please have files in email.

    Thank you in advance.

    Kind regards,

    Dejan.

  11. Daniel Merillas
    June 30, 2011 at 3:03 pm

    Hi Islam, could you please send a copy? Daniel.merillas@gmail.com Thanks

  12. bilal
    July 7, 2011 at 3:25 pm

    very good one but as the links are broken, please email to mailtobilalahmad@yahoo.com

  13. August 5, 2011 at 6:05 pm

    Can you just fix the links?

  14. August 13, 2011 at 6:33 am

    Sorry all.. I fixed the links, please try again!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: