I am trying to write a batch file which executes different powershell file on a click on the button on the interface.
<!-- :: Batch section
@echo off
SET "ThisScriptsDirectory=%~dp0"
SET "PowerShellScriptPathAdd=%ThisScriptsDirectory%powershelladd.ps1"
SET "PowerShellScriptPathRemove=%ThisScriptsDirectory%powershellremove.ps1"
#PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process
PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPathAdd%""'}"
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick=PowerShell -NoProfile -ExecutionPolicy Bypass -Command
"& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy
Bypass -File ""%PowerShellScriptPathAdd%""'}">Add User</button>
<button onclick=PowerShell -NoProfile -ExecutionPolicy Bypass -Command
"& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy
Bypass -File ""%PowerShellScriptPathRemove%""' -Verb RunAs}">Remove
User</button>
<button onclick="closeHTA(3);">Exit</button>
</BODY>
</HTML>
I tried to run the code without the click of a button and it works fine. I want it to execute certain file when I press different buttons.
You should execute the PowerShell code in the Batch file . You can not directly execute PowerShell (nor a Batch file, for that matter) from a Web/HTA page.
In order to do so, you need to follow the advice given at this answer .
<!-- :: Batch section
@echo off
setlocal
rem Initialize Batch side interface the first time
if "%~1" equ "interface" goto :interface
rem Empty pipe file
cd . > pipeFile.txt
echo Select an option:
rem Start HTA-Batch interface
mshta.exe "%~F0" >> pipeFile.txt | "%~F0" interface < pipeFile.txt
PAUSE
del pipeFile.txt
goto :EOF
:interface
set "HTAreply="
set /P "HTAreply="
if "%HTAreply%" equ "" goto interface
if "%HTAreply%" == "1" PowerShell 'Add user here...'
if "%HTAreply%" == "2" PowerShell 'Remove user here...'
if not "%HTAreply%" == "3" goto interface
echo End of HTA window
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
var fso = new ActiveXObject("Scripting.FileSystemObject");
function replyHTA(reply){
fso.GetStandardStream(1).WriteLine(reply);
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="replyHTA(1);">Add User</button>
<button onclick="replyHTA(2);">Remove User</button>
<button onclick="replyHTA(3);window.close();">Exit</button>
</BODY>
</HTML>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.