Solved Newbie Powershell help in batch file

mcinnes01

New Member
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

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:

My Computer

Hi,

Try this "Version" from your 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

I have added the "" on the %Temp% sections to get it to work on my machine, the user account name has a "&" in it and then the code faults out.

So the only other thought for now is: -

Has Powershell been "Activated" on the Vista windows machines?
 

Attachments

  • Windows Features.jpg
    Windows Features.jpg
    51.5 KB · Views: 30

My Computer

System One

  • Manufacturer/Model
    HP-Pavilion m9280.uk-a
    CPU
    2.30 gigahertz AMD Phenom 9600 Quad-Core
    Motherboard
    ASUSTek Computer INC. NARRA3 3.02
    Memory
    3582 Megabytes Usable Installed Memory (4 Gig)
    Graphics Card(s)
    ASUS NVIDIA Geforce GTS450
    Sound Card
    Realtek High Definition 7.1 Audio (HP drivers)
    Monitor(s) Displays
    HP w2408 24.0" (Dual monitor)
    Screen Resolution
    1920 * 1200, 1920 * 1200
    Hard Drives
    3*500 Gigabytes Usable Hard Drive Capacity
    Plus 2x USB (160Gig each) external HDD
    BluRay & DVD Weiters
    HL-DT-ST BD-RE GGW-H20L SCSI CdRom (Bluray RW) Device
    AlViDrv BDDVDROM SCSI CdRom (Blueray) Device
    TSSTcorp CDDVDW TS-H653N SCSI CdRom
    Internet Speed
    40 Meg
Thanks for the brilliant reply! I just wonder if there is a way I can activate the powershell settings in the batch file first? This is for non technical HR staff and I've gone with this idea as there is a lot of them and something that doesn't require following instructions is the only real way forward.
 

My Computer

My Computer

System One

  • Manufacturer/Model
    HP-Pavilion m9280.uk-a
    CPU
    2.30 gigahertz AMD Phenom 9600 Quad-Core
    Motherboard
    ASUSTek Computer INC. NARRA3 3.02
    Memory
    3582 Megabytes Usable Installed Memory (4 Gig)
    Graphics Card(s)
    ASUS NVIDIA Geforce GTS450
    Sound Card
    Realtek High Definition 7.1 Audio (HP drivers)
    Monitor(s) Displays
    HP w2408 24.0" (Dual monitor)
    Screen Resolution
    1920 * 1200, 1920 * 1200
    Hard Drives
    3*500 Gigabytes Usable Hard Drive Capacity
    Plus 2x USB (160Gig each) external HDD
    BluRay & DVD Weiters
    HL-DT-ST BD-RE GGW-H20L SCSI CdRom (Bluray RW) Device
    AlViDrv BDDVDROM SCSI CdRom (Blueray) Device
    TSSTcorp CDDVDW TS-H653N SCSI CdRom
    Internet Speed
    40 Meg
Hi Andy,

Glad to be of help & to see you are sorted.

Thank you for the rep & if you need any more info/ assistance, please ask.
 

My Computer

System One

  • Manufacturer/Model
    HP-Pavilion m9280.uk-a
    CPU
    2.30 gigahertz AMD Phenom 9600 Quad-Core
    Motherboard
    ASUSTek Computer INC. NARRA3 3.02
    Memory
    3582 Megabytes Usable Installed Memory (4 Gig)
    Graphics Card(s)
    ASUS NVIDIA Geforce GTS450
    Sound Card
    Realtek High Definition 7.1 Audio (HP drivers)
    Monitor(s) Displays
    HP w2408 24.0" (Dual monitor)
    Screen Resolution
    1920 * 1200, 1920 * 1200
    Hard Drives
    3*500 Gigabytes Usable Hard Drive Capacity
    Plus 2x USB (160Gig each) external HDD
    BluRay & DVD Weiters
    HL-DT-ST BD-RE GGW-H20L SCSI CdRom (Bluray RW) Device
    AlViDrv BDDVDROM SCSI CdRom (Blueray) Device
    TSSTcorp CDDVDW TS-H653N SCSI CdRom
    Internet Speed
    40 Meg
Back
Top