BAT file - open popup - batch-file

I've written a little .bat file containing the following commands:
#echo off
Slmgr /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr /ato edkk4b-3sf6-j3h4-i5i5-d04gd453dgr3
Then i've packaged it in a .exe with a tool.
It works great but at the end of the execution two popup are displayed...
Can i remove the first popup and let display only the second?

As I see it, there are 2 options:
Run slmgr with cscript so all of the output goes to the console.
#echo off
setlocal
cscript //nologo c:\windows\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
cscript //nologo c:\windows\system32\slmgr.vbs /ato edkk4b-3sf6-j3h4-i5i5-d04gd453dgr3
Call :MsgBox "Done!" "VBOkOnly" "Process Complete"
exit /b
:MsgBox prompt type title
::Function by MC ND
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%
Edit 1 copy of slmgr.vbs and put it on a network share and run it from there for all PC's.

Related

Launch VBS MsgBox but continue the script

I have tried multiple different way to make the below possible but cannot find a way to do it. I have a ROBOCOPY script that outputs a VBS messagebox depending on the exit code. However, I want the batch script to continue running and exit even after the messagebox appears. As of right now, I have to click Ok for the VBS message box to close and then the command line closes.
Here is my code:
#ECHO OFF
::Delete DB File
CD "C:\Program Files (x86)\SAP\Agentry Client"
DEL "Agentry.db"
CLS
::Copy new DB file from Fil003
2>nul PUSHD "\\sampleserver\123\123" >nul
ROBOCOPY /ZB /IS /IT /R:3 "\\sampleserver\123\123" "C:\Program Files (x86)\SP\AClient" "AClient.db" /log:"C:\Program Files (x86)\SP\AClient\Acleint.txt" /tee
if %ERRORLEVEL% EQU 16 GOTO ER16
if %ERRORLEVEL% EQU 1 GOTO ER1
if %ERRORLEVEL% EQU 0 GOTO ER0
:ER0
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "NoCopy.vbs"
TIMEOUT /T 3
POPD
exit
:ER1
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start "" cmd /c cscript "Success.vbs"
TIMEOUT /T 3
POPD
exit
:ER16
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "FatalError.vbs"
TIMEOUT /T 3
POPD
exit
You can use mshta in vbs rather than msgbox
Function mshta(ByVal MessageText, ByVal Title, ByVal PauseTimeSeconds)
Set WScriptShell = CreateObject("WScript.Shell")
ConfigString = "mshta.exe vbscript:close(CreateObject(""WScript.Shell"")." & "Popup(""" & MessageText & """," & PauseTimeSeconds & ",""" & Title & """))"
WScriptShell.Run ConfigString
End Function
'PauseTimeSeconds' will close the message, or you can set it to 0 for it to remain open until user clicks ok.

Convert batch script into base64 / non-readable format

Years ago I saw some batch scripts that contain nothing relevant at a fist look, but with a lot of content. The content was base64 encoded, and when you run the scripts, they are just running the commands that are encoded there.
Something like
SET mypath=%~dp0
echo "%mypath%
will look similar with
some_base_64_decoding_function(
U0VUIG15cGF0aD0lfmRwMA0KIGVjaG8gIiVteXBhdGgl
)
I don't really remember how those file were, and I can't find anything relevant on the Internet, except this article: Convert PowerShell script into non-readable format
but this only refeer to PowerShell scripts, and I need it for .bat scripts.
Anyone to give me a hint?
This is a sample batch file for encoding files in Base 64 with Certutil utility.
How to use it?
Just save this code on your notepad or on notepad++ or on any text editor as :
Certutil_B64_Encoding_Files.bat and drag and drop any file over it to be encoded
#echo off
Title Encoding files with CERTUTIL utility by Hackoo 2017
color 0A & Mode 83,3
If "%~1"=="" (
color 0C & Mode 80,3
echo(
echo You must drag and drop a file over this batch script to be encoded !
Timeout /T 5 /nobreak>nul & exit /b
)
#for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
echo CertUtil.exe not found.
pause
exit /b
)
set "TempFile=%Temp%\Temp_b64
set "OutputFile=%~nx1_encoded%~x0"
If exist "%OutputFile%" Del "%OutputFile%" >nul 2>&1
echo(
echo Please wait a while ... Encoding "%~nx1" is in progress ...
certutil.exe -f -encode "%~1" "%TempFile%" >nul 2>&1
(
echo #echo off
echo CERTUTIL -f -decode "%%~f0" "%%Temp%%\%~nx1" ^>nul 2^>^&1
echo Start "%~n1" "%%Temp%%\%~nx1"
echo Exit
)>> "%OutputFile%"
copy "%OutputFile%" /b + "%TempFile%" /b >nul 2>&1
If exist "%TempFile%" Del "%TempFile%" >nul 2>&1
Timeout /T 2 /NoBreak>nul & exit
Encoded HTA Example: CommandLine.hta_encoded.bat
This is a result of an encoded output of a HTA file of mine named as CommandLine.hta_encoded.bat
So, you should copy and paste this code as CommandLine.hta_encoded.bat and execute it by double click. And you will get something like this :
Encoded VBS Example : DJBuzzRadio.vbs_encoded.bat
#echo off
CERTUTIL -f -decode "%~f0" "%Temp%\DJBuzzRadio.vbs" >nul 2>&1
Start "DJBuzzRadio" "%Temp%\DJBuzzRadio.vbs"
Exit
-----BEGIN CERTIFICATE-----
UGxheSAiaHR0cDovL3d3dy5jaG9jcmFkaW9zLmNoL2RqYnV6enJhZGlvX3dpbmRv
d3MubXAzLmFzeCINClN1YiBQbGF5KFVSTCkNCiAgIERpbSBTb3VuZA0KICAgU2V0
IFNvdW5kID0gQ3JlYXRlT2JqZWN0KCJXTVBsYXllci5PQ1giKQ0KICAgU291bmQu
VVJMID0gVVJMDQogICBTb3VuZC5zZXR0aW5ncy52b2x1bWUgPSAxMDANCiAgIFNv
dW5kLkNvbnRyb2xzLnBsYXkNCiAgIGRvIHdoaWxlIFNvdW5kLmN1cnJlbnRtZWRp
YS5kdXJhdGlvbiA9IDANCiAgICAgICB3c2NyaXB0LnNsZWVwIDEwMA0KICAgbG9v
cA0KICAgd3NjcmlwdC5zbGVlcCAoaW50KFNvdW5kLmN1cnJlbnRtZWRpYS5kdXJh
dGlvbikrMSkqMTAwMA0KRW5kIFN1Yg0K
-----END CERTIFICATE-----
MACRO-b64decode.bat
#echo off
SETLOCAL DISABLEDELAYEDEXPANSION
::DEFINITIONS
( set LF=^
%= EMPTY =%
)
set ^"NL=^^^%LF%%LF%^%LF%%LF%^^"
set $MACRO.ForEntireLine=^^^^^^^"eol^^^^=^^^^^^^%LF%%LF%^%LF%%LF%^^^%LF%%LF%^%LF%%LF%^^^^ delims^^^^=^^^^^^^"
::MACRO
ENDLOCAL &^
set $MACRO.b64exec=FOR %%? in (args main) do if "%%?" == "main" (%NL%
FOR %%a in (!args!) do (%NL%
^>encoded.txt echo %%a%NL%
^>nul certutil -decodehex encoded.txt decoded.txt 1%NL%
FOR /F %$MACRO.ForEntireLine% %%x in (decoded.txt) do %%x%NL%
del /F /Q encoded.txt decoded.txt%NL%
)%NL%
) ELSE SETLOCAL ENABLEDELAYEDEXPANSION ^& set args=
To call the macro inside your batch file:
call MACRO-b64decode.bat
%$MACRO.b64exec% ZWNobyBoZWxsbywgd29ybGReIQ==
which will output:
hello, world!
Note: DELAYEDEXPANSION is enabled

Inconsistent sendkey with batch file

I'mt trying to create a batch file that wil be called by another program but can not get the sendkey to work properly. It calls Zune and opens it but does not send the ctrl+p to start the play. If I have Zune open and run the batch file repetitively it will eventually start playing but I may have to run the batch 10 time in a row. Not sure why that is. Ideas?
#echo off
setlocal
start zune.exe
call :SendCtrlP "Name in Windowtitle"
:SendCtrlP <app>
setlocal
set vbs=%Temp%\_.vbs
>%vbs% echo set s = CreateObject("Wscript.Shell")
>>%vbs% echo s.appactivate "%~1"
>>%vbs% echo s.sendkeys "^p"
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
exit /b

How to make shortcut to folder with Batch

How do i make a folder shortcut wiht Batch Commando Prompt?
i have tried this:
copy "C:\Windows" "C:\Users\%username%\Desktop\Windows.ink"
mklink "C:\Windows" "C:\Users\%username%\Desktop"
#echo off
call :createDesktopShortcut "%~1" "%~2"
exit /b
:createDesktopShortcut targetOfShortcut nameOfShortcut
if not exist "%~f1" goto :eof
setlocal & set "tempFile=%temp%\%~nx0.vbs.tmp" & set "name=%~2" & if not defined name set "name=%~n1"
echo(Set S=WScript.CreateObject("Wscript.Shell"):With S.Createshortcut(S.SpecialFolders("Desktop")+"\%~2.lnk"):.TargetPath="%~f1":.Save:End With>"%tempFile%"
cscript //nologo //e:vbscript "%tempFile%">nul & del /f /q "%tempFile%" >nul 2>nul
endlocal & goto :eof
Save as createDesktopShortcut.cmd and call it as
createDesktopShortcut.cmd "%windir%" "Win directory"

Can i pop a message when user clicks on close button in batch script?

I want to know How to Pop a Message When User clicks the close button in batch file
I'm looking for a method to pop a message box when user clicks close button in batch.
Like When Users clicks the close button it will pop up a message saying " You just closed " Using VBScript or any other....
I am only posting this because you are young and willing to learn.
When you close the cmd window, it stops all execution within that command, nothing triggers any further events. You therefore need to monitor that specific instance with another script. As an example, create a batch file called mytest.cmd and enter the following code.
#echo off
title Monitor_This
pause
create another batch file and let's call it monitoring.cmd and enter the following batch/vbscript code:
<!-- :
#echo off
:start
tasklist /FI "windowtitle eq Monitor_This" | findstr "cmd.exe">nul 2>&1
if %errorlevel% equ 1 cscript //nologo "%~f0?.wsf" & exit /b
timeout 1 >nul 2>&1
goto :start
exit /b
-->
<job><script language="VBScript">
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.Popup "You closed The Window",, "My Popup"
</script></job>
Now run both the scripts. Once they are running, you can close the window that has a title off Monitor_This as in the below image:
Which upon closure will then return:
Here is a command line interface only method:
#echo off
if "%~1"=="startchk" goto chk
start "" cmd /c "%~f0 startchk"
rem Rest of code here
goto :eof
:chk
set count=0
for /f "tokens=1 delims= " %%a in ('tasklist ^| findstr /r "[c][m][d][.][e][x][e]"') do set /a count += 1
:chk2
set new=0
for /f "tokens=1 delims= " %%a in ('tasklist ^| findstr /r "[c][m][d][.][e][x][e]"') do set /a new += 1
if %new% lss %count% goto over
goto chk2
:over
msg * "You have closed cmd"
Goto :eof

Resources