How can I perform different actions in windows batch file based on most recent version of installed program - windows

Some of our computers run multiple versions of Microsoft Access (97 & 2010) while others run 365. For the pcs running multiple versions, the default is set to 97. I have a batch file that is performing various tests to see if files exist, and finished by running an Access 2010 database called MWO.accdb. See below.
if exist c:\windows\system32\mscomct2.ocx goto step2
rem copy mscomct2 and register
cscript \\file\apps\Database\Maintenance\365\MsgBox.vbs "Preparing necessary libraries."
copy "\\file\apps\Database\Maintenance\365\mscomct2.ocx" "c:\windows\system32\"
regsvr32 /u mscomct2.ocx
regsvr32 /i mscomct2.ocx
:step2
if exist "%USERPROFILE%\Desktop\MWO.lnk" goto step3
rem create shortcut on user's desktop for future use
cscript \\file\apps\Database\Maintenance\365\MsgBox.vbs "Creating shortcut on desktop & adding to start menu."
copy "\\file\apps\Database\Maintenance\MWO-INSTALL.lnk" "%USERPROFILE%\Desktop\MWO.lnk"
mkdir "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Maintenance"
copy "\\file\apps\Database\Maintenance\MWO-INSTALL.lnk" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Maintenance\MWO.lnk"
:step3
\\file\apps\Database\Maintenance\365\MWO.accdb
Is there an easy way to test for the latest version of access, and force the file to open with it to avoid the defaulting to 97 problem?

wmic product where caption="Access" get caption,version
(I don't have access installed, the captionstring may differ)

Related

Batch file - IF EXISTS statement "The system cannot open the device or file specified."

I have written a batch file to uninstall a faulty WiFi driver (Intel ProSet Wireless) and set up the appropriate wireless profile on a laptop. This script is intended to be run remotely through Symantec Management Agent.
The code starts by running a group policy update to pull down appropriate
network certificates from the server. Then the code checks to see if the WiFi driver is installed. If it is, the script uninstalls it. Afterwards, in either case, it will wipe the current wireless profiles and call another batch file to install the appropriate wireless profile.
My issue is, when I run the script, the console will report "The system cannot open the device or file specified." after the software is uninstalled and it will terminate. The IF EXIST statement checks to see if one of the software files is there.
Typically, I can just run the same script a second time, and the IF EXIST case will not be met, so the rest of the batch file will work properly.
I am attaching my code below -- am I using IF EXIST correctly?
gpupdate /force
IF EXIST "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\setup.exe" (
cd "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\"
start /wait setup.exe /uninstall
)
TIMEOUT /T 3 /nobreak >nul
netsh wlan delete profile name=*
cd "C:\Wireless_Settings\"
Mobile_Devices_profile.bat
I have researched other posts, and I do believe I am using the condition correctly. I don't see any other post that matches my case. It always correctly checks to see if the condition is met, however I don't understand why the program terminates after the software is uninstalled. All that I believe should happen is the case is no longer met, so the script continues on.
Possible solution : (your if statement appears to be correct)
Insert a pushd statement before the cd and a popd after the start.
This will ensure you return to your original directory after the if statement invokes setup.exe
IF EXIST "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\setup.exe" (
PUSHD
cd "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\"
start /wait setup.exe /uninstall
POPD
)
If it works, fine and good. If it doesn't, It's easy to undo.
Sure - in theory, you could change the cd to a PUSHD instead. There are many paths.
IF EXIST "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\setup.exe" (
PUSHD "C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\"
start /wait setup.exe /uninstall
POPD
)
This should work.
Now that's interesting. I'm sure I've use pushd without arguments before. The documentation reads
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
which is not explicit when the option argument is missing.
I've also noticed that a dir list on my batch-development directory now lists a unicode-named file with spaces between the squares whereas it used to not contain those spaces. Maybe something has been silently changed... cmd version is dated 20170929
Setup.exe is trying to remove the directory from the Package Cache which happens to be your current working directory. Not a good way for a script to live.
#setlocal ENABLEEXTENSIONS
#rem #set prompt=$G
#set "_setupExe=C:\ProgramData\Package Cache\{552523b2-40ad-46b3-94f6-2b99d0860d5c}\setup.exe"
#gpupdate /force
#if exist "%_setupExe%" call %_setupExe% /uninstall
#TIMEOUT /T 3 /nobreak >nul
#netsh wlan delete profile name=*
#cd "C:\Wireless_Settings\"
#Mobile_Devices_profile.bat
I would add that debugging your script was complicated by the fact that you had a multi-line code block. You should avoid those at all costs. It's better to call a subroutine if you can fit it all on a single line.

the installation package could not be open batch file

I've been working on a batch file all day, that I can't get to work open through GPO (another day, another question). So I decided to do it manually with every computer. I have two exe's and one MSI. The exe's work perfectly fine. They get installed, and it all works out. The MSI, however, doesn't. It gives me the error: the installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.
Now when I go to the network share and use it from there, it works perfectly fine. So there must be an issue with my code.
Here's the code:
#echo off
IF NOT EXIST "C:\Program Files (x86)\Citrix\ICA Client\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\1\"
.\CitrixReceiver-4.4.1000.exe /silent
)
IF NOT EXIST "C:\Program Files (x86)\triCerat\Simplify Printing\ScrewDrivers Client v4\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\"
msiexec.exe /i ".\Screwdriver.msi"
)
IF NOT EXIST "C:\Program Files\Cloudwerx\CloudwerxPlugin\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\3\"
.\cloudwerx-setup.exe /silent
)
pause
Any help would be greatly appreciated, thanks.
I am guessing that your problem is the distinction in powershell between the current location (set by the pushd command) and the working directory (unaffected by the pushd command). You can see the working directory of the powershell process using the [Environment]::CurrentDirectory property:
# C:\> [Environment]::CurrentDirectory = "c:\"
# C:\> [Environment]::CurrentDirectory
c:\
# C:\> pushd C:\Temp
# C:\Temp> [Environment]::CurrentDirectory
c:\
# C:\Temp> Get-Location
Path
----
C:\Temp
WHat is probably happening is that msiexec.exe is using the working directory (i.e. [Environment]::CurrentDirectory) and not the current powershell location at invocation. I would just specify the full path to msiexec:
msiexec.exe /i "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\\Screwdriver.msi"
MSI installation packages build with an older WIX utility would throw the error whenever installation was attempted from a batch script that was accessed on a shared drive using UNC path instead of a mapped drive letter. On the other hand whenever the batch file was executed with a mapped drive letter the installation would work normally.
I'm not blaming WIX here because I'm not certain whether they are responsible. I'm just describing symptoms here. It might just be the result of invoking plain vanilla Windows batch script that in turn executes msiexec with a bunch of command line parameters.

Building a driver

I have some issue in a driver & I have to troubleshoot it, before that I need to build it. I have no experience in driver.
I have the source code & bat file. It might be using WDK 7(I'm not sure)
Can anyone guide me in building the driver project with below bat file.
Ofcourse I can understand bat file is setting environment variables & calling build.exe
My main questions are:
1. Which WDK i need to install, so that I can specify its path in bat file
2. How to run bat file, do i need to run in visual studio command prompt or how?
if you give some Some general idea on building a driver for a beginner that would be much appreciated. Thanks.
[if you need any info, which i can provide please feel free to ask]
bat fie:
rem #echo off
rem --------------------------------------------------------------------
rem SafeBoot Windows NT 32 bit driver build script
rem --------------------------------------------------------------------
set MC_ENV=fre
if "%1%"=="debug" goto dbg
if "%2%"=="debug" goto dbg
if "%3%"=="debug" goto dbg
goto nodbg
:dbg
echo **** DEBUG BUILD ****
set MC_ENV=chk
:nodbg
rem --------------------------------------------------------------------
rem We want the absolute path of this directory, so we use a little utility
rem that creates a batch file that sets this into a environment variable.
rem --------------------------------------------------------------------
..\Translations\Bin\setcd -d DRV_DIR > thisdir.bat
call thisdir
del thisdir.bat
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
set MSTOOLS=%MSSdk%
set BASEDIR=
set DDKBUILDENV=
set NTDBGFILES=
set DDK_INC_PATH=
set DDK_LIB_DEST=
set DDK_LIB_PATH=
set CRT_INC_PATH=
set CRT_LIB_PATH=
set BUILD_ALT_DIR=
call %DDK64%\bin\setenv %DDK64% %MC_ENV% i386
echo %DRV_DIR%
cd %DRV_DIR%
if NOT "%1"=="/a" goto build
del /q objfre_wlh_x86\i386\*.*
del /q objchk_wlh_x86\i386\*.*
rmdir /Q /S driver32
md driver32
:build
rem set MSC_OPTIMIZATION=/Od
echo Building started...
%DDK64%\bin\x86\build.exe -f -z -E
echo Building completed...
if ERRORLEVEL 1 goto error
rem copy objfre_wlh_x86\i386\McPvDrv.sys driver32\McPvDrv.sys
if "%MC_ENV%"=="chk" (
md ..\..\..\build\Win32\Debug
copy objchk_wlh_x86\i386\McPvDrv.sys ..\..\..\build\Win32\Debug\McPvDrv.sys
echo chk
If you want to build a driver for Windows 7 or newer use Visual Studio 2015 which is integrated with the lastest WDK 10. Building drivers is way easier this way.
If you want to build drivers for Windows XP then you will have to use WDK 7 and use a scripts similar like yours. Wdk 7 has no integration with visual studio. You have to specify source file in a special file called sources. See example drivers from Wdk 7. You can find a good cmd for building such drivers https://www.osronline.com/article.cfm?article=43 This website has the best driver dev tips
If you do not need XP support I highly recommend you go the much much easier way using Visual Studio 2015, windows sdk and windows 10 wdk.
LE:
You can also use Visual Studio 2013 which supports integration with WDK 8 and WDK 8.1 but not with WDK 10.
From your .bat file, it has:
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
So, it might be looking for that version. If so, here is a page: What is the Microsoft name for WinDDK version 6001.18001?
Also, see https://en.wikipedia.org/wiki/Windows_Driver_Kit Your .bat wants, by default, build 6000 (released: november 29, 2006 for Windows Vista)
You could also try to use the latest one. Here's the link for WDK 10 with download instructions. WDK 7 should be similar: https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573%28v=vs.85%29.aspx so find the right page. This page also has links to other pages that may help answer your other questions.
In any case, you'll need to modify the .bat to point to the correct WinDDK directory by setting the DDK64 variable to point to it.
You can probably install multiple different versions to different directories, but my guess is that the latest will let you build backward compatible versions. So, if you're building for Win7 (e.g.), get the latest DDK for Win7 (see the table in the wiki page).

SCCM 2012 to distribute files (not application)

I want to use SCCM 2012 to distribute some files (not application, no executable there).
I know there is a way to create a batch file with xcopy command, and create a package to deploy that batch file to the clients. However, if I use xcopy I cannot leverage the BITS functionality and other benefits from SCCM.
Is this possible?
Hi I can see this is an old post, but I have used SCCM to copy files in the past.
A company I worked for in the past refused to use GP for any file copy (don't get me started) so I had to implement GP files over SCCM.
Here are some examples I used.
xcopy User.bmp "%ProgramData%\Microsoft\User Account Pictures" /c /y /r
REG ADD HKLM\Software\FILE_GP /v Copy_User_image /t REG_sz /d 1.0 /f
timeout 5
if not exist "%windir%\system32\oobe\Info\backgrounds" md "%windir%\system32\oobe\Info\backgrounds"
xcopy backgrounddefault.jpg "%windir%\system32\oobe\Info\backgrounds" /c /y /r
REG ADD HKLM\Software\File_GP /v Backgrounddefault /t REG_sz /d 1.0 /f
timeout 5
I had the script add the Registry key so I could use Application deployment and have the detection method look for the key as well as the file. So if a user deleted the file it would return.
The main reason for me to have the Reg Key was if you wanted to push an updated version of the file I could set the new copy script to push 1.1 to the reg key and have the detection look for that version, then I could retire the 1.0 version.
I found also for short scripts of 1 file of small size without the timeout line it would report failed immediately, if there were larger files I didn't need the timeout. The initial fail does clear after some time without the timeout line.
It should work if you chose your settings right.
In your batch make sure that all the paths and references are relative and never absolute.
Then when you create the deployment there is a point called "Distribution Points" where you can select the deployment options for fast as well as unreliable networks. There you would have to choose "Download content from distribution point and run locally" in both cases. Then your batch would basically copy the files from the CCM cache to the hard drive.
This method should work for packages, for applications I think it would be more difficult because you cannot directly specify deployment options for fast networks there.
Packages are downloaded to the clients with BITS by default. The XCOPY command would copy from CCMCACHE to final destination on the machine.
Not sure if you have tried it or not but if you have deployed any .exe file for application its ask for dependence file in that you mention your required file which need to copy and create a script to copy data from ccmcache to copy the data to destination folder. for this you can get the use of BITS and to check if the file are copied or not you can use a proper detection method.
I used a PowerShell script for this purpose and it works like charm.
Create a new script and change this one liner to your mods:
Copy-Item -Path "\\[NETWORK PATH]" -Destination "[DESTINATION PATH]" -Recurse
Other way around—if need to make a required package—could be to create a new application deployment and run this through PowerShell (ExecutionPolicy ByPass) and manually install from the client side; if you don't want user interaction, make it a required deploy.

Batch file to start all programmes in the start folder of XP

I need start all folders in a the Windows "Start/Programs/Startup folder" of an XP machine, explorer is disabled to stop top people playing and remove the Start and Task-bar.
I can run a batch file at start-up but how do I write the batch to run ALL programs in the "Start/Programs/Startup folder" the programs in the folder may change but the batch needs to remain the same
I am able to open each file individually using the below code but I really need to be able to open everything in the folder to avoid problems in the future
start "" /b "C:\Documents and Settings\User\Start Menu\Programs\Startup\PROG.appref-ms"
I have tried the code below, that batch starts but nothing starts
%DIR%=C:\Documents and Settings\Pete\Start Menu\Programs\Startup
for %%a in (%DIR%\*) do "%%a"
Running the batch from the desktop also doesn't run the programs in the start folder, the DIR address is taken from windows explorer when I navigated to the folder with the short cuts in
This is an interesting request--one that I would question the motive behind, but since you asked, here's a way you could accomplish it:
#echo off
set DIR=C:\Your\Directory
for %%a in ("%DIR%\*") do "%%a"

Resources