Skip to content

Commit

Permalink
Support ignoring PS failures (#14)
Browse files Browse the repository at this point in the history
* Handle null results

* Support overriding immediate actions scheduling

* Support ignoring PS failures
  • Loading branch information
nirbar authored and flcdrg committed Apr 4, 2017
1 parent 37bd867 commit 56d17e3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
40 changes: 33 additions & 7 deletions PowerShellActions/CustomAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private static ActionResult FilesImmediate(Session session, int elevated, string
try
{
XDocument doc;
using (View view = db.OpenView(string.Format("SELECT `Id`, `File`, `Arguments` FROM `{0}` WHERE `Elevated` = {1}", tableName, elevated)))
using (View view = db.OpenView(string.Format("SELECT `Id`, `File`, `Arguments`, `IgnoreErrors` FROM `{0}` WHERE `Elevated` = {1}", tableName, elevated)))
{
view.Execute();

Expand All @@ -216,11 +216,12 @@ private static ActionResult FilesImmediate(Session session, int elevated, string
foreach (Record row in view)
{
var args = session.Format(row["Arguments"].ToString());
var IgnoreErrors = session.Format(row["IgnoreErrors"].ToString());

session.Log("args '{0}'", args);

doc.Root.Add(new XElement("d", new XAttribute("Id", row["Id"]),
new XAttribute("file", session.Format(row["File"].ToString())), new XAttribute("args", args)));
new XAttribute("file", session.Format(row["File"].ToString())), new XAttribute("args", args), new XAttribute("IgnoreErrors", IgnoreErrors)));
}
}

Expand Down Expand Up @@ -310,15 +311,40 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
string file = row.Attribute("file").Value;

string arguments = row.Attribute("args").Value;
string IgnoreErrors = row.Attribute("IgnoreErrors").Value;

using (var task = new PowerShellTask(file, arguments, session))
{
bool result = task.Execute();
session.Log("PowerShell non-terminating errors: {0}", !result);
if (!result)
try
{
session.Log("Returning Failure");
return ActionResult.Failure;
bool result = task.Execute();
session.Log("PowerShell non-terminating errors: {0}", !result);
if (!result)
{
if (!IgnoreErrors.Equals("0"))
{
session.Log("Ignoring failure due to 'IgnoreErrors' marking");
}
else
{
session.Log("Returning Failure");
return ActionResult.Failure;
}
}
}
catch (Exception ex)
{
if (!IgnoreErrors.Equals("0"))
{
session.Log("Ignoring PowerShell error due to 'IgnoreErrors' marking");
session.Log(ex.ToString());
}
else
{
session.Log("PowerShell terminating error, returning Failure");
session.Log(ex.ToString());
return ActionResult.Failure;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions PowerShellWixExtension/PowerShellCompilerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void ParseFileElement(XmlNode node)
string file = null;
string arguments = null;
var elevated = YesNoType.No;
YesNoType ignoreErrors = YesNoType.No;

foreach (XmlAttribute attribute in node.Attributes)
{
Expand All @@ -80,6 +81,9 @@ private void ParseFileElement(XmlNode node)
case "Elevated":
elevated = Core.GetAttributeYesNoValue(sourceLineNumber, attribute);
break;
case "IgnoreErrors":
ignoreErrors = Core.GetAttributeYesNoValue(sourceLineNumber, attribute);
break;
default:
Core.UnexpectedAttribute(sourceLineNumber, attribute);
break;
Expand Down Expand Up @@ -111,6 +115,7 @@ private void ParseFileElement(XmlNode node)
superElementRow[1] = file;
superElementRow[2] = arguments;
superElementRow[3] = elevated == YesNoType.Yes ? 1 : 0;
superElementRow[4] = (ignoreErrors == YesNoType.Yes) ? 1 : 0;
}

Core.CreateWixSimpleReferenceRow(sourceLineNumber, "CustomAction", "PowerShellFilesImmediate");
Expand Down
6 changes: 6 additions & 0 deletions PowerShellWixExtension/PowerShellWixExtensionSchema.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
</xs:annotation>
</xs:attribute>

<xs:attribute name="IgnoreErrors" use="optional" default="no" type="wix:YesNoType">
<xs:annotation>
<xs:documentation>Set to true to ignore PowerShell errors</xs:documentation>
</xs:annotation>
</xs:attribute>

</xs:complexType>
</xs:element>
</xs:schema>
10 changes: 10 additions & 0 deletions PowerShellWixExtension/TableDefinitions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
nullable="no"
description="Run as elevated" />

<columnDefinition
name="IgnoreErrors"
type="number"
category="integer"
length="0"
minValue="0"
maxValue="1"
nullable="no"
description="When non-zero, PowerShell errors are ignored." />

</tableDefinition>

</tableDefinitions>

0 comments on commit 56d17e3

Please sign in to comment.