Skip to content

Commit

Permalink
Added Powershell Command saucepleez#255
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Jul 31, 2021
1 parent 8a9909a commit 1fd7841
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 4 deletions.
138 changes: 138 additions & 0 deletions taskt/Core/Automation/Commands/RunPowershellCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Programs/Process Commands")]
[Attributes.ClassAttributes.Description("This command allows you to run a powershell script and wait for it to exit before proceeding.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to run a powershell script and wait for it to close before taskt continues executing.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'Process.Start' and waits for the script/program to exit before proceeding.")]
public class RunPowershellCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Enter the path to the powershell script (ex. C:\\temp\\myscript.ps, {{{vScriptPath}}})")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a fully qualified path to the script, including the script extension.")]
[Attributes.PropertyAttributes.SampleUsage("**C:\\temp\\myscript.ps** or **{{{vScriptPath}}}**")]
[Attributes.PropertyAttributes.Remarks("This command differs from **Start Process** because this command blocks execution until the script has completed. If you do not want to stop while the script executes, consider using **Start Process** instead.")]
public string v_ScriptPath { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Enter Powershell Command Arguments")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter any necessary arguments")]
[Attributes.PropertyAttributes.SampleUsage("")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_PowerShellArgs { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Convert variables before execution")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
[Attributes.PropertyAttributes.InputSpecification("Select the necessary option.")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_ReplaceScriptVariables { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Select the variable to receive the output")]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_applyToVariableName { get; set; }
public RunPowershellCommand()
{
this.CommandName = "RunPowershellCommand";
this.SelectionName = "Run Powershell";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_PowerShellArgs = "-NoProfile -ExecutionPolicy unrestricted";
this.v_ReplaceScriptVariables = "No";
}

public override void RunCommand(object sender)
{
{

//define script path
var scriptPath = v_ScriptPath.ConvertToUserVariable(sender);

//get script text
var psCommand = System.IO.File.ReadAllText(scriptPath);

if (v_ReplaceScriptVariables.ToUpperInvariant() == "YES")
{
//convert variables
psCommand = psCommand.ConvertToUserVariable(sender);
}

//convert ps script
var psCommandBytes = System.Text.Encoding.Unicode.GetBytes(psCommand);
var psCommandBase64 = Convert.ToBase64String(psCommandBytes);

//execute
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"{v_PowerShellArgs} -EncodedCommand {psCommandBase64}",
UseShellExecute = false,
RedirectStandardOutput = true
};


var proc = Process.Start(startInfo);

proc.WaitForExit();

//store output into variable
StreamReader reader = proc.StandardOutput;
string output = reader.ReadToEnd();
output.StoreRawDataInUserVariable(sender, v_applyToVariableName);

}
}

public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ScriptPath", this, editor));

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_PowerShellArgs", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_ReplaceScriptVariables", this, editor));

RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;
}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Script Path: " + v_ScriptPath + "]";
}

public override bool IsValidate(frmCommandEditor editor)
{
base.IsValidate(editor);

if (String.IsNullOrEmpty(this.v_ScriptPath))
{
this.validationResult += "Script is empty.\n";
this.IsValid = false;
}

return this.IsValid;
}
}
}
1 change: 1 addition & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(RunScriptCommand))]
[XmlInclude(typeof(StartProcessCommand))]
[XmlInclude(typeof(StopProcessCommand))]
[XmlInclude(typeof(RunPowershellCommand))]

// Regex
[XmlInclude(typeof(GetRegexMatchesCommand))]
Expand Down
23 changes: 19 additions & 4 deletions taskt/Core/Automation/Commands/VariableCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ public class VariableCommand : ScriptCommand
[Attributes.PropertyAttributes.SampleUsage("**1** or **Hello** or {{{vNum}}}")]
[Attributes.PropertyAttributes.Remarks("You can use variables in input if you encase them within brackets {{{vName}}}. You can also perform basic math operations.")]
public string v_Input { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Convert Variables in Input Text Above")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
[Attributes.PropertyAttributes.InputSpecification("Select the necessary option.")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_ReplaceInputVariables { get; set; }

public VariableCommand()
{
this.CommandName = "VariableCommand";
this.SelectionName = "Set Variable";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_ReplaceInputVariables = "Yes";
}

public override void RunCommand(object sender)
Expand All @@ -54,9 +63,15 @@ public override void RunCommand(object sender)

if (requiredVariable != null)
{


var variableInput = v_Input.ConvertToUserVariable(sender);
string variableInput;
if (v_ReplaceInputVariables.ToUpperInvariant() == "YES")
{
variableInput = v_Input.ConvertToUserVariable(sender);
}
else
{
variableInput = v_Input;
}


if (variableInput.StartsWith("{{") && variableInput.EndsWith("}}"))
Expand Down Expand Up @@ -111,7 +126,7 @@ public override List<Control> Render(UI.Forms.frmCommandEditor editor)
RenderedControls.Add(VariableNameControl);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Input", this, editor));

RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_ReplaceInputVariables", this, editor));
return RenderedControls;
}

Expand Down
16 changes: 16 additions & 0 deletions taskt/Core/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ public static void StoreInUserVariable(this String str, object sender, string ta
v_Input = str
};
newVariableCommand.RunCommand(sender);
}

/// <summary>
/// Stores value of the string to a user-defined variable without attempting to convert variables
/// </summary>
/// <param name="sender">The script engine instance (frmScriptEngine) which contains session variables.</param>
/// <param name="targetVariable">the name of the user-defined variable to override with new value</param>
public static void StoreRawDataInUserVariable(this String str, object sender, string targetVariable)
{
Core.Automation.Commands.VariableCommand newVariableCommand = new Core.Automation.Commands.VariableCommand
{
v_userVariableName = targetVariable,
v_Input = str,
v_ReplaceInputVariables = "No"
};
newVariableCommand.RunCommand(sender);
}
/// <summary>
/// Formats item as a variable (enclosing brackets)s
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="249a7ce3-f8af-4716-a971-46584dee74e6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="Create .ps file with text 'Write-Host &quot;Hello, World from {rpa}!&quot;'" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="AddVariableCommand" CommandID="1a73c02c-0729-47b6-8656-e582fdf74658" CommandName="AddVariableCommand" IsCommented="false" SelectionName="New Variable" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_userVariableName="rpa" v_Input="taskt">
<v_IfExists>Replace If Variable Exists</v_IfExists>
</ScriptCommand>
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="249a7ce3-f8af-4716-a971-46584dee74e6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" v_Comment="Execute Powershell and replace with variables" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="RunPowershellCommand" CommandID="ef1ea10b-1698-44e3-a116-f0fb94e8d00f" CommandName="RunPowershellCommand" IsCommented="false" SelectionName="Run Powershell" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_ScriptPath="C:\Path\to\ps\script\run-me.ps" v_PowerShellArgs="-NoProfile -ExecutionPolicy unrestricted" v_ReplaceScriptVariables="Yes" v_applyToVariableName="vOut" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="249a7ce3-f8af-4716-a971-46584dee74e6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" v_Comment="Will Show 'Hello World from taskt'" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="2f51674b-fd4d-4799-bb35-a52c285018ad" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vOut}" v_AutoCloseAfter="0" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="249a7ce3-f8af-4716-a971-46584dee74e6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" v_Comment="Execute Powershell and do not replace with variables" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="RunPowershellCommand" CommandID="ef1ea10b-1698-44e3-a116-f0fb94e8d00f" CommandName="RunPowershellCommand" IsCommented="false" SelectionName="Run Powershell" DefaultPause="0" LineNumber="8" PauseBeforeExeucution="false" CommandEnabled="true" v_ScriptPath="C:\Path\to\ps\script\run-me.ps" v_PowerShellArgs="-NoProfile -ExecutionPolicy unrestricted" v_ReplaceScriptVariables="No" v_applyToVariableName="vOut" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="249a7ce3-f8af-4716-a971-46584dee74e6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="9" PauseBeforeExeucution="false" v_Comment="Will Show 'Hello World from {rpa}'" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="2f51674b-fd4d-4799-bb35-a52c285018ad" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="10" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vOut}" v_AutoCloseAfter="0" />
</ScriptAction>
</Commands>
<Variables>
<ScriptVariable>
<VariableName>vOut</VariableName>
<VariableValue xsi:type="xsd:string"></VariableValue>
</ScriptVariable>
</Variables>
</Script>
1 change: 1 addition & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
<Compile Include="Core\Automation\Commands\ExcelGetCellRCCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelRenameWorksheetCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelSetCellRCCommand.cs" />
<Compile Include="Core\Automation\Commands\RunPowershellCommand.cs" />
<Compile Include="Core\Automation\Commands\WordCheckWordInstanceExistsCommand.cs" />
<Compile Include="Core\Automation\Commands\SeleniumBrowserCheckBrowserInstanceExistsCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelCheckExcelInstanceExistsCommand.cs" />
Expand Down

0 comments on commit 1fd7841

Please sign in to comment.