I'm trying to determine the computer model (e.g., "Optiplex 9010") in a batch script running in the context of Windows PE 3.1. When running Windows proper, I can do this using wmic csproduct or wmic bios but neither of these return any data when running Windows PE. (This also seems to be true of most or all other classes.)
I've already installed the winpe-wmi.cab package to support WMI. Using wmic path instead of an alias makes no difference.
How can I make my wmic commands work?
It turns out that most wmic commands, including both wmic csproduct and wmic bios, will work if you install the winpe-scripting.cab package in addition to the winpe-wmi.cab package.
Related
Hopefully, this question can be resolved, as I've been searching all over the web for answers to no avail. Does Cygwin have a command to display cpu % of a Windows process? I know that the command top can display this information, but it only displays Cygwin processes and not Windows. There's another command, ps, but this doesn't display cpu %. Perhaps there's a command that has the best of both top and ps.
Well, you don't need "native" Cygwin application to do that, because Cygwin can execute also native Windows' executables. In fact, Cygwin's "native" executables are Windows' native executables. They just use Cygwin's DLLs to map Linux system calls to Windows' ones.
Just use for example package PsTools from SysInternals (now part of Microsoft). It contains pslist which is able to output running Windows' processes information.
This works for me in cygwin, to check if any audio is playing:
t1=`/cygdrive/c/windows/system32/tasklist /fi "ImageName eq audiodg.exe" /v /nh`
(remove the "/nh" to see column headers)
(Actually, this only returned the total cpu usage. But i put it in a loop in a script w/ 'date +%s to get the time, and then calculated the percentage.)
Scenario
Let us assume an application is installed on Windows XP, and on Windows 7, for a single user and that it installs to the following directories in these 2 operating systems:
Windows XP:
C:\Documents and Settings\<user_name>\Local Settings\Application Data\<target-folder>
Windows 7:
C:\Users\<user_name>\AppData\Local\<target-folder>
I want to create a batch script that will assign the target-folder directory to a variable within the script.
Question
What is the simplest and most robust way to set a variable in a batch script based on which version of windows the script is running in?
Example Answer
set targetDir = ?
ver | find "XP" >nul && (
set "TargetDir=%USERPROFILE%\Local Settings\Application Data\[target-folder]"
) || (
set "TargetDir=%USERPROFILE%\AppData\Local\[target-folder]"
)
echo "%TargetDir%"
There is a environment variable named %USERPROFILE% or %APPDATA% which can be used to query the profile path name.
In general use WMIC for to query info like this, it works in XP (Professional but not home), Vista, windows 7, windows 8, and servers. For example to get the OS name use:
WMIC OS GET caption
Or name instead of caption if you want more info. You can ask for other data such as OSArchitecture that would tell you if its 32 or 64 bit. And you can combine queries, like this:
WMIC OS GET caption, osarchitecture, muilanguages
You can get full list with WMIC OS but read the docs on WMIC it can do a lot of different queries and tasks, such as listing all users. But the real catch is that you can query OTHER users environments with:
wmic ENVIRONMENT
:: and
wmic /user user ENVIRONMENT
Last but not least, if even version fails to give you a clue you can use
IF EXISTS C:\Users
To determine if folders exist.
After further research I found this resource to be both simple and robust because it detects all versions of Windows rather than just windows xp or windows 7.
The answer is, "it depends".
For example, I control the computer setups at work and our Windows 7 machines are all 64-bit, so I can check for C:\Program Files (x86).
So, I do something like this:
IF EXIST "C:\Program Files (x86)" (
SET TargetDir=C:\Users\<user_name>\AppData\Local\<target-folder>
) ELSE (
SET TargetDir=C:\Documents and Settings\<user_name>\<target-folder>
)
Granted, these are not foolproof, but they work for my needs.
If you are talking about computers that you don't control or use a different language, then things get more complicated.
I am in a small bind. The program in question can be installed in the program files directory (64bit) or X86 path. The program is already installed in over 200 machines. I am fairly certain the default install path was X86 as that's the default. I am not certain and must cover both scenarios. The original sys admin that installed this didn't use an .msi so I'm left with what I've found as ""C:\Program Files\InstallShield Installation Information{78AC336D-25F6-4916-A711-2EA2F69E0319}\setup.exe" as the command provided by one utility to remotely uninstall said application I found. Didn't work and I cannot attempt to push this out in hopes it'll work.
Given this problem, is there a way to uninstall this program via a script that would check both program files and X86 paths and uninstall depending on location? OR, is there a script that will just flat out uninstall the program regardless without the concern for the X86/program original install location. I just need to uninstall it period across all of these machines. The install .bat is good to go. What I cannot do is just get window to uninstall X application via a script for 32 or 64 bit machines.
I've tried MsiExec.exe /X{78AC336D-25F6-4916-A711-2EA2F69E0319} /quiet with no go. I can try to install the .msi this time around but am lost and my knowledge is limited with scripting or any uninstall scripts for telling "end users" without confusing them to just click here. I could tell them to go to control panel, etc..but they'll be lost....typical.
Any ideas on how to script this uninstall given it wasn't an original .msi and I am not sure how to get something working? I'm open to anything. I have two days to get this fixed and I'm in panic mode...
Any ideas or help on code would be greatly appreciated.
Regards,
Brian
wmic can call an uninstaller. I haven't tried this, but I think it might work.
wmic /node:computername /user:adminuser /password:password product where name="name of application" call uninstall
If you don't know exactly what the program calls itself, do
wmic product get name | sort
and look for it. You can also uninstall using SQL-ish wildcards.
wmic /node:computername /user:adminuser /password:password product where "name like '%j2se%'" call uninstall
... for example would perform a case-insensitive search for *j2se* and uninstall "J2SE Runtime Environment 5.0 Update 12". (Note that in the example above, %j2se% is not an environment variable, but simply the word "j2se" with a SQL-ish wildcard on each end. If your search string could conflict with an environment or script variable, use double percents to specify literal percent signs, like %%j2se%%.)
If wmic prompts for y/n confirmation before completing the uninstall, try this:
echo y | wmic /node:computername /user:adminuser /password:password product where name="whatever" call uninstall
... to pass a y to it before it even asks.
I haven't tested this, but it's worth a shot anyway. If it works on one computer, then you can just loop through a text file containing all the computer names within your organization using a for loop, or put it in a domain policy logon script.
Further reading on wmic
More reading on wmic
Assuming you're dealing with Windows 7 x64 and something that was previously installed with some sort of an installer, you can open regedit and search the keys under
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
(which references 32-bit programs) for part of the name of the program, or
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
(if it actually was a 64-bit program).
If you find something that matches your program in one of those, the contents of UninstallString in that key usually give you the exact command you are looking for (that you can run in a script).
If you don't find anything relevant in those registry locations, then it may have been "installed" by unzipping a file. Because you mentioned removing it by the Control Panel, I gather this likely isn't then case; if it's in the list of programs there, it should be in one of the registry keys I mentioned.
Then in a .bat script you can do
if exist "c:\program files\whatever\program.exe" (place UninstallString contents here)
if exist "c:\program files (x86)\whatever\program.exe" (place UninstallString contents here)
In my experience, to use wmic in a script, you need to get the nested quoting right:
wmic product where "name = 'Windows Azure Authoring Tools - v2.3'" call uninstall /nointeractive
quoting both the query and the name. But wmic will only uninstall things installed via windows installer.
I have a batch installer that overrides my usb devices drivers.
how can I force my windows to scan for hardware changes using a batch file?
START /WAIT RunDll32.exe Syssetup.dll,UpdatePnpDeviceDrivers
1 The best way I found is:
powershell -windowstyle hidden -command "& {\"rescan\" | diskpart}"
For the detail, you can refer to this link :"Use the rescan command to rescan all I/O buses and cause any new disks that have been added to the computer to be discovered."
For Windows 2008/7 and above, the powershell and diskpart is shipped with OS.
Or just run it without powershell:
echo rescan | diskpart
2 The other way is use the MS command line tool, Devcon, which you have to download it first
You can use it to do a lot of things, including enable/disable/rescan all kind of device(not only disk), update device driver, ... even on the remote machine.
You also can see the source code of it in this link
Windows 10 ships with PnPUtil.exe. Run pnputil.exe /scan-devices from administrative command prompt.
https://serverfault.com/a/1060172/365042
For example I have entered
netsh.exe
in command line (aka cmd.exe)
Now I would like to know which netsh.exe is being run, lets say I have more than one netsh.exe on my PATH (I do know that the first one in the PATH will be run, but lets say I have a very BIG PATH and I don't have time to search for it manually. To be fair its not always your machine you are using and many times PATH is set by admins and many times they are not the best).
Is there any way in windows to find that out from command line? I want to write a BATCH application that is using that.
It's a one-liner batch file:
#for %%e in (%PATHEXT%) do #for %%i in (%1%%e) do #if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
Save this as whereis.cmd, then type
whereis netsh
I think the following blog post does exactly what you want: http://pankaj-k.net/weblog/2004/11/equivalent_of_which_in_windows.html
I would use Windows Management Instrument (WMI) to query:
"SELECT ExecutablePath FROM Win32_Process WHERE Name = 'netsh.exe'"
http://www.activexperts.com/activmonitor/windowsmanagement/wmi/samples/ WMI samples
You will need to find something suitable for your scripting
On Windows Server (at least 2003 and 2008, dont know with 2000) you can use where.exe
Where.exe /?
Description:
Displays the location of files that match the search pattern.
By default, the search is done along the current directory and
in the paths specified by the PATH environment variable.
...
The first file listed is also the first file windows will use.
I use a copy on my XP workstation and it works fine too.