forked from saucepleez/taskt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Powershell Command saucepleez#255
- Loading branch information
1 parent
8a9909a
commit 1fd7841
Showing
6 changed files
with
218 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
taskt/Sample Scripts/4.x Use Case Samples/Powershell Command Sample [4.0.0.0].xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "Hello, World from {rpa}!"'" 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters