Creating a script to gather PC information - to assist those asking for help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Creating a script to gather PC information - to assist those asking for help

#1 Post by foxidrive » 10 Aug 2016 23:20

I'm asking for help here: this script provides essential information so that volunteer helpers have information about a question writer's computer.

Help would be welcomed to think of extra details to add.

aGerman and I have had discussion regarding this and here is the result so far: some extra samples from more computers would be valuable too. There could be bugs lurking here...

//EDIT: Latest Update -> http://www.dostips.com/forum/viewtopic.php?f=3&t=6108&p=49091#p49091



Code: Select all

Windows version:   Microsoft Windows [Version 6.3.9600]
Product name:      Windows 8.1 Pro with Media Center
Processor arch:    x86 and 32 bit Windows

Date format:       dd/mm/yy =  Wed 10/08/2016
Time format:       24 hours =  14:09:50.35
Extensions:        system:Enabled   user:Enabled
Delayed expansion: system:Disabled  user:Disabled
Locale name:       en-AU      Code Pages:   OEM 850     ANSI 1252
DIR  format:       06/08/2016  20:36     3,484,418,048 pagefile.sys

pagefile.sys was chosen for the DIR format as it provides the last boot time of the computer and an indication of the amount of virtual memory.


The script

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion
 set "International=HKCU\Control Panel\International"
 set "CurrentVersion=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
 set "CodePage=HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage"
 set "CMDproc=Software\Microsoft\Command Processor"
 set bitness=32
 if exist "%SystemRoot%\SysWOW64\" set bitness=64
 for /f "tokens=1,2,*" %%a in ('reg query "%International%"^|find "REG_"')  do set "%%a=%%c"
 if "%iDate%"=="0" (set format=mm/dd/yy) else if "%iDate%"=="1" (set format=dd/mm/yy) else if "%iDate%"=="2" (set format=yy/mm/dd)
 if "%iTime%"=="0" (set hours=12) else if "%iTime%"=="1" (set hours=24)
 for /f "delims=" %%a in ('dir "%SystemDrive%\pagefile.sys" /a ^|find ":"') do set DirFormat=%%a
 for %%a in (es eu ds du) do set "%%a=Disabled"
 reg query "HKLM\%CMDproc%" /v "EnableExtensions" 2>nul|find "0x1">nul && set "es=Enabled "
 reg query "HKCU\%CMDproc%" /v "EnableExtensions" 2>nul|find "0x1">nul && set "eu=Enabled "
 reg query "HKLM\%CMDproc%" /v "DelayedExpansion" 2>nul|find "0x1">nul && set "ds=Enabled "
 reg query "HKCU\%CMDproc%" /v "DelayedExpansion" 2>nul|find "0x1">nul && set "du=Enabled "
 >"%temp%\info.txt" (
  echo [code^]
  for /f "delims=" %%a in ('ver') do echo Windows version:   %%a
  for /f "tokens=2*" %%a in ('
    reg query "%CurrentVersion%"^|find /i "ProductName"') do echo Product name:      %%b
  echo Processor arch:    %PROCESSOR_ARCHITECTURE% and %Bitness% bit Windows&echo(
  echo Date format:       %format% =  %date%
  echo Time format:       %hours% hours =  %time%
  for /f "tokens=3" %%a in ('reg query "%CodePage%" /v "OEMCP"') do (
  for /f "tokens=3" %%b in ('reg query "%CodePage%" /v "ACP"')   do (
  echo Extensions:        system:%es%  user:%eu%
  echo Delayed expansion: system:%ds%  user:%du%
  echo Locale name:       %LocaleName%      Code Pages:   OEM %%a     ANSI %%b))
  echo DIR  format:       %DirFormat%
  echo [/code^]
)
clip >nul 2>&1 && clip < "%temp%\info.txt"
start "" notepad.exe "%temp%\info.txt"
>nul ping -n 2 localhost
del "%temp%\info.txt"
Last edited by aGerman on 17 Aug 2016 11:33, edited 1 time in total.

aGerman
Expert
Posts: 3515
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#2 Post by aGerman » 11 Aug 2016 00:48

Some explanations about the data that the script collects.
- Windows version, name and bitness tell us something about available tools, folder structure, available environment variables, potential behavior of the script.
- Date/time formats are language dependent. Order, separators and number of tokens may differ. But often users ask for help how to parse.
- Usually command extensions are enabled and usually we assume that they are enabled. Nevertheless they could be disabled by changes in the registry. Things like FOR /F, IF /I, SET /P wouldn't work.
- If delayed variable expansion is enabled via registry you would have side effects if strings contain exclamation marks.
- The locale name tells us something about potential non-ASCII characters in the questioners environment.
- OEMCP and ACP may differ. Thus, the character encoding of the batch window and of the script may differ too.
- Users ask how to parse the output of DIR. Again date/time format and number of tokens may differ.

What we are NOT interested in are things like user name, IP addresses and any kind of sensitive data.

Squashman
Expert
Posts: 4071
Joined: 23 Dec 2011 13:59

Re: Creating a script to gather PC information - to assist those asking for help

#3 Post by Squashman » 11 Aug 2016 07:13

The systeminfo command can get you a lot of information as well.

SIMMS7400
Posts: 423
Joined: 07 Jan 2016 07:47

Re: Creating a script to gather PC information - to assist those asking for help

#4 Post by SIMMS7400 » 11 Aug 2016 08:58

Nice script!

I use the system info command in a script I run.

However, I've incorporated parts of your script into mine - thanks!

Note, computername/hostname, logon server, and domain will be displayed in this script.

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion

REM --
REM -- SET VARIABLES --
REM --
set system=
set manufacturer=
set model=
set serialnumber=
set osname=
set sp=
setlocal ENABLEDELAYEDEXPANSION
set "volume=C:"
set totalMem=
set availableMem=
set usedMem=

set "International=HKCU\Control Panel\International"
set "CurrentVersion=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
set "CodePage=HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage"
set "CMDproc=Software\Microsoft\Command Processor"

set bitness=32
if exist "%SystemRoot%\SysWOW64\" set bitness=64

for /f "tokens=1,2,*" %%a in ('reg query "%International%"^|find "REG_"')  do set "%%a=%%c"
if "%iDate%"=="0" (set format=mm/dd/yy) else if "%iDate%"=="1" (set format=dd/mm/yy) else if "%iDate%"=="2" (set format=yy/mm/dd)
if "%iTime%"=="0" (set hours=12) else if "%iTime%"=="1" (set hours=24)

for /f "delims=" %%a in ('dir "%SystemDrive%\pagefile.sys" /a ^|find ":"') do set DirFormat=%%a
for %%a in (es eu ds du) do set "%%a=Disabled"

reg query "HKLM\%CMDproc%" /v "EnableExtensions" 2>nul|find "0x1">nul && set "es=Enabled "
reg query "HKCU\%CMDproc%" /v "EnableExtensions" 2>nul|find "0x1">nul && set "eu=Enabled "
reg query "HKLM\%CMDproc%" /v "DelayedExpansion" 2>nul|find "0x1">nul && set "ds=Enabled "
reg query "HKCU\%CMDproc%" /v "DelayedExpansion" 2>nul|find "0x1">nul && set "du=Enabled "

echo      \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
echo      \\                                     \\
echo      \\              WELCOME                \\
echo      \\                                     \\
echo      \\             DOS TIPS                \\
echo      \\                                     \\
echo      \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\   
echo.
echo Retrieving Data [Computer: %computername%]...
echo.
echo Please Wait....
echo.
echo The first section of output is a quick reference
echo The second section of output contains all System Info
echo.

REM Get Computer Name
FOR /F "tokens=2 delims='='" %%A in ('wmic OS Get csname /value') do SET system=%%A

REM Get Computer Manufacturer
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Manufacturer /value') do SET manufacturer=%%A

REM Get Computer Model
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

REM Get Computer Serial Number
FOR /F "tokens=2 delims='='" %%A in ('wmic Bios Get SerialNumber /value') do SET serialnumber=%%A

REM Get Memory
FOR /F "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)
set totalMem=%totalMem:,=%
set availableMem=%availableMem:,=%
set /a usedMem=totalMem-availableMem

FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
    SET "diskfree=!disktotal!"
    SET "disktotal=!diskavail!"
    SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
 
>"%temp%\info.txt" (

echo ------------------------
echo System Name: %system%
echo ------------------------
echo.
echo Manufacturer:      %manufacturer%
echo Model:             %model%
echo Serial Number:     %serialnumber%
echo.
for /f "delims=" %%a in ('ver') do echo Windows version:   %%a
for /f "tokens=2*" %%a in ('
reg query "%CurrentVersion%"^|find /i "ProductName"') do echo Product name:      %%b
echo Processor arch:    %PROCESSOR_ARCHITECTURE% and %Bitness% bit Windows&echo(
echo Service Pack:      %sp%
echo.
echo Date format:       %format% =  %date%
echo Time format:       %hours% hours =  %time%
echo.
for /f "tokens=3" %%a in ('reg query "%CodePage%" /v "OEMCP"') do (
for /f "tokens=3" %%b in ('reg query "%CodePage%" /v "ACP"')   do (
echo Extensions:        system:%es%  user:%eu%
echo Delayed expansion: system:%ds%  user:%du%
echo Locale name:       %LocaleName%      Code Pages:   OEM %%a     ANSI %%b))
echo.
echo DIR  format:       %DirFormat%
echo.
echo -----------------
echo -- DISK SPACE ---
echo -----------------
echo C:\ Total: %disktotal:~0,-9% GB
echo C:\ Avail: %diskavail:~0,-9% GB
echo.
echo -------------------
echo    -- MEMORY --
echo -------------------
echo Total Memory: %totalMem%
echo Used  Memory: %usedMem%

echo.
echo --------------------------------------------
echo --------------ALL SYSTEM INFO---------------
echo --------------------------------------------
systeminfo /S %computername%
echo --------------------------------------------

)
clip >nul 2>&1 && clip < "%temp%\info.txt"
start "" notepad.exe "%temp%\info.txt"
>nul ping -n 2 localhost
del "%temp%\info.txt"

aGerman
Expert
Posts: 3515
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#5 Post by aGerman » 11 Aug 2016 11:46

@Squashman
SYSTEMIINFO is one of the damn commands that is completely language dependent :(

Code: Select all

Hostname:                                      DESKTOP-2KLLDT9
Betriebssystemname:                            Microsoft Windows 10 Home
Betriebssystemversion:                         10.0.10586 Nicht zutreffend Build 10586
Betriebssystemhersteller:                      Microsoft Corporation
Betriebssystemkonfiguration:                   Eigenständige Arbeitsstation
... etc.

@SIMMS7400
Nice script :)
Although something like

Code: Select all

...
-------------------
   -- MEMORY --
-------------------
Total Memory: ,=
Used  Memory: 0
...

... is exactly what we try to avoid :wink:

~~~~~~~~~~~~~

The purpose of the script shall be to collect data that would help to notice the differences of language settings and Windows versions. Hence the question is what informations would help and what commands and algorithms (that should be as downward compatible as possible) are suitable to gather these informations in a language independent manner.
I've seen people from all over the world here at DosTips. Most of them don't tell us where they are from or what OS they use. The consequence is that we guess something and afterwards the only reaction from the questioner is "the script doesn't work". That's the point where we usually begin with endless inquiries ... :cry:
However. Just to be clear - it's NOT the goal to collect as many informations as possible.

What do you think - are there informations missing in the script above that would help to give the questioners from Russia, China, Italy, ... a good solution for their problems?

Regards
aGerman

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Creating a script to gather PC information - to assist those asking for help

#6 Post by douglas.swehla » 11 Aug 2016 17:27

Getting the Windows version is great, since it tells us what commands should be available on their system. To further that, maybe have a sticky post with a list of what commands should be available on which OS, for reference by helpers.

What should be true often isn't, though, so maybe collect some "make sure things aren't broken" data?
  • Get the values of some key environment variables, like Path and PATHEXT, to make sure they're not corrupted.
  • Check for the existence of utilities that are version-dependent, like CHOICE and SLEEP.
  • Confirm that commonly used or recommended utilities are present and findable by running WHERE on them. Check for cscript, find, findstr, net, ping, etc.

You could add a call to "secondary.bat" or to "%~1", so that helpers can include additional, question-specific tests to run. You'd probably want to draw a clear distinction between the official, "DosTips-approved" script, and the unofficial, "run it if you trust them" script(s). Dunno how feasible that is, if we're worried about language issues in the first place.

The ever-popular TRON script (https://www.reddit.com/r/TronScript/, https://github.com/bmrf/tron/blob/master/tron.bat) checks for a lot of environment stuff in the prep stages, you might find something interesting in there.

You can try to launch an elevated prompt, to see if user has admin rights.

penpen
Expert
Posts: 1650
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#7 Post by penpen » 11 Aug 2016 17:32

If i remember right:
If one of the international settings is not present in "HKCU\Control Panel\International",
then its value is read from "HKU\.DEFAULT\Control Panel\International" if present,
else the default values are used:
- iDate: 0
- iTime: 1
- ...


penpen

jacobcarl
Posts: 1
Joined: 12 Aug 2016 03:34
Location: Bangalore
Contact:

Re: Creating a script to gather PC information - to assist those asking for help

#8 Post by jacobcarl » 12 Aug 2016 03:49

i am looking for Dos code which can retrieve system information of the graphics card as well as mother board.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Creating a script to gather PC information - to assist those asking for help

#9 Post by foxidrive » 12 Aug 2016 07:43

jacobcarl wrote:i am looking for Dos code which can retrieve system information of the graphics card as well as mother board.


Try this post.

viewtopic.php?p=48394#p48394

aGerman
Expert
Posts: 3515
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#10 Post by aGerman » 12 Aug 2016 10:49

douglas.swehla wrote:
  • Get the values of some key environment variables, like Path and PATHEXT, to make sure they're not corrupted.

OK, I agree. Although most of the time predefined variables are rather overwritten than corrupted. In that case you have to know the complete batch code of the questioner. Outputting these values by a separate script wouln't help.

douglas.swehla wrote:
  • Check for the existence of utilities that are version-dependent, like CHOICE and SLEEP.
  • Confirm that commonly used or recommended utilities are present and findable by running WHERE on them. Check for cscript, find, findstr, net, ping, etc.

We should already know what tools are available if we know the Windows version. That's the reason why I preferred this version check instead of verifying the existence of each single tool. On the other hand those tools could have been already downloaded. But at what point would you stop? CHOICE, SLEEP, WMIC, DEVCON, ...? I think the questioner would get a proper error message if something is missing.

douglas.swehla wrote:You could add a call to "secondary.bat" or to "%~1", so that helpers can include additional, question-specific tests to run. You'd probably want to draw a clear distinction between the official, "DosTips-approved" script, and the unofficial, "run it if you trust them" script(s). Dunno how feasible that is, if we're worried about language issues in the first place.

Good idea. If we assume a specific problem we could ask the questioner to run a specific script instead of including everything into one. Too much information could be more confusing than helpful.

douglas.swehla wrote:The ever-popular TRON script (https://www.reddit.com/r/TronScript/, https://github.com/bmrf/tron/blob/master/tron.bat) checks for a lot of environment stuff in the prep stages, you might find something interesting in there.

He's in a different league :lol:

douglas.swehla wrote:You can try to launch an elevated prompt, to see if user has admin rights.

I'd like to work around

Code: Select all

net localgroup
net user %username%

Unfortunately NET is another damn language dependent command ... :(

penpen wrote:then its value is read from "HKU\.DEFAULT\Control Panel\International" if present

Would you say we should try to read these values alternatively?

Regards
aGerman

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Creating a script to gather PC information - to assist those asking for help

#11 Post by douglas.swehla » 12 Aug 2016 15:09

aGerman wrote:
douglas.swehla wrote:
  • Get the values of some key environment variables, like Path and PATHEXT, to make sure they're not corrupted.

OK, I agree. Although most of the time predefined variables are rather overwritten than corrupted. In that case you have to know the complete batch code of the questioner. Outputting these values by a separate script wouln't help.
Overwritten is a better word choice than corrupted, thank. Not sure what you mean by the last couple sentences. To be clear, I'm thinking of situations where the asker thinks they're having trouble composing some command, when the real problem is that no commands are running at all, because they overwrote the PATH with some working directory, instead of appending it.

aGerman wrote:
douglas.swehla wrote:
  • Check for the existence of utilities that are version-dependent, like CHOICE and SLEEP.
  • Confirm that commonly used or recommended utilities are present and findable by running WHERE on them. Check for cscript, find, findstr, net, ping, etc.

We should already know what tools are available if we know the Windows version. That's the reason why I preferred this version check instead of verifying the existence of each single tool. On the other hand those tools could have been already downloaded. But at what point would you stop? CHOICE, SLEEP, WMIC, DEVCON, ...? I think the questioner would get a proper error message if something is missing.

It's been my experience that "what should be" and "what is" often have little or nothing to do with each other. :|

The idea here is to prevent helpers from suggesting solutions that the user doesn't have. Sure, most of us know that if they're on XP, they don't have CHOICE natively, but maybe (like you pointed out) they installed it. Some helpers don't have a good grip on which commands are available on which systems. Maybe one or more of the normal commands got moved/deleted/renamed/corrupted. I'm thinking of testing a list of, say, 20 of the most commonly used commands, and only mention them if any aren't found:

Code: Select all

Command Availability
------------------------
CHOICE is not found on this system.
WMIC is not found on this system.


aGerman wrote:
douglas.swehla wrote:You could add a call to "secondary.bat" or to "%~1", so that helpers can include additional, question-specific tests to run. You'd probably want to draw a clear distinction between the official, "DosTips-approved" script, and the unofficial, "run it if you trust them" script(s). Dunno how feasible that is, if we're worried about language issues in the first place.

Good idea. If we assume a specific problem we could ask the questioner to run a specific script instead of including everything into one. Too much information could be more confusing than helpful.

It's funny, I thought this would be the one to get shot down. Now I think about it, this would be a good place to test for particular utilities. You could have a few core ones, or none, in the main script, and if helper thinks a good solution would involve CHOICE or DEVCON or WEVTUTIL, they can check for it.

I keep harping on CHOICE because it's so useful, and freaking MS took it out, but then they put it back, but you can get if you want, but a lot people don't know that, but it shouldn't matter because people are mostly off XP now, but a lot aren't, and I just never know if I can suggest it or not. If you kick out the rest of the "check for utilities" idea, can we at least keep that? Just a line saying "The CHOICE command is[ not] available."

aGerman wrote:
douglas.swehla wrote:The ever-popular TRON script (https://www.reddit.com/r/TronScript/, https://github.com/bmrf/tron/blob/master/tron.bat) checks for a lot of environment stuff in the prep stages, you might find something interesting in there.

He's in a different league :lol:

He's definitely a smart guy. Not actually into exotic scripting, though--that tool started out as just a traditional batch script, running one cleanup utility after another, with nothing esoteric at all. A lot of people have contributed to it over the years, and he's great about accepting and acknowledging help. If you want to help, you probably can.

aGerman wrote:
douglas.swehla wrote:You can try to launch an elevated prompt, to see if user has admin rights.

I'd like to work around

Code: Select all

net localgroup
net user %username%

Unfortunately NET is another damn language dependent command ... :(

Is this to get a list of possible admin names? Every system I've worked with recently has an "administrator" user. Not sure if that's built-in or added by convention. If it's a default, you could do something like this:

Code: Select all

rem :: Test for current admin status. Any of these commands will return an error if not in admin mode.
fsutil /?
net file
cacls.exe "%SYSTEMROOT%\system32\config\system"
cacls.exe "%SYSTEMROOT%\SysWOW64\config\system"

rem :: If not in admin mode, see if we can get there. Will require user to enter password.
runas /user:administrator "cmd /c exit"


Some resources on testing for and getting to elevated status. These rely on UAC &/or VBS.

aGerman
Expert
Posts: 3515
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#12 Post by aGerman » 12 Aug 2016 16:09

douglas.swehla wrote:Not sure what you mean by the last couple sentences.

Imagine the questioners script would look like that

Code: Select all

@echo off &setlocal
set /p "path=Enter the path: "
echo "%path%"|find "C:"
pause

This will crash for sure. But it wouldn't even help if you check the path variable in another script. Most likely it would be ok.

douglas.swehla wrote:I'm thinking of testing a list of, say, 20 of the most commonly used commands, and only mention them if any aren't found

Yeah. Maybe something like that:

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion
for %%i in (
  findstr wmic foo
) do for /f "tokens=1,2 delims=?" %%j in ("%%i.exe?%%i.com") do if "%%~$PATH:j%%~$PATH:k"=="" echo %%i is missing
pause


douglas.swehla wrote:Is this to get a list of possible admin names?

I actually didn't want the questioner to run anything as admin only to check if he is able to obtain elevated permissions. That's what I was thinking about:

Code: Select all

set "check="
for /f "tokens=*" %%a in ('net localgroup 2^>nul^|findstr /ic:"admin"') do (
  set "check=1"
  net user %username%|findstr /c:"%%a">nul &&(
    echo You belong to the local admins.
  )||(
    echo You don't belong to the local admins.
  )
)
if not defined check echo You have no permissions to run the NET command.

The local group I belong to is *Administratoren. I'm looking for "admin". So probably the code will work for you as well. But with something like "администраторы" it would fail.

Regards
aGerman

penpen
Expert
Posts: 1650
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#13 Post by penpen » 12 Aug 2016 16:27

aGerman wrote:
penpen wrote:then its value is read from "HKU\.DEFAULT\Control Panel\International" if present

Would you say we should try to read these values alternatively?
Yes, but only if not defined in "HKCU\..."; something like that:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for %%a in ("iDate" "iTime") do set "%%~a="
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International" /v "i???e" ^| find "REG_"') do set "%%~a=%%~c"
for /f "tokens=1,2*" %%a in ('reg query "HKU\.DEFAULT\Control Panel\International" /v "i???e" ^| find "REG_"') do if not defined %%~a set "%%~a=%%~c"
if not defined iDate set "iDate=0"
if not defined iTime set "iTime=1"

set iDate
set iTime

endlocal


penpen

Edit: Added set-/endlocal, and initializing of iDate and iTime.
Last edited by penpen on 12 Aug 2016 18:51, edited 1 time in total.

aGerman
Expert
Posts: 3515
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a script to gather PC information - to assist those asking for help

#14 Post by aGerman » 12 Aug 2016 16:38

Cool. That can be easily implemented :)

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Creating a script to gather PC information - to assist those asking for help

#15 Post by douglas.swehla » 12 Aug 2016 19:45

aGerman wrote:Imagine the questioners script would look like that

Code: Select all

@echo off &setlocal
set /p "path=Enter the path: "
echo "%path%"|find "C:"
pause

This will crash for sure. But it wouldn't even help if you check the path variable in another script. Most likely it would be ok.

Ah, okay. You're thinking of the case when the questioner has overwritten the path in the script they're asking about. I'm thinking of the case where it's broken already: maybe someone ran SETX or messed up the registry, and PATH is either undefined or doesn't have System32 or SysWOW64 in it for some reason.

Another case is if they're trying to run a non-native program, and don't realize that they need to either specify the full path to the executable or have its directory stored in the PATH variable. In a recent thread, someone was jumping through hoops to find the full path of an executable on a remote system, when I think they could have just relied on that system's PATH. I asked about it, but never got a clear answer. If I had been able to see a report that said "User's PATH is '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\'.", then I'd have known how to proceed.

aGerman wrote:Maybe something like that:

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion
for %%i in (
  findstr wmic foo
) do for /f "tokens=1,2 delims=?" %%j in ("%%i.exe?%%i.com") do if "%%~$PATH:j%%~$PATH:k"=="" echo %%i is missing
pause


Yes, exactly. You could also add a snippet to check for the existence of bad or confusing executables. If someone has a local batch file with the same name as some arcane command, that could mess things up.

Code: Select all

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Make a bad file in a safe place for demo.
pushd "%userprofile%"
echo bad file > wmic.bat

for %%i in (findstr wmic foo) do (
   rem Check for missing good version.
   for /f "tokens=1,2 delims=?" %%j in ("%%i.exe?%%i.com") do (
      if "%%~$PATH:j%%~$PATH:k"=="" echo %%i not found as .exe or .com
   )
   rem Check for present bad version.
   for /f "tokens=* delims=" %%j in ('where %%i 2^>nul ^| findstr /i /v ".exe .com"') do (
      echo %%i found as "%%~j".
   )
)

rem Cleanup. Delete bad file.
del wmic.bat
popd

endlocal
exit /b



aGerman wrote:I actually didn't want the questioner to run anything as admin only to check if he is able to obtain elevated permissions.

Oh, agreed. That's why I did the immediate exit trick. I guess that's technically running something, but it's about as harmless as you can get.

aGerman wrote: That's what I was thinking about:

Code: Select all

set "check="
for /f "tokens=*" %%a in ('net localgroup 2^>nul^|findstr /ic:"admin"') do (
  set "check=1"
  net user %username%|findstr /c:"%%a">nul &&(
    echo You belong to the local admins.
  )||(
    echo You don't belong to the local admins.
  )
)
if not defined check echo You have no permissions to run the NET command.

The local group I belong to is *Administratoren. I'm looking for "admin". So probably the code will work for you as well. But with something like "администраторы" it would fail.

The language issue is tricky. I expect that between WMIC, VBscript, Jscript, and PowerShell, there's a language-independent way around that, though.

Post Reply