2016-02-04 4 views
5

मैं से एक powershell स्क्रिप्ट कॉल करने के लिए कोशिश कर रहा हूँ HTML आवेदन [HTA] के रूप में:एक HTA में एक PowerShell स्क्रिप्ट का आउटपुट प्राप्त

Set WshShell = CreateObject("WScript.Shell") 

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

कहाँ test.ps1 सिर्फ लौटने प्रक्रिया गणना

return (Get-Process).Count 

मैं इस शक्तिशक्ति स्क्रिप्ट का आउटपुट प्राप्त करना चाहता हूं और फिर उसे स्थानीय चर में स्टोर या एचटीए पर प्रदर्शित करना चाहता हूं। यह कैसे किया जा सकता है ?

मैं उपयोग करने की कोशिश:

retVal.StdIn.Close() 

result = retVal.StdOut.ReadAll() 


alert(result) 

लेकिन मुद्रित परिणाम मान शून्य है।

कृपया इसे प्राप्त करने में मेरी सहायता करें।

+0

क्या आप इसे पढ़ने में आसान बनाने के लिए कोड में कुछ स्वरूपण जोड़ सकते हैं? – tomRedox

+0

संभावित डुप्लिकेट [चुपचाप VbScript के साथ कमांड लाइन चलाना और आउटपुट प्राप्त करना?] (Http://stackoverflow.com/questions/5690134/running-command-line-silently-with-vbscript-and-getting-output) – Marc

+0

@Marc मैंने ऊपर उल्लिखित समाधान की कोशिश की, फाइल को आउटपुट लिखना। लेकिन फ़ाइल सामग्री फिर खाली है। पावरहेल Powerhell कंसोल पर आउटपुट (प्रक्रियाओं की संख्या) को ठीक से प्रिंट करता है। एचटीएमएल एप्लिकेशन इस जानकारी को पुनः प्राप्त करने में असमर्थ है। – 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

यह आपको दिखा कैसे एक पाठ क्षेत्र में उत्पादन परिणाम प्राप्त करने के एक और उदाहरण है जबकि आप एक एचटीए के साथ एक पाउडरहेल फ़ाइल निष्पादित करते हैं!

<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> 
संबंधित मुद्दे