Forum Discussion

PeterZ's avatar
PeterZ
New Contributor
2 years ago

Running Windows PowerShell during test execution

I am trying to run a powershell script during executing a test cases, but I am not quite sure what is wrong with the function I'm using.

 


function sendHttpPostRequest() {
var path_org = Project.Path;
var path = aqString.Replace(path_org, "\\","/");
var powerShellScript = path + "PS_request/POST_REQ.PS1";
var oShell = getActiveXObject("WScript.Shell");
var oExec = oShell.Exec(`powershell -file ${powerShellScript}`);
oExec.StdIn.Close(); // Close standard input before reading output
// Get PowerShell output
var strOutput = oExec.StdOut.ReadAll();
// Trim leading and trailing empty lines
strOutput = aqString.Trim(strOutput, aqString.stAll);

// Post PowerShell output to the test log line by line
aqString.ListSeparator = "\r\n";
for (var i = 0; i < aqString.GetListLength(strOutput); i++)
{
Log.Message(aqString.GetListItem(strOutput, i));
}
}
sendHttpPostRequest();

5 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's an example,

    function PSTest()
    {
        var oShell = getActiveXObject("WScript.Shell");
        var oExec = oShell.Exec("powershell -Command \"C:\\Temp\\HelloWorld.ps1\"");
        oExec.StdIn.Close(); // Close standard input before reading output
    
        // Get PowerShell output
        var strOutput = oExec.StdOut.ReadAll();
        // Trim leading and trailing empty lines
        strOutput = aqString.Trim(strOutput, aqString.stAll);
    
        // Post PowerShell output to the test log line by line
        aqString.ListSeparator = "\r\n";
        for (var i = 0; i < aqString.GetListLength(strOutput); i++)
        {
            Log.Message(aqString.GetListItem(strOutput, i));
        }
    }

    HelloWorld.ps1 contains just

    Write-Host "Hello World"

    Ensure your path is correct; use double backslash, and enclose the full path in quotes

  • PeterZ's avatar
    PeterZ
    New Contributor

    When I'm running the function (the one I posted) in a separate script, it works fine. 

    but when I run it during the excution of the whole test, it works but does not show any effect in the required task.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm using the latest version of TestComplete, and the output log shows,

    You can also try this,

    function PSTest()
    {
        var oExec = WshShell.Exec("powershell -Command \"C:\\Temp\\HelloWorld.ps1\"");
        while (oExec.Status == 0) {
            aqUtils.Delay(1000);
        }
        while (!oExec.StdOut.AtEndOfStream) {
            Log.Message(oExec.StdOut.ReadLine());
        }
    }

     

    Does POST_REQ.PS1 produce any output when running the script via PowerShell command?

     

    Can you insert Log.Message(oExec) beneath the line, var oExec = oShell.Exec(`powershell -file ${powerShellScript}`);

     

    What is the output?