您的位置:首页 > 运维架构 > Shell

TestComplete 调用powershell 和cmd并获取屏幕输出

2017-03-22 17:28 661 查看
1.Run powershell 命令并捕获屏幕输出:

function RunPowerShell()
{
var oShell = Sys.OleObject("WScript.Shell"); // or oShell = WshShell
var oExec = oShell.Exec("powershell -command $PSVersionTable.PSVersion.Major");
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));
}
}


2.Run cmd文件并获取屏幕输出:

function StartCMD()
{
var dscliCmd = "Test.cmd";
aqString.ListSeparator = "\r\n";
if(aqFile.Exists(dscliCmd))
{
Sys.OleObject("WScript.Shell").Run("C:\\Windows\\system32\\cmd.exe");
var p = Sys.Process("cmd");
wCMD = p.Window("ConsoleWindowClass", "*");
wCMD.Keys(dscliCmd +" [Enter]");
}
else
ReportError("Can not find " + dscliCmd);
wCMD.Close()
}


获取输出:

txt = wCMD.wText
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐