Batch script with powershell command (run from task scheduler) - powershell

I am using a batch script with a powershell command.
cutout:
FOR /F "usebackq delims=" %%v IN (`powershell -noprofile -executionpolicy bypass "& { (Get-Date).AddDays(-1).DayOfYear }"`) DO set "DayOfYear=00%%v"
set DayOfYear=%DayOfYear:~-3%
set rinex_dest=E:\02_RINEX\%GNSSWEEK%\%DayOfYear%\
set rinex_dest_extern=E:\02_RINEX\%GNSSWEEK%\%DayOfYear%\extern\
IF NOT EXIST (mkdir %rinex_dest%)
IF NOT EXIST (mkdir %rinex_dest_extern%)
CD %rinex_dest%
set temp_unzip=%rinex_dest%temp_unzip\
set temp_nw=%rinex_dest%temp_nw\
set temp_extern=%rinex_dest%temp_extern\
mkdir %temp_unzip%
mkdir %temp_nw%
mkdir %temp_extern%
7z.exe e Y:\data\%Year%\%Month%\%Day%\*.zip -o%temp_unzip% *_MO.rnx -r
FOR %%i IN (%NWStationen%) DO MOVE %temp_unzip%%%i00DEU_S_%Year%%DayOfYear%*_MO.rnx %temp_nw%
FOR %%h IN (%externeStationen%) DO MOVE %temp_unzip%%%h_S_%Year%%DayOfYear%*_MO.rnx %temp_extern%
rmdir /Q /S %temp_unzip%
This works fine for me, if i run the the batchfile.
But now i try to implement the batchfile in the task scheduler (on Win Server 2019).
The batchfile runs, but the powershell command doesn`t work.
The Action in task scheduler is:
"Start program" C:\Windows\System32\CMD.exe /c"PatchtoScript\Script.bat"
Any suggestions to solve the problem?
Thank You!

Related

Batch knowing its invoker: cmd.exe or powershell?

I wonder how to check if a batch file is executed by cmd.exe or instead by powershell
I discovered that the ENV variable %CmdCmdLine%is set to something in cmd.exe and should not be set inside a powershell shell.
But if I run a batch file in powershell it temporary assumes a value like C:\WINDOWS\system32\cmd.exe /c ""C:\path\to\check-interpreter.bat""
Given also the inability of batch to check IF PATTERN I cannot find a way to let my batch file be able to know which command interpreter is running.
In the below code I tried to use findstr /R to check cmd at the start of the line. Infact inside cmd.exe the line should start with cmd.exe ... while in powershell should start with the full path C:\WINDOWS\system32\cmd.exe /c ...
Here my attempt:
#ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`echo %CmdCmdLine% ^^^| findstr /R
"^cmd"`) DO (
SET var=%%F
)
ECHO var set to: %var%
REM IF [%var%]==[] ECHO var set to: %var%
IF DEFINED var (
ECHO CmdCmdLine founnd!
ECHO VAR : %var%
ECHO COMMANDLINE: %CmdCmdLine%
) ELSE (
ECHO CmdCmdLine NOT found..
)
The problem in the above code is that var is not populated at all:
# cmd.exe
C:\path\to>.\check-interpreter.bat
CmdCmdLine founnd!
COMMANDLINE: "C:\WINDOWS\system32\cmd.exe"
# powershell
PS C:\path\to> .\check-interpreter.bat
CmdCmdLine founnd!
COMMANDLINE: C:\WINDOWS\system32\cmd.exe /c ""C:\path\to\check-
interpreter.bat""

Psexec batch file foor loop error

I have problem. All working on local machine but when use psexec i get error.
This is scrap of my .bat file
psexec.exe \%pc% cmd.exe /k for /f "tokens=2 delims=:" %%g in ('certutil -verifystore My ^| findstr /i "serial"') do echo %%g
On local cmd all its work but on remote i get error:
"At this point | was unexpected"
Could you help me?
Correct part of code
(...) in ('"certutil -verifystore My | findstr /i serial"') do (...)

how do i search for a file and when found set the file path to a variable in a batch script

I am trying to create a batch file that will run and open a Powershell script to then run.
this is what i have so far
#echo off
for /r C:\folder %%a in (*) do if "%%~nxa"=="2WNRN4VMS2.txt" set p=%%~dpnxa
if defined p (
echo %p%
) else (
echo File not found
Pause
)
Powershell.exe -Command "& '%p%'"
exit
That is very simple using command DIR for searching for the file recursively in folder C:\folder and all its subfolders and command FOR for assigning the drive and path of found file to an environment variable:
#echo off
for /F "delims=" %%I in ('dir /A-D /B /S "C:\folder\2WNRN4VMS2.txt" 2^>nul') do set "FilePath=%%~dpI" & goto FoundFile
echo File not found
pause
goto :EOF
:FoundFile
Powershell.exe -Command "& '%FilePath%'"
Please note that the string assigned to environment variable FilePath ends with a backslash. Use in PowerShell command line %FilePath:~0,-1% if the path of the file should be passed without a backslash at end.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
for /?
goto /?
pause /?
set /?

Batch file to look for file from today and run another .bat otherwise run vbs and exit

I have a batch file I would like to search a folder for any xlsm file updated today.
If it finds an xlsm file from today, it then runs another bat file. If there is no updated file it should run a .vbs file (to send an email) and then exit.
Currently I have the following:
#echo off
set var=%date:~-10%
dir "*.xlsm"|find "%var%">nul&&CALL Update.bat||EXIT
I think I probably need to include some sort of IF/ELSE instead of the current method, but my skills are lacking..
EDIT:
The actual solution I've gone with, based on the answer from #Monacraft is:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
If using windows 7 you could do forfiles:
#echo off
set found=FALSE
forfiles /p "C:\...[path to folder]" /m *.xlsm /d +1 /c "cmd /c Echo Updating...&CALL Update.bat&set found=TRUE"
if /i %found%==False (
start email.vbs
) else (
Exit
)
And that should work fine
Mona
This worked for me:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
I think your code should work - some small changes here.
It will just echo the commands to the screen, for you to test.
One point to consider is that the DIR command will also match *.xls files in the short name.
#echo off
set "var=%date:~-10%"
dir "*.xlsm"|find "%var%">nul && (echo CALL Update.bat) || (echo start failemail.vbs)
pause

batch file that opens cmd prompt types string as command line

I got my batch file to open cmd prompt and change directory successfully, however what I cannot get to work is after the directory has changed I want the batch to then to enter a string as a command.
I've tried cmd /k string but that didn't seem to work, the cmd just sits at the changed directory. I've also tried:
set Opvar= echo string
%Opvar%
Again it just sits at the last changed directory. The batchisp line that's commented out is what the string is that I want to act as if I typed the whole string and pressed enter at the current directory prompt.
Here's the whole thing:
#Echo OFF
FOR /F "Tokens=*" %%# IN ('Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" ^| FIND "-1000"') DO (
FOR /F "Tokens=2,*" %%A IN ('Reg Query "%%#" /v "ProfileImagePath" ^| FIND /V "%%#"') DO (
set drive=%%B
Echo Admin SID: %%~n#
Echo Admin Folder: %%B
)
)
:Ask
echo What are you flashing? Please select one option(1,2, or 3):
echo 1. Main Board w/ 6-Station OpCon.
echo 2. Main Board w/ 12-Station Opcon.
echo 3. OpCon Board.
set /P INPUT=Type input: %=%
If "%INPUT%"=="1" goto Option1
If "%INPUT%"=="2" goto Option2
If "%INPUT%"=="3" goto Option3
::::::::::::
:Option1
echo Starting 6-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\6-STATION\*.*" "%drive%\Desktop\MainPCB6\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB6\6-STATION\Debug\"
goto End
::::::::::::
:Option2
echo Starting 12-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\12-STATION\*.*" "%drive%\Desktop\MainPCB12\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB12\12-STATION\Debug\"
goto End
::::::::::::
:Option3
echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
cmd /k "cd /d %drive%\Desktop\SWPCB\SWPCB\Debug\"
::batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0
goto End
::::::::::::
:End
echo ******
echo *****
echo ****
echo ***
echo **
echo *
echo Flash Completed! Press any key to exit...
Pause>NUL&Exit
Any help would be much appreciated.
Thank you
You are changing the working directory in a new instance of CMD (CMD /K), use PUSHD to change the working directory in your current CMD instance and POPD to return to the last used directory:
#Echo OFF
PUSHD %WINDIR%"
Echo I'm on "%CD%" Directory!
POPD
Echo I'm on "%CD%" Directory!
Pause&Exit
Change your code to this:
:Option3
echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
PUSHD "%drive%\Desktop\SWPCB\SWPCB\Debug"
batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0
POPD
goto :End
...Or if you want to start directly the command in a new instance of CMD change it to this:
CMD /k "Start /W """" "%drive%\Desktop\SWPCB\SWPCB\Debug\batchisp" -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0"
PS: Remember that you don't need to change the working dir to start an app, you can write the path followed by the process name like in my last example.