N
N
Nikita2021-07-31 16:15:25
PowerShell
Nikita, 2021-07-31 16:15:25

How to display the name of a running batch file containing a PowerShell script on the command line?

There is a batch file open_website.bat that runs a PowerShell script every time Windows starts that opens certain web pages in the Edge browser to receive daily bonuses and closes the browser after a certain time.

How, upon completion of this script, to display a message on the command line that this script has been successfully completed, specifying the name of this particular batch file?

The code inside the batch file is as follows:

@echo off
@powershell.exe -ExecutionPolicy Bypass -Command "$_=((Get-Content \"%~f0\") -join \"`n\");iex $_.Substring($_.IndexOf(\"goto :\"+\"EOF\")+9)"
@goto :EOF
Start-Process microsoft-edge:https://yoomoney.ru/moneylandia/lootboxes
Start-Sleep 5
Get-Process -Name "*msedge*" | Stop-Process
echo %~nx0 completed!
pause


The echo %~nx0 command prints only the text "%~nx0", instead of displaying the contents of the given variable:
61054b564e058733399065.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sergey, 2021-07-31
@volniol

example

@echo off
REM для https://qna.habr.com/q/1027240
set C=%~nx0
if NOT "%DEBUG%" equ "" echo Running with DEBUG set
REM using two environment parameters: C and DEBUG
REM NOTE:  passing arguments appears tricky when
REM powershell run with command built inline as string
@powershell.exe -ExecutionPolicy Bypass -Command "$debug=$env:DEBUG;$s=(get-content \"%~f0\") -join \"`n\"; $s = $s.substring($s.IndexOf(\"goto :\"+\"EOF\")+9);if ($debug -ne $null){write-output (\"Running:`n{0}\" -f$s);} invoke-expression -command $s"
@goto :EOF
# powershell code



start-sleep 5
write-output ( 'Calling script: {0}' -f $env:C)
pause

exit 0

we execute:
c:\Users\sergueik\Desktop>set debug=1

c:\Users\sergueik\Desktop>show_caller.cmd
Running with DEBUG set
Running:


Start-Sleep 5
write-output ( 'Calling script: {0}' -f $env:C)
pause
exit 0

Calling script: show_caller.cmd
Press Enter to continue...:

W
wisgest, 2021-08-01
@wisgest

Your batch file contains PowerShell code, which is run by the command on the 2nd line, and after the execution of which is the command to complete the batch file on the 3rd line. By whom the last two lines are performed, I do not quite understand. You can try to move them up and insert them before the 3rd one.
But one could do without PowerShell at all, using the command startand utilities tasklist, taskkilland timeoutsomething like this:

@start  microsoft-edge:https://yoomoney.ru/moneylandia/lootboxes
@timeout /t 5 /nobreak>nul
@for /f %%1 in ('tasklist^|find /i "msedge"') do @taskkill /im %%1 /f
@echo %~nx0 completed!
@pause

M
MaxKozlov, 2021-08-01
@MaxKozlov

Your mistake is that the code "echo %~nx0 completed!" executes powershell, which does not know what %~nx0 is (echo is an alias for write-output) So
, or as Sergey Kuzmin suggested, or such an edit

@echo off
@powershell.exe -ExecutionPolicy Bypass -Command "$fn = \"%~f0\"; $_=((Get-Content $fn) -join \"`n\");iex $_.Substring($_.IndexOf(\"goto :\"+\"EOF\")+9)"
@goto :EOF
Start-Process microsoft-edge:https://yoomoney.ru/moneylandia/lootboxes
Start-Sleep 5
Get-Process -Name "*msedge*" | Stop-Process
echo "$fn completed!"
pause

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question