(nameof(WorkingDirectory), value);
}
}
internal override TaskActionType InternalActionType => TaskActionType.Execute;
/// Determines whether the specified path is a valid filename and, optionally, if it exists.
/// The path.
/// if set to true check if file exists.
/// if set to true throw exception on error.
/// true if the specified path is a valid filename; otherwise, false.
public static bool IsValidPath(string path, bool checkIfExists = true, bool throwOnException = false)
{
try
{
if (path == null) throw new ArgumentNullException(nameof(path));
/*if (path.StartsWith("\"") && path.EndsWith("\"") && path.Length > 1)
path = path.Substring(1, path.Length - 2);*/
var fn = System.IO.Path.GetFileName(path);
System.Diagnostics.Debug.WriteLine($"IsValidPath fn={fn}");
if (fn == string.Empty)
return false;
var dn = System.IO.Path.GetDirectoryName(path);
System.Diagnostics.Debug.WriteLine($"IsValidPath dir={dn ?? "null"}");
System.IO.Path.GetFullPath(path);
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"IsValidPath exc={ex}");
if (throwOnException) throw;
}
return false;
}
/// Indicates whether the current object is equal to another object of the same type.
/// An object to compare with this object.
/// true if the current object is equal to the parameter; otherwise, false.
public override bool Equals(Action other) => base.Equals(other) && Path == ((ExecAction)other).Path && Arguments == ((ExecAction)other).Arguments && WorkingDirectory == ((ExecAction)other).WorkingDirectory;
///
/// Validates the input as a valid filename and optionally checks for its existence. If valid, the property is
/// set to the validated absolute file path.
///
/// The file path to validate.
/// if set to true check if the file exists.
public void SetValidatedPath([NotNull] string path, bool checkIfExists = true)
{
if (IsValidPath(path, checkIfExists, true))
Path = path;
}
/// Gets a string representation of the .
/// String representation of this action.
public override string ToString() => string.Format(Properties.Resources.ExecAction, Path, Arguments, WorkingDirectory, Id);
internal static string BuildPowerShellCmd(string actionType, string cmd) => string.Format(PowerShellArgFormat, ScriptIdentifer, actionType, cmd);
internal static ExecAction ConvertToPowerShellAction(Action action) => CreatePowerShellAction(action.ActionType.ToString(), action.GetPowerShellCommand());
internal static ExecAction CreatePowerShellAction(string actionType, string cmd) => new ExecAction(PowerShellPath, BuildPowerShellCmd(actionType, cmd));
internal static Action FromPowerShellCommand(string p)
{
var match = System.Text.RegularExpressions.Regex.Match(p, "^Start-Process -FilePath '(?[^']*)'(?: -ArgumentList '(?[^']*)')?(?: -WorkingDirectory '(?[^']*)')?;?\\s*$");
return match.Success ? new ExecAction(match.Groups["p"].Value, match.Groups["a"].Success ? match.Groups["a"].Value.Replace("''", "'") : null, match.Groups["d"].Success ? match.Groups["d"].Value : null) : null;
}
/// Copies the properties from another the current instance.
/// The source .
internal override void CopyProperties(Action sourceAction)
{
if (sourceAction.GetType() == GetType())
{
base.CopyProperties(sourceAction);
Path = ((ExecAction)sourceAction).Path;
Arguments = ((ExecAction)sourceAction).Arguments;
WorkingDirectory = ((ExecAction)sourceAction).WorkingDirectory;
}
}
internal override void CreateV2Action(IActionCollection iActions) => iAction = iActions.Create(TaskActionType.Execute);
internal override string GetPowerShellCommand()
{
var sb = new System.Text.StringBuilder($"Start-Process -FilePath '{Path}'");
if (!string.IsNullOrEmpty(Arguments))
sb.Append($" -ArgumentList '{Arguments.Replace("'", "''")}'");
if (!string.IsNullOrEmpty(WorkingDirectory))
sb.Append($" -WorkingDirectory '{WorkingDirectory}'");
return sb.Append("; ").ToString();
}
internal string[] ParsePowerShellItems()
{
if (((Path?.EndsWith(PowerShellPath, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
(Path?.EndsWith(PowerShellPath + ".exe", StringComparison.InvariantCultureIgnoreCase) ?? false)) && (Arguments?.Contains(ScriptIdentifer) ?? false))
{
var match = System.Text.RegularExpressions.Regex.Match(Arguments, @"<# " + ScriptIdentifer + ":(?\\w+) #> (?.+)}\"$");
if (match.Success)
return new[] { match.Groups["type"].Value, match.Groups["cmd"].Value };
}
return null;
}
}
///
/// Represents an action that shows a message box when a task is activated. Only available for Task Scheduler 2.0 on Windows Vista
/// or Windows Server 2003 and later.This action has been deprecated in Windows 8 and later. However, this
/// library is able to mimic its functionality using PowerShell if the property is
/// set to . To disable this conversion, set the value to .
///
/// Display a message when the trigger fires using the ShowMessageAction.
///
///
///
///
///
[XmlType(IncludeInSchema = true)]
[XmlRoot("ShowMessage", Namespace = TaskDefinition.tns, IsNullable = false)]
public sealed class ShowMessageAction : Action, IBindAsExecAction
{
/// Creates a new unbound instance of .
public ShowMessageAction() { }
/// Creates a new unbound instance of .
/// Message text that is displayed in the body of the message box.
/// Title of the message box.
public ShowMessageAction([CanBeNull] string messageBody, [CanBeNull] string title)
{
MessageBody = messageBody;
Title = title;
}
internal ShowMessageAction([NotNull] V1Interop.ITask task) : base(task)
{
}
internal ShowMessageAction([NotNull] IAction action) : base(action)
{
}
/// Gets or sets the message text that is displayed in the body of the message box.
[XmlElement("Body")]
[DefaultValue(null)]
public string MessageBody
{
get => GetProperty(nameof(MessageBody));
set => SetProperty(nameof(MessageBody), value);
}
/// Gets or sets the title of the message box.
[DefaultValue(null)]
public string Title
{
get => GetProperty(nameof(Title));
set => SetProperty(nameof(Title), value);
}
internal override TaskActionType InternalActionType => TaskActionType.ShowMessage;
/// Indicates whether the current object is equal to another object of the same type.
/// An object to compare with this object.
/// true if the current object is equal to the parameter; otherwise, false.
public override bool Equals(Action other) => base.Equals(other) && string.Equals(Title, (other as ShowMessageAction)?.Title) && string.Equals(MessageBody, (other as ShowMessageAction)?.MessageBody);
/// Gets a string representation of the .
/// String representation of this action.
public override string ToString() => string.Format(Properties.Resources.ShowMessageAction, Title, MessageBody, Id);
internal static Action FromPowerShellCommand(string p)
{
var match = System.Text.RegularExpressions.Regex.Match(p, @"^\[System.Reflection.Assembly\]::LoadWithPartialName\('System.Windows.Forms'\); \[System.Windows.Forms.MessageBox\]::Show\('(?(?:[^']|'')*)'(?:,'(?(?:[^']|'')*)')?\);?\s*$");
return match.Success ? new ShowMessageAction(match.Groups["msg"].Value.Replace("''", "'"), match.Groups["t"].Success ? match.Groups["t"].Value.Replace("''", "'") : null) : null;
}
/// Copies the properties from another the current instance.
/// The source .
internal override void CopyProperties(Action sourceAction)
{
if (sourceAction.GetType() == GetType())
{
base.CopyProperties(sourceAction);
Title = ((ShowMessageAction)sourceAction).Title;
MessageBody = ((ShowMessageAction)sourceAction).MessageBody;
}
}
internal override void CreateV2Action(IActionCollection iActions) => iAction = iActions.Create(TaskActionType.ShowMessage);
internal override string GetPowerShellCommand()
{
// [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Your_Desired_Message','Your_Desired_Title');
var sb = new System.Text.StringBuilder("[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('");
sb.Append(MessageBody.Replace("'", "''"));
if (Title != null)
{
sb.Append("','");
sb.Append(Title.Replace("'", "''"));
}
sb.Append("'); ");
return sb.ToString();
}
}
}