HTA

2016-02-04 8 views
5

中のPowerShellスクリプトの出力を取得し、私のようにHTMLアプリケーション[HTA]からPowerShellスクリプトを呼び出すようにしようとしています:HTA

test.ps1はちょうど

return (Get-Process).Count 
を返すプロセス数を持っている
Set WshShell = CreateObject("WScript.Shell") 

Set retVal = WshShell.Exec("powershell.exe C:\PS_Scripts\test.ps1") 

私はこのPowerShellスクリプトの出力を取得し、ローカル変数に格納したりHTAに表示します。これはどうすればできますか?

私が使用してみました:

retVal.StdIn.Close() 

result = retVal.StdOut.ReadAll() 


alert(result) 

をしかし、印刷結果の値はnullです。

これを達成するためにどのように私を助けてください。

+0

これを読みやすくするために、コードにいくつかの書式を追加できますか? – tomRedox

+0

[VbScriptでサイレントモードでコマンドラインを実行し、出力を出力しますか?](http://stackoverflow.com/questions/5690134/running-command-line-silently-with-vbscript-and-getting-output) – Marc

+0

@Marc私は上記の解決策も試み、出力をファイルに書き出しました。しかし、ファイルの内容は再び空です。 powershellは、出力(プロセス数)をpowershellコンソールに適切に表示します。 HTMLアプリケーションはこの情報を取得できません。 – shanky

答えて

4

これは私の作品:

test.ps1:

(Get-Process).Count | Out-File c:\temp\output.txt -Encoding ascii 

がtest.hta:

<head> 
<title>HTA Test</title> 
<HTA:APPLICATION 
    APPLICATIONNAME="HTA Test" 
    SCROLL="yes" 
    SINGLEINSTANCE="yes" 
    WINDOWSTATE="maximize" 
</head> 
<script language="VBScript"> 
    Sub TestSub 

     Set WshShell = CreateObject("WScript.Shell") 
     return = WshShell.Run("powershell.exe -ExecutionPolicy Unrestricted -File test.ps1", 0, true) 
     Set fso = CreateObject("Scripting.FileSystemObject") 
     Set file = fso.OpenTextFile("c:\temp\output.txt", 1) 
     text = file.ReadAll  
     alert(text)  
     file.Close  
    End Sub 
</script> 
<body> 
    <input type="button" value="Run Script" name="run_button" onClick="TestSub"><p> 
</body> 
+1

ありがとう、これは魅力的なものでした:-) – shanky

3

これは、テキストエリアに出力結果を取得する方法をあなたに示す別の例でありますあなたがHTAでpowhershellファイルを実行している間!

<html> 
<head> 
<title>Execution a powershell file with HTA by Hackoo</title> 
<HTA:APPLICATION 
    APPLICATIONNAME="Execution a powershell file with HTA by Hackoo" 
    SCROLL="yes" 
    SINGLEINSTANCE="yes" 
    WINDOWSTATE="maximize" 
    ICON="Winver.exe" 
    SCROLL="no" 
/> 
<script language="VBScript"> 
Option Explicit 
Sub Run_PS_Script() 
    ExampleOutput.value = "" 
    btnClick.disabled = True 
    document.body.style.cursor = "wait" 
    btnClick.style.cursor = "wait" 
    Dim WshShell,Command,PSFile,return,fso,file,text,Temp 
    Set WshShell = CreateObject("WScript.Shell") 
    Temp = WshShell.ExpandEnvironmentStrings("%Temp%") 
    Command = "cmd /c echo Get-WmiObject Win32_Process ^| select ProcessID,ProcessName,Handle,commandline,ExecutablePath ^| Out-File %temp%\output.txt -Encoding ascii > %temp%\process.ps1" 
    PSFile = WshShell.Run(Command,0,True) 
    return = WshShell.Run("powershell.exe -ExecutionPolicy Unrestricted -File %temp%\process.ps1", 0, true) 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set file = fso.OpenTextFile(Temp &"\output.txt", 1) 
    text = file.ReadAll 
    ExampleOutput.Value=text 
    file.Close 
    document.body.style.cursor = "default" 
    btnClick.style.cursor = "default" 
    btnClick.disabled = False 
End Sub 
</script> 
</head> 
<body bgcolor="123456"> 
<textarea id="ExampleOutput" style="width:100%" rows="37"></textarea> 
<br> 
<center><input type="button" name="btnClick" value="Run powershell script file " onclick="Run_PS_Script()"></center> 
</body> 
</html> 
関連する問題