Get the OS information if server 2003 then run the script - windows

I have a .bat script that I created yesterday. I now need to find the OS name or a remote server and if its server 2003 64bit = true then run test.bat
Is there an easy way to do this?
Heres the script I want to run:
If OS = Server 2003
Then
psexec -u domain\user -p password \\#serverlist -s -i -d
msiexec.exe /i "\\share\folder\Avmr64.msi" /qb
I am finding several ways to get the info but how would I add an if statement in there?

One way you can test for a 64-bit system, without getting too technical, is to check for the C:\program files (x86)\ directory. In a 32-bit system, it's just C:\Program Files. This directory does also exist on 64-bit systems, so the absence of the C:\Program Files (x86)\ tells you it's 32-bit.
The third line of this batch file will run if the directory exists, if it doesn't exist, it goes to the :NoUse32bitVersion label.
C:
IF NOT EXIST "C:\Program Files (x86)\" GOTO IS32BIT
psexec -u domain\user -p password \\#serverlist -s -i -d msiexec.exe /i "\\share\folder\Avmr64.msi" /qb
:IS32BIT
echo Put your 32-bit version of the code here (or just EXIT)

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003
ver | find "some other os string" > nul
if %ERRORLEVEL% == 0 goto some_other_os
:ver_2003
your 2003 specific code here
Replace "some other os string" with meaningful string to get info about other os's if you want.

Related

Environment variable only works for elevated (admin) command prompt but does not for normal command prompt

I am using Windows 11 Pro Version: 21H2 (Build: 22000.978)
So I have OpenSSH on my device which is also added to system path variable as shown below in the command prompt snippet.
C:\Windows\System32>set path
Path=C:\oraclexe\app\oracle\product\11.
\VMware Workstation\bin\;C:\WINDOWS\sys
1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Pr
\ProgramData\nvm;C:\Program Files\nodej
m Files\dotnet\;C:\Android\android-sdk\
in;C:\Program Files\Git\cmd;C:\Program
I'm inside System32 folder in my command prompt (normal window | non-admin) and I opened the Explorer in the current directory by entering:
explorer .
I do see OpenSSH folder, but when I try to go inside (change directory) to OpenSSH, the command prompt throws an error denoting that the path does not exist. But it does exist on the drive!
C:\Windows\System32>cd OpenSSH
The system cannot find the path specified.
And obviously I cannot access ssh.exe which is located inside OpenSSH directory.
But interestingly, when I elevate the shell (open CMD as administrator) and try to access the OpenSSH directory or even the file ssh.exe, it worked! When I use the where command for ssh, it points to that right directory as shown below:
Command prompt as standard user:
C:\Windows\System32>ssh
'ssh' is not recognized as an internal or external command,
operable program or batch file.
Command prompt as administrator:
C:\Users\Admin>where ssh
C:\Windows\System32\OpenSSH\ssh.exe
It just does not work for cmd in normal window but works when cmd is opened as administrator.
Please read first at least the Microsoft documentation about the Windows File System Redirector and best also the documentation pages WOW64 Implementation Details and Registry Keys Affected by WOW64.
When a 32-bit application on 64-bit Windows starts cmd.exe either with %SystemRoot%\System32\cmd.exe or with %ComSpec% or with cmd.exe or in worst case with just cmd, there is started the 32-bit version of the Windows Command Processor in directory %SystemRoot%\SysWOW64 because of the file system redirector.
The file system redirector is also responsible for redirecting each access of %SystemRoot%\System32\OpenSSH to %SystemRoot%\SysWOW64\OpenSSH which does not exist on 64-bit Windows because of the OpenSSH package is available only as 64-bit application suite on 64-bit Windows on being available at all (depends on version of Windows).
The usage of Run as administrator results on 64-bit Windows in starting 64-bit cmd.exe in %SystemRoot%\System32 and therefore no file system redirection is done on any access to files and directories in %SystemRoot%\System32. For more details see Why does 'Run as administrator' change (sometimes) batch file's current directory?
In a Windows batch file can be used the following code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Expect ssh.exe in OpenSSH in Windows system directory by default.
set "ExeSSH=%SystemRoot%\System32\OpenSSH\ssh.exe"
if exist "%ExeSSH%" goto RunSSH
rem Expect ssh.exe in OpenSSH in native Windows system directory
rem on batch file processed by 32-bit cmd.exe on 64-bit Windows.
set "ExeSSH=%SystemRoot%\Sysnative\OpenSSH\ssh.exe"
if exist "%ExeSSH%" goto RunSSH
rem Search for OpenSSH using environment variable PATH.
rem The environment variable ExeSSH is undefined by the
rem next command line on no file ssh.exe found by cmd.exe.
for %%I in (ssh.exe) do set "ExeSSH=%%~$PATH:I"
if defined ExeSSH goto RunSSH
rem There could not be found the executable ssh.exe anywhere.
echo ERROR: Could not find ssh.exe in any directory.
echo/
pause
exit /B 1
:RunSSH
rem Use here "%ExeSSH%" ... to run this executable.
echo Found: "%ExeSSH%"
endlocal
This batch file can be used on Windows XP and all newer Windows versions whereby on the older Windows versions is most likely output just the error message that the executable ssh.exe could not be found because of not being installed by default.

How to get chrome version using command prompt in windows

Is it possible to get version installed chrome version using command prompt in windows?
Tried,
"C:\Program Files\Google\Chrome\Application\chrome.exe" -version
"C:\Program Files\Google\Chrome\Application\chrome.exe" --version
"C:\Program Files\Google\Chrome\Application\chrome.exe" -product-version
"C:\Program Files\Google\Chrome\Application\chrome.exe" --product-version
When i do that, a browser instance is opening. What flag should I be using to get the version.
I am using Windows 7. Google Chrome version is 67.0.3396.87.
Thanks in advance
As of today user4851's is still working. I took a look at his linked bug report and the proposed work around did not work for me anymore.
Anways a new hkey is present in my directory which allows you to query the chrome version without being aware of the actual installation location:
reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version
There's a bug filed about this: https://bugs.chromium.org/p/chromium/issues/detail?id=158372
Original Answer (but see the update below)
What works for me is
wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value
It prints
Version=67.0.3396.99
surrounded by some blank lines.
There are some other suggestions in the bug comments, like querying the registry.
Update
Someone from the Chromium team posted this "totally unsupported" batch file in the bug comment thread:
#ECHO OFF
:: Look for machine-wide Chrome installs (stable, Beta, and Dev).
:: Get the name, running version (if an update is pending relaunch), and
:: installed version of each.
FOR %%A IN (
{8A69D345-D564-463c-AFF1-A69D9E530F96},
{8237E44A-0054-442C-B6B6-EA0509993955},
{401C381F-E0DE-4B85-8BD8-3F3F14FBDA57}) DO (
reg query HKLM\Software\Google\Update\Clients\%%A /v name /reg:32 2> NUL
reg query HKLM\Software\Google\Update\Clients\%%A /v opv /reg:32 2> NUL
reg query HKLM\Software\Google\Update\Clients\%%A /v pv /reg:32 2> NUL
)
:: Look for Chrome installs in the current user's %LOCALAPPDATA% directory
:: (stable, Beta, Dev, and canary).
:: Get the name, running version (if an update is pending relaunch), and
:: installed version of each.
FOR %%A IN (
{8A69D345-D564-463c-AFF1-A69D9E530F96},
{8237E44A-0054-442C-B6B6-EA0509993955},
{401C381F-E0DE-4B85-8BD8-3F3F14FBDA57},
{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}) DO (
reg query HKCU\Software\Google\Update\Clients\%%A /v name /reg:32 2> NUL
reg query HKCU\Software\Google\Update\Clients\%%A /v opv /reg:32 2> NUL
reg query HKCU\Software\Google\Update\Clients\%%A /v pv /reg:32 2> NUL
)
That should probably be seen as the right way to go for the time being.
I tried Kilian's answer, however in my case, I was running it against a bunch of machines remotely via a service, so I don't think HKEY_CURRENT_USER was valid:
ERROR: The system was unable to find the specified registry key or value.
Assuming you know where the exe is, you can try a different approach and read the version property of the exe file:
# Powershell
# Older versions install to the 32-bit directory
(Get-Item "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe").VersionInfo
# Newer versions use the 64-bit directory
(Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo
ProductVersion FileVersion FileName
-------------- ----------- --------
76.0.3809.100 76.0.3809.100 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
To using it in cmd.exe or via any subprocess calls (python, go os/exec, etc.) you can do,
powershell -command "&{(Get-Item 'Absolute\path\to\chrome.exe').VersionInfo.ProductVersion}"
Using only commandline utils
dir /B/AD "C:\Program Files (x86)\Google\Chrome\Application\"|findstr /R /C:"^[0-9].*\..*[0-9]$"
78.0.3904.97
List only directories /AD within the Chrome application folder in short form /B.
findstr /R /C:"..." applies the following regex to the list of directories. The regex matches every foldername starting with a digit ^[0-9] and ending with ad digit [0-9]$.
Between the first and last digit there are any characters .* allowed but at least one dot should appear \.
user1425134's solution worked for me, but if you are allowed to assume that Chrome is part of %PATH% (if you can open Command Prompt and type chrome to start the browser), then it can be greatly simplified.
From Powershell you can type (Get-Command "chrome").Version.ToString()
Or from cmd.exe you can type powershell -c "(Get-Command "chrome").Version.ToString()"
(same for Chromium, just replace the command name)
I was able to use the rust kitty's solution without having chrome on my path, as:
from PowerShell:
(Get-Command C:\Program Files (x86)\Google\Chrome\Application\chrome.exe').Version.ToString()
from cmd:
powershell -command "(Get-Command C:\Program Files (x86)\Google\Chrome\Application\chrome.exe').Version.ToString()"
Via Powershell the command is -
Get-WmiObject -Class Win32_Product | ? {$_.name -eq 'Google Chrome'} |select Name,Version

Running a UNC path using PSexec through CMD

I got this code below, it will run the LS-PrePost-3.0-Win32_setup.exe using cmd.
My psexec is in c:\psexec. And I put put my batch file and the exe file in the same folder.
c:\psexec\psexec -d \\%%M cmd /c start /wait "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
My code seems does nothing. It execute the code but the exe file didnt run the the remote PC.
Edit: I changed the directory of Psexec.
You can try with something like
c:\psexec\psexec \\%%M -d -c "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
As you are not waiting (-d) you don't need the start /wait
As you are starting a executable file not an internal command, you don't need the cmd /c.
The file is executed in the remote machine, so, it must be available in the remote machine. Copy it (-c)
From psexec help
-c Copy the specified program to the remote system for
execution. If you omit this option the application
must be in the system path on the remote system.
-d Don't wait for process to terminate (non-interactive).
cmd Name of application to execute.
arguments Arguments to pass (note that file paths must be
absolute paths on the target system).
You can use pushd and popd to pass the the command through psexec for UNC paths.
psexec.exe //FQDN -d cmd /c "pushd \\UNCFOLDER\ && file2execute.exe && popd"

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.

"Windows was unexpected at this time" error when trying to compare 2 strings in batch script

I am writing a batch script to use USMT to update computers from XP to 7. Because USMT has a scanstate component that needs to be run before the OS upgrade and a loadstate computer that has to be run after the OS upgrade I am trying to use an if statement to check what the operating system is and then run the proper commands. I am new to batch files but from everything I have been reading it seems like I am writing it properly but I am obviously messing up somewhere. I am getting a "Windows is unexpected at this time error." I also know that the variables are being set properly because of the pause commands that I included. I also tried using IF %WINVERSION% == %XP% goto XPTRUE/WIN7TRUE and enclosing everything within the brackets under a :XPTRUE/WIN7TRUE but that gives the same error.
::Don't have commands print...only outputs are printed
#echo off
:: Set constants
SET XP=Microsoft Windows XP [Version 5.1.2600]
SET WIN7=Microsoft Windows [Version 6.1.7601]
SET XPUSMTLOCATION=C:\Program Files\USMT\Binaries\v4\x86
SET 7USMTLOCATION=C:\Program Files (x86)\USMT\Binaries\v4\amd64
SET BACKUPLOACTION=\\[SERVER IP]\z$\UserAccountBackUps\Backups
SET LOCALBACKUPLOCATION=C:\Backup\USMT
SET NASBACKUPLOCATION=S:\UserAccountBackUps\Backups
#PAUSE
::Get the current version of Windows batch file is running on and store it in WINVERSION
FOR /f "delims=" %%A IN ('ver') DO #SET WINVERSION=%%A
echo %WINVERSION%
PAUSE
::Get the MAC address of the computer and store it in MACA
FOR /F %%A IN ('getmac') DO #SET MACA=%%A
echo The MAC Address is: %MACA%
:: Tell user about script
echo This is a script designed to migrate computers with one network card from Windows XP to Windows 7 using USMT, this script should not be used with computers that have multiple network cards
echo Xp is %XP%
echo 7 is %WIN7%
::Check to see if the current version is XP
PAUSE
IF %WINVERSION% == %XP% (
echo This is windows XP
::Change directory to the location of USMT files
cd %XPUSMTLOCATION%
::Run scanstate to create backup
scanstate.exe C:\Backup /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigApp.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigDocs.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigUser.xml" /o /v:2
::Change directory to the location of where the USMT backup is
cd %LOCALBACKUPLOCATION%
::Rename the backup to the MAC Address
rename USMT.MIG %MACA%.MIG
::Map the NAS to a drive because xcopy can not take IP addresses
echo Mapping NAS to drive
::NAS is mapped to drive S, if S is used for something else change s below to different letter
net use s: \\[SERVER IP]\z$
echo Prepairing to copy backup to NAS
::Use xcopy to transfer backup file the /v ensures the files are identical
::This must be done this way because if USMT tries to backup directly to the NAS it tries to overwrite all existing files
xcopy %LOCALBACKUPLOCATION%\%MACA%.MIG %NASBACKUPLOCATION% /v
echo The copy has completed, run this batch file again after OS Upgrade
)
IF %WINVERSION% == %WIN7% (
echo This is Windows 7
PAUSE
)
When I run this on my Windows 7 computer I get this:
I get the same output on my XP computer except it tells me the current version is xp instead. Help would be greatly appreciated.
The line below:
FOR /f "delims=" %%A IN ('ver') DO #SET WINVERSION=%%A
stores in WINVERSION variable a string that contain several words separated by spaces, for example:
SET WINVERSION=Microsoft Windows [Version 6.2.9200]
This way, the line below:
IF %WINVERSION% == %XP% (
is expanded to:
IF Microsoft Windows [Version 6.2.9200] == Microsoft Windows XP [Version 5.1.2600] (
that, of course, cause a syntax error! Type: IF /? for further details.
The way to compare two strings that may contains spaces, is enclosing they in quotes:
IF "%WINVERSION%" == "%XP%" (

Resources