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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#46 Post by foxidrive » 18 Aug 2016 06:18

aGerman wrote:
foxidrive wrote:The number of cores only gives an indication of the machine capability.

Hmm. The number of cores is rather only one of many indicators.

Yes, and my point is still valid. It doesn't give a definitive assessment of the capability of the machine, just an indication.

This may just be subtle use of language that wasn't as clear to you
Thus, I think Compo's objection is valid. That's the reason why I already left it out.

I've said that it's not something that I am concerned about.

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

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

#47 Post by penpen » 18 Aug 2016 07:08

aGerman wrote:This line

Code: Select all

%wmic% path Win32_Group WHERE "LocalAccount='TRUE' AND SID='S-1-5-32-544'" assoc /assocclass:Win32_GroupUser|>nul %findstr% /c:Name^=\"%username%\" && set "LocalAdmin=Yes"
took more than half an hour on my PC at work. Probably because of thousands of users in the domain the associated Win32_GroupUser is terribly slow.
Does anybody know of a better way?
I'm not sure if the following is faster (could be because typically each user has tons of associated data).
I'm also unsure what exactly you are searching for; if i understand it right, then you check, if the current user is a local admin or not.
The following works for windows 10, but must be tested on other versions of windows; i#ve marked the critical point:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "adminGroupName="
for /F "skip=1 tokens=* delims=" %%a in ('wmic group where "LocalAccount='TRUE' AND SID='S-1-5-32-544'" get Name') do if not defined adminGroupName set "adminGroupName=%%~a:"
:: i'm not sure if there are always 3 characters appended (like Windows 10), so have to check this for other Versions of windows
set "adminGroupName=%adminGroupName:~0,-4%"
echo adminGroupName="%adminGroupName%"

2>nul wmic path win32_groupuser where (GroupComponent="win32_Group.Name=\"Administratoren\",Domain=\"%userDomain%\"" AND PartComponent="Win32_UserAccount.Name=\"%userName%\",Domain=\"%userDomain%\"") | (
   findstr /L /I /C:"%userName%" && (
      echo The user "%userName%" is a local admin on domain "%userDomain%".
   ) || (
      echo The user "%userName%" is no local admin on domain "%userDomain%".
   )
)

endlocal
goto :eof


penpen

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

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

#48 Post by aGerman » 18 Aug 2016 07:38

foxidrive wrote:It doesn't give a definitive assessment of the capability of the machine, just an indication.

I think number of cores together with the RAM space could be valuable in this case.

Code: Select all

wmic os GET TotalVisibleMemorySize /value


penpen wrote:I'm not sure if the following is faster

Not quite. It's querying the Win32_GroupUser class that isn't optimized. I decided to use WMIC path Win32_Group WHERE "LocalAccount='TRUE' AND SID='S-1-5-32-544'" to get the admin group name and try to find it in the output of NET USER %USERNAME%. The code was already updated... Thanks anyway!

penpen wrote:i'm not sure if there are always 3 characters appended

I always use the /value option in order to have name and value in only one line. I never saw any appended characters as in the list format.

Steffen

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

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

#49 Post by SIMMS7400 » 18 Aug 2016 08:01

Very interesting reads!

Here's another portion of a script I use:

Code: Select all

echo INFO: Getting information about Java ...
echo Java Information on Computer: %COMPUTERNAME%> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Current Date and Time: %DATE% %TIME%>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo ================================================================================>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Java JDKs installed:>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit" /s >>  "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Java JREs installed:>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /s >>  "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Where is java.exe?>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
where java >> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo.>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Java.exe version information:>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
java -version 2>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo.>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Java related environment variables (may not be set):>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo   %%JAVA_HOME%%=%JAVA_HOME%>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo   %%CLASSPATH%%=%CLASSPATH%>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo.>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Folders under "%%PROGRAMFILES%%\Java":>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
dir /b "%PROGRAMFILES%\Java" >> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo.>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
echo Folders under "%%ProgramFiles(x86)%%\Java":>> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"
dir /b "%ProgramFiles(x86)%\Java" >> "%userprofile%\%SaveFolderName%\javainfo_%COMPUTERNAME%.txt"


Not sure if this would benefit your purpose, but figured I'd share. Thanks!

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

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

#50 Post by aGerman » 18 Aug 2016 10:22

@SIMMS7400
Thanks! That's probably interesting for some people. Although as you already suspected it's not the purpose of the script to collect data about 3rd party applications.

~~~~~~~~~~~~
Added %NUMBER_OF_PROCESSORS% again. Also included the RAM space.

BTW: I linked the latest version of the script in the initial post.

Compo
Posts: 488
Joined: 21 Mar 2014 08:50

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

#51 Post by Compo » 18 Aug 2016 10:24

I have always used a Query of HKU\S-1-5-19 using Reg.exe in order to check if the user is admin, (also used earlier by foxidrive).
As a result of Dave Benham's inability to run reg.exe but ability to run WMIC then what about:

Code: Select all

@Echo Off
SetLocal
(Set Admin=No)
(Set NCC=/NameSpace:\\root\default Class StdRegProv Call CheckAccess)
(Set DSR=hDefKey="&H80000003" sSubKeyName="S-1-5-19" uRequired="&H1")
WMIC %NCC% %DSR%|Find "TRUE;">Nul 2>&1&&(Set Admin=Yes)
Set Admin
Pause
This should check if the current user has read access to the 'Special' LOCAL SERVICE Group

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

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

#52 Post by aGerman » 18 Aug 2016 10:57

Compo wrote:I have always used a Query of HKU\S-1-5-19

It's useful to have a plan B. I'll include this registry check using JScript as an alternative.
Although this is only another way to investigate if the user runs the script with administrative elevation. Do you know of another way to check if the user is able to gain elevation (without running the script elevated)?

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

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

#53 Post by foxidrive » 18 Aug 2016 11:34

SIMMS7400 wrote:but figured I'd share.


Here are a few changes to your code showing a different way to arrange it. You'll find the ) are changed to ^) to use this style of redirecting into a file.

One benefit here is that the code becomes a good deal easier to read.

Code: Select all

@echo off
set "file=%userprofile%\javainfo_%COMPUTERNAME%.txt"
(
echo INFO: Getting information about Java ...
echo Java Information on Computer: %COMPUTERNAME%
echo Current Date and Time: %DATE% %TIME%
echo ================================================================================
echo Java JDKs installed:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit" /s
echo Java JREs installed:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /s
echo Where is java.exe?
where java
echo.
echo Java.exe version information:
java -version 2>&1
echo.
echo Java related environment variables (may not be set^):
if defined JAVA_HOME echo %%JAVA_HOME%%=%JAVA_HOME%
if defined CLASSPATH echo %%CLASSPATH%%=%CLASSPATH%
echo.
if exist "%%PROGRAMFILES%%\Java\" echo Folders under "%%PROGRAMFILES%%\Java":
if exist "%%PROGRAMFILES%%\Java\" dir /b "%PROGRAMFILES%\Java"
if exist "%%ProgramFiles(x86^)%%\Java\" echo Folders under "%%ProgramFiles(x86^)%%\Java":
if exist "%%ProgramFiles(x86^)%%\Java\" dir /b "%ProgramFiles(x86^)%\Java"
) >"%file%"

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

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

#54 Post by douglas.swehla » 18 Aug 2016 16:11

aGerman wrote:
penpen wrote:I'm not sure if the following is faster

Not quite. It's querying the Win32_GroupUser class that isn't optimized. I decided to use WMIC path Win32_Group WHERE "LocalAccount='TRUE' AND SID='S-1-5-32-544'" to get the admin group name and try to find it in the output of NET USER %USERNAME%. The code was already updated... Thanks anyway!

penpen wrote:i'm not sure if there are always 3 characters appended

I always use the /value option in order to have name and value in only one line. I never saw any appended characters as in the list format.


I'm running Windows 7 Pro on a laptop connected to a large network. When disconnected, commands for the Win32_GroupUser class run fine. When connected, they takes hours, and likewise for Win32_UserAccount. My research says that 'wmic /node:"%computername%" path ...' or 'wmic /node:localhost path ...' ought to limit the search to the local machine, but they have no apparent effect.

I've found a solution that's pretty ugly, but works, and also addresses a weird scenario that hasn't been brought up yet. Supposing that my computer name is "MyComputer", my user name is "douglas", and the user name of an administrator who has logged in to MyComputer is "smith". When I run "net user", I get this output:

Code: Select all


User accounts for \\MyComputer

-------------------------------------------------------------------------------
administrator            douglase945iz6wtitsr     douglasxwhmq2q508hge     
guest                    smith74                 smith75                 
The command completed successfully.



There is no "douglas" account, according to NET. If I run 'net user douglas' I get "The user name could not be found." We could extract the account names that start with my login name, but then we'd have to either repeat the following code for each one, or decide between them, and I have no idea what the criteria would be.

The solution below relies on GPRESULT, which has been available since XP SP2, but only became standard in Windows Server 2003, so we might have compatibility issues with XP, but I figure we have the same issues with WMIC, so it's pretty much break even. This runs in about 40 seconds for me.

Code: Select all

@echo off
setlocal
::cls

set "start_time=%time%"

rem :: Get name of admin group, based on SID (language-independent)
for /f "usebackq skip=1" %%F in (
   `wmic group where "LocalAccount='TRUE' AND SID='S-1-5-32-544'" get Name /format:table ^| findstr /i /r "[a-z0-9]"`
) do set "admin_group_name=%%F"
::pause

rem :: Group membership is listed at the end of the RSOP report from GPRESULT.
rem :: Each major heading is underlined with a series of dashes.
rem :: I don't know if the output of the report is language-dependent,
rem :: so instead of looking for the heading, we look for the dashes,
rem :: and store the line number, which tells us where the info starts.
for /f "delims=[]" %%F in ('gpresult /r /scope user ^| find /n "--------"') do set /a _lines=%%F

rem :: Now that we know where the data starts, we can skip down to there,
rem :: and trust that we won't be examining irrelevant lines that might match.
rem :: For some reason, piping the output directly to FIND or FINDSTR
rem :: isn't working for me (it may have something to do with extra CR chars),
rem :: so I'm writing it to a temp text file first.
set "user_groups=%temp%\user_groups.txt"
(
   for /f "skip=%_lines% tokens=* delims=" %%F in ('gpresult /r /scope user') do @echo %%F
) > "%user_groups%"
::pause

rem :: Read the temp file and filter for admin group name.
rem :: If it's found, set the admin flag to true.
rem :: My normal account isn't admin, so I've added the Everyone account
rem :: as a backup test, just to be sure it all worked.
type "%user_groups%" | find "%admin_group_name%" && set "LocalAdmin=true"
type "%user_groups%" | find "Everyone" && set "EveryoneAccount=true"
::pause

rem :: Check result.
set LocalAdmin
set EveryoneAccount
::pause

echo\
echo end: %time%
echo beg: %start_time%

rem :: Clean up.
del "%user_groups%"

endlocal
exit /b


I haven't run into the extra characters that penpen mentioned, and I can't speak to the registry workaround that compo brought up.

For what it's worth, this guy ran into a similar "running forever when on a network" problem, and says he solved it by resorting to PowerShell. Unfortunately, he doesn't say exactly what the solution was. My guess is just re-writing the original command in PS format, but I haven't tried that.

I found a page that mentioned high resource usage from WMI (the system, not WMIC the tool) under certain conditions, and suggested using get-object instead of... whatever the alternative was. I can't find the page again to save my life, so I'm just going off memory.

The point of the last two paragraphs is that there may be an issue with the WMIC tool, or with WMIC on Windows 7/8, rather than with WMI itself. You seem comfortable with the other Windows scripting languages, so if you haven't already, I suggest trying the 'assoc /assocclass:Win32_GroupUser' command using one of them, possibly in combination with "/node", and see if that solves the hanging issue. If it does, then I think you've also got a solution that works with XP, since those are older technologies.

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

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

#55 Post by aGerman » 18 Aug 2016 17:17

Thanks for reporting the bug!

douglas.swehla wrote:GPRESULT

Nice find Douglas! I'll try it at work tomorrow to see how it works there.

You stepped into the MUI trap btw. Just posting the last few lines of
gpresult /r /scope user

Code: Select all

    Der Benutzer ist Mitglied der folgenden Sicherheitsgruppen
    ----------------------------------------------------------
        Kein
        Jeder
        Lokales Konto und Mitglied der Gruppe "Administratoren"
        Administratoren
        Leistungsprotokollbenutzer
        Benutzer
        INTERAKTIV
        KONSOLENANMELDUNG
        Authentifizierte Benutzer
        Diese Organisation
        Lokales Konto
        LOKAL
        NTLM-Authentifizierung
        Hohe Verbindlichkeitsstufe

Your "Everyone" is my "Jeder" :lol: Fortunately at least "Administratoren" is what WMIC GROUP outputs too.

Steffen

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

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

#56 Post by penpen » 18 Aug 2016 18:42

I remembered an old way to detect if the actual user has admin rights (after i've seen the "NET USER %USERNAME%" command; should be the fastest):

Code: Select all

>nul 2>nul net session && echo Hello admin. || echo Hello noadmin.


penpen

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

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

#57 Post by aGerman » 18 Aug 2016 18:58

Haha yes. That's what I already use.
But again NET SESSION does only succeed if the script runs elevated. What I'm after is a way to determine if the user would be able to gain elevation even if the script wasn't run as admin. That's the reason why I tried to figure out if the user belongs to the local admin group :wink:

Steffen

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

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

#58 Post by penpen » 18 Aug 2016 19:05

Oh, :oops: ... somehow i missed that (although i read the script... - too late i guess... better i say gn8 for now :) )
Sorry!

penpen

SirJosh3917
Posts: 35
Joined: 02 May 2016 18:59

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

#59 Post by SirJosh3917 » 18 Aug 2016 19:07

penpen wrote:I remembered an old way to detect if the actual user has admin rights (after i've seen the "NET USER %USERNAME%" command; should be the fastest):

Code: Select all

>nul 2>nul net session && echo Hello admin. || echo Hello noadmin.


penpen


I have a "faster" but perhaps "less accurate" method of checking if a user is an admin,

Code: Select all

::Run this piece of code when you want to check if the user is an admin

set Admin=Normal User
:IsUserAdmin
set FolderId=%random%%random%
IF EXIST "C:\Windows\System32\%FolderID%" goto :IsUserAdmin
mkdir "C:\Windows\System32\%FolderID%"
IF EXIST "C:\Windows\System32\%FolderID%" set Admin=Administrator
set FolderId=

::And the result
echo You are a(n) %Admin%
::%Admin% is "Normal User" if a normal user, and "Administrator" if an administrator.

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

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

#60 Post by aGerman » 19 Aug 2016 03:39

@Douglas
GPRESULT works fine on my PC at work too. I'll implement it when I'm back home this evening. I have some ideas how to avoid running the tool twice ...

@SirJosh3917
Thank you! I think I should post a list somewhere of all the possibilities how to check if a user runs the script elevated :)

Steffen

Post Reply