Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 02/02/2013 in all areas

  1. NOTE: I wrote this before I saw the last post by myselfidem. I made notations accordingly, but if I missed something please forgive me. Geej & myselfidem, As I mentioned previously, I've got some issues with "Everything_Schedule.bat" as included in the file you last posted Geej. (NOTE: You caught some of the errors myselfidem, but not all. ) It simply does not work correctly. It looks as if someone cut-and-pasted from a couple of different sources, and not very well. For reference, you can check out the original script http://myunster.com/blog/server-administration/30.html#comment16'>http://myunster.com/blog/server-administration/30.html#comment16 and compare that to the code as I extracted it from EverythingSearchEngine_1.3.0.632bSi.exe: Original from http://myunster.com/blog/server-administration/30.html#comment16'>http://myunster.com/blog/server-administration/30.html#comment16: ::*******************************************:::: schedule.bat :::: Set variables TaskName, Task and go ahed :::: http://myunster.com ::::*******************************************::@ECHO offECHO "Proceeding..." REM Delete variables, may be cachedSET "TaskName="SET "Task=" REM Set variablesSET TaskName=DummyTaskNameREM Following task will be executed every hourSET Task=C:\www.domain.dev\usr\local\php5\php.exe C:\www.domain.dev\cron.php REM Determine if windows xpVER | find "XP" > NULIF %ERRORLEVEL% == 0 GOTO ver_xp REM Determine if windows Vista/Win7systeminfo | find "OS Name" > %TEMP%\osname.txtFOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO SET Version=%%iDEL /F %TEMP%\osname.txt ECHO %Version% | find "Windows 7" > NULIF %ERRORLEVEL% == 0 GOTO ver_7ECHO %Version% | find "Windows Vista" > NULIF %ERRORLEVEL% == 0 GOTO ver_vista :ver_xp:Run Windows XP specific commands here.REM Delete variable, may be cachedSET "Result="REM WinXP doesn't support TN for schtasks /queryFOR /F "delims=, tokens=2" %%R IN ('schtasks /query /fo csv /v ^| findstr /L /C:"%TaskName%"') DO SET Result=%%RIF (%Result%)==() SET Result="-1"IF "%TaskName%" == %Result% (REM Delete Task if it exists SCHTASKS /Delete /TN "%TaskName%" /F)REM Then Create hourly running oneSCHTASKS /Create /TN "%TaskName%" /TR "%Task%" /SC HOURLY /RU SYSTEMGOTO exit :ver_vista:Run Windows Vista specific commands here.GOTO Elevation :ver_7:Run Windows 7 specific commands here.GOTO Elevation :ElevationREM Don't forget escape double quotes for CMD argument that you will pass to powershellPushD "%~dp0"IF EXIST "%~0.ELEVATED" DEL /F "%~0.ELEVATED"SET Argument=SCHTASKS /Create /F /TN \"%TaskName%\" /TR \"%Task%\" /SC HOURLY /RU SYSTEMSET 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"PopDGOTO exit :exitECHO "Done!"EXIT From EverythingSearchEngine_1.3.0.632bSi.exe: ::*******************************************:::: Everything_Schedule.bat :::: Set variables TaskName, Task and go ahead :::: http://myunster.com ::::*******************************************::@ECHO offECHO "Proceeding..." >NUL REM Delete variables, may be cachedSET "TaskName="SET "Task="REM Set variablesSET TaskName=EverythingREM Following task will be executed on LogonIF EXIST "%SystemRoot%\SysWOW64\cmd.exe" GOTO :X64:X64SET Task=\"%%ProgramFiles(x86)%%\Everything\Everything.exe\" -startup IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" GOTO :x86:x86SET Task=\"%%ProgramFiles%%\Everything\Everything.exe\" -startupREM Determine if windows xpVER | find "XP" > NULIF %ERRORLEVEL% == 0 GOTO ver_xpREM Determine if windows Vista/Win7systeminfo | find "OS Name" > %TEMP%\osname.txtFOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO SET Version=%%iDEL /F %TEMP%\osname.txtECHO %Version% | find "Windows 7" > NULIF %ERRORLEVEL% == 0 GOTO ver_7ECHO %Version% | find "Windows Vista" > NULIF %ERRORLEVEL% == 0 GOTO ver_vista:ver_xp:Run Windows XP specific commands here.REM Delete variable, may be cachedSET "Result="REM WinXP doesn't support TN for schtasks /queryFOR /F "delims=, tokens=2" %%R IN ('schtasks /query /fo csv /v ^| findstr /L /C:"%TaskName%"') DO SET Result=%%RIF (%Result%)==() SET Result="-1"IF "%TaskName%" == %Result% (REM Delete Task if it exists SCHTASKS /Delete /TN "%TaskName%" /F)REM Then Create hourly running oneSCHTASKS /Create /TN "%TaskName%" /TR "%Task%" /SC ONLOGON /RL HIGHESTGOTO exit:ver_vista:Run Windows Vista specific commands here.GOTO Elevation:ver_7:Run Windows 7 specific commands here.GOTO Elevation:ElevationREM Don't forget escape double quotes for CMD argument that you will pass to powershellPushD "%~dp0"IF EXIST "%~0.ELEVATED" DEL /F "%~0.ELEVATED"SET Argument=SCHTASKS /Create /F /TN \"%TaskName%\" /TR \"%Task%\" /SC ONLOGON /RL HIGHESTSET 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"PopDGOTO exit:exitECHO "Done!"EXIT I'll list the problems I see in order. -- 1) REM Following task will be executed on LogonIF EXIST "%SystemRoot%\SysWOW64\cmd.exe" GOTO :X64:X64SET Task=\"%%ProgramFiles(x86)%%\Everything\Everything.exe\" -startup IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" GOTO :x86:x86SET Task=\"%%ProgramFiles%%\Everything\Everything.exe\" -startup[NOTE: You caught this myselfidem, but I'm afraid your correction is not quite right.] This does not work correctly. The IF statements do not do anything at all. The last SET Task statement will get executed every time no matter what. So Task will be equal to -- \"%%ProgramFiles%%\Everything\Everything.exe\" -startup -- for both x86 and x64 systems. Also, the test for x86/x64, IF it was properly executed, will probably work OK in most situations, but it is not as robust a test as it could be. (See here - http://www.ryanvm.net/forum/viewtopic.php?t=9672'>http://www.ryanvm.net/forum/viewtopic.php?t=9672 - for a full discussion.) That section of code could be rewritten as: SET Task=\"%%ProgramFiles(x86)%%\Everything\Everything.exe\" -startupIF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" ( IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET Task=\"%%ProgramFiles%%\Everything\Everything.exe\" -startup)but I would probably rewrite it as: (SET "Tsk=(x86)") & (IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" ( IF NOT DEFINED PROCESSOR_ARCHITEW6432 (SET "Tsk=")))SET Task=\"%%ProgramFiles%Tsk%%%\Everything\Everything.exe\" -startup -- 2) You next have two lines that are run together: [NOTE: You caught this myselfidem, good job!] REM Determine if windows xpVER | find "XP" > NULThis causes the "VER" test to never be executed. As a result, the next line: IF %ERRORLEVEL% == 0 GOTO ver_xpcauses my Win7 x64 system to be identified as XP and the rest of the version checking code to be skipped completely. I would expect that this is happening to you as well, myselfidem. In fact, from what I can tell, thanks to this error and error (II), every system will be identified as a 32-bit XP system. The Powershell commands listed at the bottom of the batch will never be executed. I guess this is just as well for Vista users, since if the version checking code was executed on a Vista system it would fail because you have another two lines run together at the end of the "XP Section" of code: [NOTE: You missed this one myselfidem.] GOTO exit:ver_vistaThe result of this is that the :ver_vista label doesn't exist as far as this batch is concerned so the code will fail and cause an execution error with an error box that the user will have to respond to in order to clear it. -- 3) Even if the run together lines were fixed, the version checking code takes a looong time for OS > XP, is more complicated than necessary, and Win 8 will be treated as XP, since all versions except Win 7 and Vista will be treated as XP. [As noted above, I think EVERYTHING is treated as if it was XP right now, ie I don't see how the Powershell code can possibly be executed.] For that matter, both Win 7 and Vista are treated the same so the extra tests to identify Vista are not necessary. Best I can tell, the only real reason to do an OS Version check is to see if it is Vista or above so that Powershell can be utilized, correct? Well, what if Powershell is not installed or has been removed? And Powershell can be added to XP, right? So, bottom line, there is no way to really tell if Powershell is present without checking for it, so you might as well just do that. Isn't that a better approach? Assuming that is a better approach, and based off info I found here - http://blogs.msdn.com/b/powershell/archive/2009/06/25/detection-logic-poweshell-installation.aspx'>http://blogs.msdn.com/b/powershell/archive/2009/06/25/detection-logic-poweshell-installation.aspx - I would replace the version checking code with this: REM Check for PowesrshellSTART "Get Windows Version" /WAIT REGEDIT /E "%TEMP%.\_Temp.reg" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1"IF EXIST "%TEMP%.\_Temp.reg" ( FOR /F "tokens=1* delims=:" %%G IN ('TYPE "%TEMP%.\_Temp.reg" ^| FIND /I "Install"') DO ( (SET /A "_PSInstalled=%%H") & (IF "!_PSInstalled!"=="1" GOTO:psFound) )) REM ps NOT foundREM put the old "XP" code hereGOTO:exit :psFoundREM put the old "Elevation" code hereGOTO:exitIf you really need to check for OS version, a much quicker way than "systeminfo" is to use the command "VER". See here - http://ss64.com/nt/ver.html'>http://ss64.com/nt/ver.html - for a discussion. Again, since in this instance it seems you only care if it is XP (or below?), or above XP, I would shorten it this way: FOR /F "tokens=2 delims=[]" %%G IN ('VER') DO (SET "_WinVer=%%G")FOR /F "tokens=2 delims=. " %%G IN ('ECHO %_WinVer%') DO (SET "_Major=%%G")IF "%_Major%"=="6" GOTO:aboveXP REM do your XP stuff hereGOTO:restOfStuff :aboveXPREM do your above XP stuff here :restOfStuffREM do anything else that pertains to all OS here EDIT: I was reminded by myselfidem that even if Powershell had been added to XP, that the commands for dealing with SCHTASKS are slightly different for XP vs Vista/7, so the two methods above have been combined in the full code shown below like this: REM Check for XPFOR /F "tokens=2 delims=[]" %%G IN ('VER') DO (SET "_WinVer=%%G")FOR /F "tokens=2 delims=. " %%G IN ('ECHO %_WinVer%') DO (IF "%%G"=="5" GOTO:treatAsXP)REM Check for Powershell in case it was uninstalledSTART "Get Windows Version" /WAIT REGEDIT /E "%TEMP%.\_Temp.reg" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1"IF EXIST "%TEMP%.\_Temp.reg" ( FOR /F "tokens=1* delims=:" %%G IN ('TYPE "%TEMP%.\_Temp.reg" ^| FIND /I "Install"') DO ( (SET /A "_PSInstalled=%%H") & (IF "!_PSInstalled!"=="1" GOTO:psFound) )) :treatAsXPREM do your XP stuff hereGOTO:exit :psFound - old "Elevation" codeREM do your above XP stuff hereGOTO:exitNote that the Powershell tests are still useful in case Powershell has been removed from the OS, as some folks have done in attempts to "slim" Win 7. -- 4) I was mainly concerned with the Batch command errors as I saw them. My resulting revised code is: @ECHO OFF & SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION::***********************************************************:::: Everything_Schedule.bat :::: Sets variables _Schedule, _TaskName, _Task and goes ahead :::: Original script from: http://myunster.com :::: Modified by bphlpt 2013-02-05 ::::***********************************************************::CALL :sub_CleanUp REM Set Task specific variables - %ProgramFiles% will be used if OS is 32-bit, %ProgramFiles(x86)% if OS is 64-bit(SET "_Schedule=ONLOGON")(SET "_TaskName=Everything")(SET "_X86=(x86)") & (IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" (IF NOT DEFINED PROCESSOR_ARCHITEW6432 (SET "_X86=")))(SET _Task=\"%%ProgramFiles%_X86%%%\Everything\Everything.exe\" -startup) REM Check if OS is XP, if it is not XP then check for PowerShell in case it was uninstalledFOR /F "tokens=2 delims=[]" %%G IN ('VER') DO (SET "_WinVer=%%G")FOR /F "tokens=2 delims=. " %%G IN ('ECHO %_WinVer%') DO (IF "%%G"=="5" GOTO:treatAsXP)START "Get Windows Version" /WAIT REGEDIT /E "%TEMP%.\_Temp.reg" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1"IF EXIST "%TEMP%.\_Temp.reg" (FOR /F "tokens=1* delims=:" %%G IN ('TYPE "%TEMP%.\_Temp.reg" ^| FIND /I "Install"') DO ( (SET /A "_PSInstalled=%%H") & (IF "!_PSInstalled!"=="1" GOTO:psFound))) :treatAsXPREM or PowerShell NOT found - using FINDSTR is necessary since WinXP does not support TN for schtasks /query(SET _Result="-1") & FOR /F "delims=, tokens=2" %%R IN ('SCHTASKS /query /fo csv /v ^| FINDSTR /L /C:"%_TaskName%"') DO (SET _Result=%%R)IF "%_TaskName%"==%Result% (SCHTASKS /Delete /TN "%_TaskName%" /F)SCHTASKS /Create /TN "%_TaskName%" /TR "%_Task%" /SC %_Schedule% /RL HIGHESTGOTO:exit :psFoundREM Do not forget to escape any double quotes in CMD arguments that you will pass to PowerShellSET _Argument=SCHTASKS /Create /TN \"%_TaskName%\" /TR \"%_Task%\" /SC %_Schedule% /RL HIGHEST /FSET _ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('Cmd.exe', '/C %_Argument%', '', 'runas')PushD "%~dp0"DEL /F "%~0.ELEVATED" >NUL 2>&1ECHO %_ELEVATED_CMD% >"%~0.ELEVATED"CALL %_ELEVATED_CMD%DEL /F "%~0.ELEVATED" >NUL 2>&1PopDGOTO:exit :sub_CleanUpREM Clean up any leftover files or variablesDEL "%TEMP%.\_Temp.reg" >NUL 2>&1FOR /F "tokens=1* delims==" %%G IN ('"SET "_" 2>nul"') DO (SET "%%G=")EXIT /B 0 :exitCALL :sub_CleanUpENDLOCAL & EXIT Please note that I have changed all variable names to begin with "_". I've never used either the "SCHTASKS" command or Powershell, so I left the code alone that dealt with them other than a very slight "cleaning up". Someone else really needs to double check this code to make sure it functions correctly in all circumstances. For example, I don't understand the purpose of this: IF EXIST "%~0.ELEVATED" DEL /F "%~0.ELEVATED"...ECHO %_ELEVATED_CMD% >> "%~0.ELEVATED"CALL %_ELEVATED_CMD%DEL /F "%~0.ELEVATED"Unless Powershell uses it in some way, I don't see that "%~0.ELEVATED" is ever used, just created and deleted. Did I miss something? I figure it must be for handling UAC if enabled, but I don't know how it works. If it is not needed, it seems like that could be changed to just: ...CALL %_ELEVATED_CMD% I hope all this makes sense. My final code has been very MINIMALLY tested. Please comment and ask for clarification if needed. Cheers and Regards
    1 point
×
×
  • Create New...