/// <summary>Executes supplied action within separate application domain.</summary> /// <param name="action">Action to be executed.</param> /// <returns>Any object that results from action execution or null if nothing is supposed to be returned.</returns> /// <exception cref="ArgumentNullException">Is thrown in case supplied action is null.</exception> private static TReturn ExecuteSharePointAction <TReturn>(SharePointAction <TReturn> action) { if (action == null) { throw new ArgumentNullException("action"); } AppDomain domain = null; try { Type type = typeof(HostedSharePointServer2019Impl); var info = new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), PrivateBinPath = GetPrivateBinPath() }; domain = AppDomain.CreateDomain("WSS30", null, info); var impl = (HostedSharePointServer2019Impl)domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); return(action(impl)); } finally { if (domain != null) { AppDomain.Unload(domain); } } throw new ArgumentNullException("action"); }
/// <summary> /// Executes supplied action within separate application domain. /// </summary> /// <param name="action">Action to be executed.</param> /// <returns>Any object that results from action execution or null if nothing is supposed to be returned.</returns> /// <exception cref="ArgumentNullException">Is thrown in case supplied action is null.</exception> private static TReturn ExecuteSharePointAction <TReturn>(SharePointAction <TReturn> action) { if (action == null) { throw new ArgumentNullException("action"); } AppDomain domain = null; try { // Create instance of server implementation in a separate application domain for // security and isolation purposes. Type type = typeof(HostedSharePointServerImpl); AppDomainSetup info = new AppDomainSetup(); info.ApplicationBase = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); info.PrivateBinPath = "bin; bin/debug"; domain = AppDomain.CreateDomain("WSS30", null, info); HostedSharePointServerImpl impl = (HostedSharePointServerImpl) domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); // Execute requested action within created application domain. return(action(impl)); } finally { if (domain != null) { AppDomain.Unload(domain); } } }
/// <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(SharePointAction action, string outputFromScript) { var results = new List <SharePointDeploymentStatus>(); switch (action) { case SharePointAction.GetFeature: case SharePointAction.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 != null) && (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()); }
internal static string GeneratePowerShellScript( string serverName, SharePointAction action, string wspName, string siteUrl, string wspLiteralPath, string featureName, bool gacDeployment, bool force, string otherParameters) { var command = "Add-PsSnapin Microsoft.SharePoint.PowerShell; "; switch (action) { case SharePointAction.AddSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Add-SPSolution -LiteralPath '{0}'", wspLiteralPath); break; case SharePointAction.InstallSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Install-SPSolution –Identity {0}", wspName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" –WebApplication {0}", siteUrl); } if (gacDeployment) { command += " -GACDeployment"; } if (force) { command += " -Force"; } break; case SharePointAction.UpdateSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Update-SPSolution –Identity {0} –LiteralPath '{1}'", wspName, wspLiteralPath); if (gacDeployment) { command += " -GACDeployment"; } if (force) { command += " -Force"; } break; case SharePointAction.UninstallSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Uninstall-SPSolution –Identity {0} -Confirm:$false", wspName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" –WebApplication {0}", siteUrl); } break; case SharePointAction.RemoveSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Remove-SPSolution –Identity {0} -Confirm:$false", wspName); break; case SharePointAction.EnableFeature: command = command.AppendFormat(CultureInfo.InvariantCulture, "enable-spfeature –Identity {0}", featureName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" -Url {0}", siteUrl); } if (force) { command += " -Force"; } break; case SharePointAction.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 SharePointAction.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 SharePointAction.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 (string.IsNullOrEmpty(serverName)) { return(command); } return(string.Format(CultureInfo.InvariantCulture, "invoke-command -computername {0} {{{1}}}", serverName, command)); }
/// <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(SharePointAction action, string outputFromScript) { var results = new List<SharePointDeploymentStatus>(); switch (action) { case SharePointAction.GetFeature: case SharePointAction.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 != null) && (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(); }
internal static string GeneratePowerShellScript( string serverName, SharePointAction action, string wspName, string siteUrl, string wspLiteralPath, string featureName, bool gacDeployment, bool force, string otherParameters) { var command = "Add-PsSnapin Microsoft.SharePoint.PowerShell; "; switch (action) { case SharePointAction.AddSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Add-SPSolution -LiteralPath '{0}'", wspLiteralPath); break; case SharePointAction.InstallSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Install-SPSolution –Identity {0}", wspName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" –WebApplication {0}", siteUrl); } if (gacDeployment) { command += " -GACDeployment"; } if (force) { command += " -Force"; } break; case SharePointAction.UpdateSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Update-SPSolution –Identity {0} –LiteralPath '{1}'", wspName, wspLiteralPath); if (gacDeployment) { command += " -GACDeployment"; } if (force) { command += " -Force"; } break; case SharePointAction.UninstallSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Uninstall-SPSolution –Identity {0} -Confirm:$false", wspName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" –WebApplication {0}", siteUrl); } break; case SharePointAction.RemoveSolution: command = command.AppendFormat(CultureInfo.InvariantCulture, "Remove-SPSolution –Identity {0} -Confirm:$false", wspName); break; case SharePointAction.EnableFeature: command = command.AppendFormat(CultureInfo.InvariantCulture, "enable-spfeature –Identity {0}", featureName); if (string.IsNullOrEmpty(siteUrl) == false) { command = command.AppendFormat(" -Url {0}", siteUrl); } if (force) { command += " -Force"; } break; case SharePointAction.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 SharePointAction.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 SharePointAction.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 (string.IsNullOrEmpty(serverName)) { return command; } return string.Format(CultureInfo.InvariantCulture, "invoke-command -computername {0} {{{1}}}", serverName, command); }