Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zs-3 authored Aug 3, 2024
1 parent e28a69d commit 72680a2
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 255 deletions.
75 changes: 75 additions & 0 deletions ZS/Cmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,81 @@ namespace ZS
[SmallBasicType]
public static class ZSCmd
{

/// <summary>
/// Executes a batch script directly from a single string.
/// </summary>
/// <param name="script">The batch script to execute.</param>
/// <returns>A string containing the output and errors from the batch script execution.</returns>
public static Primitive RunBatchScript(Primitive script)
{
try {
using (Process process = new Process()) {
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c \"" + script + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();

return new Primitive("Output:\n" + output + "\nError:\n" + error);
}
} catch (Exception ex) {
return new Primitive("Exception:\n" + ex.Message);
}
}

/// <summary>
/// Executes a batch script provided as an array of strings.
/// Saves the script in a temporary file, runs it, returns the output, and deletes the script file.
/// </summary>
/// <param name="scriptLines">Array of strings, each representing a line of the batch script.</param>
/// <returns>A string containing the output and errors from the batch script execution.</returns>
public static Primitive RunBatchScriptFromArray(Primitive scriptLines)
{
string tempFilePath = Path.Combine(Path.GetTempPath(), "tempScript.bat");

try {
// Combine script lines into a single script and write to temp file
using (StreamWriter writer = new StreamWriter(tempFilePath)) {
int count = (int)scriptLines.GetItemCount();
for (int i = 1; i <= count; i++) {
writer.WriteLine(scriptLines[i].ToString());
}
}

using (Process process = new Process()) {
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c \"" + tempFilePath + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();

return new Primitive("Output:\n" + output + "\nError:\n" + error);
}
} catch (Exception ex) {
return new Primitive("Exception:\n" + ex.Message);
} finally {
// Delete the temporary batch script file
if (System.IO.File.Exists(tempFilePath)) {
System.IO.File.Delete(tempFilePath);
}
}
}

/// <summary>
/// Executes a command in the command line and returns the output.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions ZS/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static Primitive InvokeInstanceMethod(Primitive instanceId, Primitive met
/// <param name="methodName">The name of the method to invoke.</param>
/// <param name="argumentsStackName">The name of the stack containing the method arguments.</param>
/// <returns>The result of the method invocation or "ERROR" if invocation fails.</returns>
[HideFromIntellisense]
public static Primitive InvokeStaticMethod(Primitive typeName, Primitive methodName, Primitive argumentsStackName)
{
try
Expand Down
32 changes: 31 additions & 1 deletion ZS/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ namespace ZS
[SmallBasicType]
public static class ZSPowerShell
{

/// <summary>
/// Executes a PowerShell script directly from a single string.
/// </summary>
/// <param name="script">The PowerShell script to execute.</param>
/// <returns>A string containing the output and errors from the PowerShell script execution.</returns>
public static Primitive RunPowerShellScript(Primitive script)
{
try {
using (Process process = new Process()) {
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -Command \"" + script + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();

return new Primitive("Output:\n" + output + "\nError:\n" + error);
}
} catch (Exception ex) {
return new Primitive("Exception:\n" + ex.Message);
}
}

/// <summary>
/// Executes a PowerShell command and returns the result.
/// </summary>
Expand Down Expand Up @@ -128,7 +158,7 @@ public static Primitive RunPowerShellFile(Primitive filePath)
/// </summary>
/// <param name="scriptLines">Array of strings, each representing a line of the PowerShell script.</param>
/// <returns>A string containing the output and errors from the PowerShell script execution.</returns>
public static Primitive RunPowerShellScript(Primitive scriptLines)
public static Primitive RunPowerShell(Primitive scriptLines)
{
string tempFilePath = Path.Combine(Path.GetTempPath(), "tempScript.ps1");

Expand Down
Loading

0 comments on commit 72680a2

Please sign in to comment.