Hi,
I am new to creating batch files and to powershell.
Basically I found some code and adapted it to allow me to create a scheduled task on staff machines. They run the batch file and it creates an onlogon task, which runs another batch file thats on a shared drive, which updates a database I 've made.
I tried the file on my machine which is running windows 7 enterprise x86 and it worked perfectly. On testing on the vista x86 machines everyone else is on I noticed a task wasn't created. So I put a pause in the batch and found this error:
'PowerShell' is not recognized as an internal or external command, operable program or batch file.
I have no idea what is going wrong, on my pc I get a pop up from powershell that requires me to click 'yes' to grant it permission to make changes. As this error occurs on vista I don't get that and so the permission isn't given to create a scheduled task.
Please see the code below and thanks in advance for the help.
Andy
I am new to creating batch files and to powershell.
Basically I found some code and adapted it to allow me to create a scheduled task on staff machines. They run the batch file and it creates an onlogon task, which runs another batch file thats on a shared drive, which updates a database I 've made.
I tried the file on my machine which is running windows 7 enterprise x86 and it worked perfectly. On testing on the vista x86 machines everyone else is on I noticed a task wasn't created. So I put a pause in the batch and found this error:
'PowerShell' is not recognized as an internal or external command, operable program or batch file.
I have no idea what is going wrong, on my pc I get a pop up from powershell that requires me to click 'yes' to grant it permission to make changes. As this error occurs on vista I don't get that and so the permission isn't given to create a scheduled task.
Please see the code below and thanks in advance for the help.
Andy
Code:
@ECHO off
ECHO "Proceeding..."
REM Delete variables, may be cached
SET "TaskName="
SET "Task="
REM Set variables
SET TaskName=EnquiryUpdater
REM Following task will be executed every hour
SET Task=\\OPACAFP01.LOCAL\personnel$\temp\AMcInst\EnquiryInstall.bat
REM Determine if windows xp
VER | find "XP" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_xp
REM Determine if windows Vista/Win7
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO SET Version=%%i
DEL /F %TEMP%\osname.txt
ECHO %Version% | find "Windows 7" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_7
ECHO %Version% | find "Windows Vista" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_vista
:ver_xp
:Run Windows XP specific commands here.
REM Delete variable, may be cached
SET "Result="
REM WinXP doesn't support TN for schtasks /query
FOR /F "delims=, tokens=2" %%R IN ('schtasks /query /fo csv /v ^| findstr /L /C:"%TaskName%"') DO SET Result=%%R
IF (%Result%)==() SET Result="-1"
IF "%TaskName%" == %Result% (
REM Delete Task if it exists
SCHTASKS /Delete /TN "%TaskName%" /F
)
REM Then Create daily running one
SCHTASKS /Create /TN "%TaskName%" /TR "%task%" /SC ONLOGON
GOTO exit
:ver_vista
:Run Windows Vista specific commands here.
GOTO Elevation
:ver_7
:Run Windows 7 specific commands here.
GOTO Elevation
:Elevation
REM Don't forget escape double quotes for CMD argument that you will pass to powershell
PushD "%~dp0"
IF EXIST "%~0.ELEVATED" DEL /F "%~0.ELEVATED"
SET Argument=SCHTASKS /Create /F /TN \"%TaskName%\" /TR \"%task%\" /SC ONLOGON
SET ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('Cmd.exe', '/C %Argument%', '', 'runas')
ECHO %ELEVATED_CMD% >> "%~0.ELEVATED"
CALL %ELEVATED_CMD%
DEL /F "%~0.ELEVATED"
PAUSE
PopD
GOTO exit
:exit
ECHO "Done!"
EXIT
Last edited: