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
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#31 Post by aGerman » 15 Aug 2016 14:23

@Dave
I remembered that the WMI provides the StdRegProv class. You may try it at work.

JScript (that we may include into a hybrid script later on):
registry.js

Code: Select all

/*
http://msdn.microsoft.com/en-us/library/aa393664(v=vs.85).aspx
*/

/* Hives */
var HKCR = HKEY_CLASSES_ROOT   = 0x80000000;
var HKCU = HKEY_CURRENT_USER   = 0x80000001;
var HKLM = HKEY_LOCAL_MACHINE  = 0x80000002;
var HKU  = HKEY_USERS          = 0x80000003;
var HKCC = HKEY_CURRENT_CONFIG = 0x80000005;
/* Data Types */
var REG_NONE      =  0;
var REG_SZ        =  1;
var REG_EXPAND_SZ =  2;
var REG_BINARY    =  3;
var REG_DWORD     =  4;
var REG_MULTI_SZ  =  7;
var REG_QWORD     = 11;
/* Access Rights (can be combined with the | operator) */
var KEY_QUERY_VALUE        = 0x00000001; //Required to query the values of a registry key.
var KEY_SET_VALUE          = 0x00000002; //Required to create, delete, or set a registry value.
var KEY_CREATE_SUB_KEY     = 0x00000004; //Required to create a subkey of a registry key.
var KEY_ENUMERATE_SUB_KEYS = 0x00000008; //Required to enumerate the subkeys of a registry key.
var KEY_NOTIFY             = 0x00000010; //Required to request change notifications for a registry key or for subkeys of a registry key.
var KEY_CREATE             = 0x00000020; //Required to create a registry key.
var DELETE                 = 0x00010000; //Required to delete a registry key.
var READ_CONTROL           = 0x00020000; //Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.
var WRITE_DAC              = 0x00040000; //Required to modify the DACL in the object's security descriptor.
var WRITE_OWNER            = 0x00080000; //Required to change the owner in the object's security descriptor.

function regCheckAccess(strComputer, uHive, strRegPath, uRequiredAccess) {
  try {
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objAccessMethod = objReg.Methods_.Item("CheckAccess");
    var objAccessInParam = objAccessMethod.InParameters.SpawnInstance_();
    objAccessInParam.hDefKey = uHive;
    objAccessInParam.sSubKeyName = strRegPath;
    objAccessInParam.uRequired = uRequiredAccess;
    var objAccessOutParam = objReg.ExecMethod_(objAccessMethod.Name, objAccessInParam);
    if (objAccessOutParam.ReturnValue == 0) {
      if (objAccessOutParam.bGranted) {return true;}
    }
    return false;
  }
  catch(e) {return false;}
}

function regCreateKey(strComputer, uHive, strRegPath) {
  try {
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objCreateMethod = objReg.Methods_.Item("CreateKey");
    var objCreateInParam = objCreateMethod.InParameters.SpawnInstance_();
    objCreateInParam.hDefKey = uHive;
    objCreateInParam.sSubKeyName = strRegPath;
    var objCreateOutParam = objReg.ExecMethod_(objCreateMethod.Name, objCreateInParam);
    if (objCreateOutParam.ReturnValue == 0) {return true;}
    return false;
  }
  catch(e) {return false;}
}

function regWriteVal(strComputer, uHive, strRegPath, strValName, iType, vData) {
  try {
    var bRet = false;
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objCreateMethod = objReg.Methods_.Item("CreateKey");
    var objCreateInParam = objCreateMethod.InParameters.SpawnInstance_();
    objCreateInParam.hDefKey = uHive;
    objCreateInParam.sSubKeyName = strRegPath;
    var objCreateOutParam = objReg.ExecMethod_(objCreateMethod.Name, objCreateInParam);
    if (objCreateOutParam.ReturnValue != 0) {return false;}
    switch (iType) {
      case REG_SZ:
        var objSetMethod = objReg.Methods_.Item("SetStringValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.sValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      case REG_EXPAND_SZ:
        var objSetMethod = objReg.Methods_.Item("SetExpandedStringValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.sValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      case REG_BINARY:
        var objSetMethod = objReg.Methods_.Item("SetBinaryValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.uValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      case REG_DWORD:
        var objSetMethod = objReg.Methods_.Item("SetDWORDValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.uValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      case REG_MULTI_SZ:
        var objSetMethod = objReg.Methods_.Item("SetMultiStringValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.sValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      case REG_QWORD:
        var objSetMethod = objReg.Methods_.Item("SetQWORDValue");
        var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
        objSetInParam.hDefKey = uHive;
        objSetInParam.sSubKeyName = strRegPath;
        objSetInParam.sValueName = strValName;
        objSetInParam.uValue = vData;
        var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
        if (objSetOutParam.ReturnValue == 0) {bRet = true;}
        break;
      default:
        return false;
    }
    return bRet;
  }
  catch(e) {return false;}
}

function regReadVal(strComputer, uHive, strRegPath, strValName) {
  try {
    var vRet = null, iType = -1;
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objEnumMethod = objReg.Methods_.Item("EnumValues");
    var objEnumInParam = objEnumMethod.InParameters.SpawnInstance_();
    objEnumInParam.hDefKey = uHive;
    objEnumInParam.sSubKeyName = strRegPath;
    var objEnumOutParam = objReg.ExecMethod_(objEnumMethod.Name, objEnumInParam);
    if (objEnumOutParam.ReturnValue == 0) {
      if (objEnumOutParam.sNames != null) {
        for (var i = 0; i <= objEnumOutParam.sNames.ubound(); i++) {
          if (objEnumOutParam.sNames.getItem(i).toLowerCase() == strValName.toLowerCase()) {
            iType = objEnumOutParam.Types.getItem(i);
            break;
          }
        }
      }
      else {if (strValName == "") {iType = REG_NONE;}}
      if (iType == -1) {return null;}
    }
    else {return null;}
    switch (iType) {
      case REG_SZ:
        var objGetMethod = objReg.Methods_.Item("GetStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
        break;
      case REG_EXPAND_SZ:
        var objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
        break;
      case REG_BINARY:
        var objGetMethod = objReg.Methods_.Item("GetBinaryValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray();}}
        break;
      case REG_DWORD:
        var objGetMethod = objReg.Methods_.Item("GetDWORDValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
        break;
      case REG_MULTI_SZ:
        var objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray();}}
        break;
      case REG_QWORD:
        var objGetMethod = objReg.Methods_.Item("GetQWORDValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
        break;
      case REG_NONE:
        var objGetMethod = objReg.Methods_.Item("GetStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetBinaryValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray(); break;}}
        objGetMethod = objReg.Methods_.Item("GetDWORDValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray(); break;}}
        objGetMethod = objReg.Methods_.Item("GetQWORDValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
      default:
        return null;
    }
    return vRet;
  }
  catch(e) {return null;}
}

function regDelKey(strComputer, uHive, strRegPath) {
  try {
    var iRet = 0;
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objEnumMethod = objReg.Methods_.Item("EnumKey");
    var objEnumInParam = objEnumMethod.InParameters.SpawnInstance_();
    objEnumInParam.hDefKey = uHive;
    objEnumInParam.sSubKeyName = strRegPath;
    var objEnumOutParam = objReg.ExecMethod_(objEnumMethod.Name, objEnumInParam);
    if (objEnumOutParam.ReturnValue == 0) {
      if (objEnumOutParam.sNames != null) {
        for (var i = 0; i <= objEnumOutParam.sNames.ubound(); i++) {
          var strNewPath = strRegPath + "\\" + objEnumOutParam.sNames.getItem(i);
          iRet += regDelKey(strComputer, uHive, strNewPath);
        }
      }
      var objDelMethod = objReg.Methods_.Item("DeleteKey");
      var objDelInParam = objDelMethod.InParameters.SpawnInstance_();
      objDelInParam.hDefKey = uHive;
      objDelInParam.sSubKeyName = strRegPath;
      var objDelOutParam = objReg.ExecMethod_(objDelMethod.Name, objDelInParam);
      iRet = (objDelOutParam.ReturnValue == 0) ? ++iRet : -1;
    }
    return iRet;
  }
  catch(e) {return -1;}
}

function regDelVal(strComputer, uHive, strRegPath, strValName) {
  try {
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objDelMethod = objReg.Methods_.Item("DeleteValue");
    var objDelInParam = objDelMethod.InParameters.SpawnInstance_();
    objDelInParam.hDefKey = uHive;
    objDelInParam.sSubKeyName = strRegPath;
    objDelInParam.sValueName = strValName;
    var objDelOutParam = objReg.ExecMethod_(objDelMethod.Name, objDelInParam);
    if (objDelOutParam.ReturnValue == 0) {return true;}
    return false;
  }
  catch(e) {return false;}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var sCompName = ".";

if (regCheckAccess(sCompName, HKCU, "Software\\Microsoft\\Command Processor", KEY_QUERY_VALUE)) {
  WScript.Echo("Extensions (User)\n" + regReadVal(sCompName, HKCU, "Software\\Microsoft\\Command Processor", "EnableExtensions"));
} else {
  WScript.Echo("HKCU Access Denied");
}

if (regCheckAccess(sCompName, HKLM, "Software\\Microsoft\\Command Processor", KEY_QUERY_VALUE)) {
  WScript.Echo("Extensions (System)\n" + regReadVal(sCompName, HKLM, "Software\\Microsoft\\Command Processor", "EnableExtensions"));
} else {
  WScript.Echo("HKLM Access Denied");
}

Regards
aGerman

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

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

#32 Post by penpen » 15 Aug 2016 18:25

aGerman wrote:I know from former posts that csc was removed from Dave's PC at work :(
Is it really removed, or is its execution blocked?
In the second case it might be possible to create a powershell script using c#,
and therefore executing csc indirectly (here is a howto).

aGerman wrote:- enabled extensions and delayed expansion could be checked using IF statements. E.g.:
You don't need to execute cmd, just use the set command:

Code: Select all

set extensions=disabled
2>nul set "extensions=enabled"
set delayedExpansion=disabled
if "!delayedExpansion!" == "%delayedExpansion%" set delayedExpansion=enabled

echo extensions: %extensions%   delayedExpansion: %delayedExpansion%

aGerman wrote:- OEMCP can be extracted from CHCP.
- ACP ??? No idea yet.
This seems to work under windows 10 (actually can't test it for other win versions):

Code: Select all

wmic os get codeset
I don't know if this would work on dave's work pc:
I could imagine wmic.exe may also be disabled.
(Maybe within jscript - but copuld also be disabled... else see "wmic /locale:ms_409 os get * /format:list".)


penpen

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

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

#33 Post by aGerman » 16 Aug 2016 02:39

penpen wrote:Is it really removed, or is its execution blocked?

Actually Dave should answer that. This information is quite old:
http://www.dostips.com/forum/viewtopic.php?f=3&p=27584#p27584

penpen wrote:You don't need to execute cmd, just use the set command:

Good idea!

penpen wrote:This seems to work under windows 10 (actually can't test it for other win versions):

Right now tested on Win7. Works great.


Tested at work too:
Not surprizingly the Locale Name and MUI Languages can differ. Indeed half of the commands are in German and half in English :D But both searching for "ulib.dll.mui" and wmic report the the same languages.

Code: Select all

@echo off &setlocal
for /f "tokens=1,2,*" %%a in ('reg query "HKCU\Control Panel\International" /v "LocaleName"^|find "REG_"') do echo %%a=%%c

echo ~~~~~~~~~~~~~~~~~~~~~~
for /f %%i in ('dir /ad /b "%SystemRoot%\System32\??-??"^|findstr /x "[a-z][a-z]-[a-z][a-z]"') do (
  if exist "%SystemRoot%\System32\%%i\ulib.dll.mui" echo %%i
)

echo ~~~~~~~~~~~~~~~~~~~~~~
wmic os get MUILanguages /value

pause

Code: Select all

LocaleName=de-DE
~~~~~~~~~~~~~~~~~~~~~~
de-DE
en-US
~~~~~~~~~~~~~~~~~~~~~~


MUILanguages={"en-US","de-DE"}



Drücken Sie eine beliebige Taste . . .

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#34 Post by dbenham » 16 Aug 2016 10:30

The WMI access via JScript worked :D

Code: Select all

D:\test>cscript //nologo test.js
Extensions (User)
1
Extensions (System)
1

WMIC works perfectly fine in my work environment, and has worked for as long as I have been here.

I'm not 100% sure, but I believe csc is removed.


Dave Benham

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

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

#35 Post by aGerman » 16 Aug 2016 11:17

dbenham wrote:The WMI access via JScript worked :D

That's great news. Thanks Dave!

Now it's time to assemble the script new before I lose track of all the improvements ...

FWIW Is there a way to check if the WSH (JScript in this case) is administrative disabled?

Regards
aGerman

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

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

#36 Post by penpen » 16 Aug 2016 15:23

aGerman wrote:FWIW Is there a way to check if the WSH (JScript in this case) is administrative disabled?
This works on Windows 10:

Code: Select all

>nul 2>nul cscript /?
if errorlevel 1 echo WSH is disabled.


The only way to retrieve the Locale Name (if reg access is disabled) that i could see is to build it ourselves from the Locale Language Code:

Code: Select all

wmic os get locale


penpen

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

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

#37 Post by aGerman » 16 Aug 2016 16:32

Thanks penpen!

penpen wrote:The only way to retrieve the Locale Name (if reg access is disabled) that i could see is to build it ourselves from the Locale Language Code:

Code: Select all

wmic os get locale

I think "MUILanguages" is sufficient in that case. It's already quite a lot of code for just a few informations. Currently more than 350 rows and I'm still working on it :lol:

Regards
aGerman

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

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

#38 Post by aGerman » 16 Aug 2016 18:30

I know it's not perfect yet. At least I hope there are not too many mistakes...

Code: Select all

@if (@a)==(@b) @end /* :: ~~~~~~~~~ Batch Part ~~~~~~~~~
@echo off

set ex_noreg=Disabled
2>nul set "ex_noreg=Enabled "
set de_noreg=Disabled
if "!!"=="" (set de_noreg=Enabled )

setlocal EnableExtensions DisableDelayedExpansion
cd /d "%~dp0"
>"%temp%\info.txt" echo [code^]

:: prepare some variables to shorten lines in the script
set "International=Control Panel\International"
set "CurrentVersion=SOFTWARE\Microsoft\Windows NT\CurrentVersion"
set "CodePage=SYSTEM\CurrentControlSet\Control\Nls\CodePage"
set "CMDproc=Software\Microsoft\Command Processor"
set /a "HKCU=80000001, HKLM=80000002, HKU=80000003"
set "sys32=%SystemRoot%\System32"

:: try to assign variables for used tools in order to make the script run even if the path or pathext variables are corrupted
if exist "%sys32%\clip.exe" (set "clip=%sys32%\clip.exe") else (set "clip=" &>>"%temp%\info.txt" echo clip.exe not found.)
if exist "%sys32%\cscript.exe" (set "cscript=%sys32%\cscript.exe") else set "cscript=cscript.exe"
>nul 2>nul %cscript% /? || (set "cscript=" &>>"%temp%\info.txt" echo cscript.exe not accessible.)
if exist "%sys32%\find.exe" (set "find=%sys32%\find.exe") else (set "find=echo" &>>"%temp%\info.txt" echo find.exe not found.)
if exist "%sys32%\findstr.exe" (set "findstr=%sys32%\findstr.exe") else (set "findstr=echo" &>>"%temp%\info.txt" echo findstr.exe not found.)
if exist "%sys32%\gpresult.exe" (set "gpresult=%sys32%\gpresult.exe") else (set "gpresult=" &>>"%temp%\info.txt" echo gpresult.exe not found.)
if defined gpresult >nul 2>nul %gpresult% /? || (set "gpresult=" &>>"%temp%\info.txt" echo gpresult.exe not accessible.)
if exist "%sys32%\net.exe" (set "net=%sys32%\net.exe") else (set "net=echo" &>>"%temp%\info.txt" echo net.exe not found.)
if exist "%SystemRoot%\notepad.exe" (set "notepad=%SystemRoot%\notepad.exe") else if exist "%sys32%\notepad.exe" (
  set "notepad=%sys32%\notepad.exe"
) else (set "notepad=%sys32%\cmd.exe /k type" &>>"%temp%\info.txt" echo notepad.exe not found.)
if exist "%sys32%\ping.exe" (set "ping=%sys32%\ping.exe") else (set "ping=(for /l %%i in (0 1 10000) do echo %%i>nul)&echo" &>>"%temp%\info.txt" echo ping.exe not found.)
if exist "%sys32%\reg.exe" (set "reg=%sys32%\reg.exe") else (set "reg=foo?.exe" &>>"%temp%\info.txt" echo reg.exe not found.)
if exist "%sys32%\whoami.exe" (set "whoami=%sys32%\whoami.exe") else (set "whoami=" &>>"%temp%\info.txt" echo whoami.exe not found.)
if defined whoami >nul 2>nul %whoami% /? || (set "whoami=" &>>"%temp%\info.txt" echo whoami.exe not acessible.)
if exist "%sys32%\wbem\WMIC.exe" (set "wmic=%sys32%\wbem\WMIC.exe") else (set "wmic=" &>>"%temp%\info.txt" echo wmic.exe not found.)
if defined wmic >nul 2>nul %wmic% /? || (set "wmic=" &>>"%temp%\info.txt" echo wmic.exe not accessible.)
>>"%temp%\info.txt" echo ----------------------------------------------------------------------------


:: list of directories to check if missing in the path variable
set dirs="%SystemRoot%","%sys32%","%sys32%\wbem","%sys32%\WindowsPowerShell\v1.0"

:: list ofextensions to check if missing in the pathext variable
set ext=.bat,.cmd,.com,.exe,.js,.jse,.msc,.vbe,.vbs,.wsf,.wsh

:: list of tools to check if missing in the path environment
set tools=certutil,choice,clip,debug,forfiles,gpresult,icacls,openfiles,powershell,robocopy,timeout,whoami,wmic

:: check reg access
%reg% query "HKCU\%International%" >nul 2>&1 && (set "RegUserInternational=1") || (set "RegUserInternational=0")
%reg% query "HKU\.DEFAULT\%International%" >nul 2>&1 && (set "RegDefInternational=1") || (set "RegDefInternational=0")
%reg% query "HKLM\%CurrentVersion%" >nul 2>&1 && (set "RegSysCurrentVersion=1") || (set "RegSysCurrentVersion=0")
%reg% query "HKLM\%CodePage%" >nul 2>&1 && (set "RegSysCodePage=1") || (set "RegSysCodePage=0")
%reg% query "HKCU\%CMDproc%" >nul 2>&1 && (set "RegUserCMDproc=1") || (set "RegUserCMDproc=0")
%reg% query "HKLM\%CMDproc%" >nul 2>&1 && (set "RegSysCMDproc=1") || (set "RegSysCMDproc=0")

:: check elevation and Admin membership
set "RunAs="
%net% session >nul 2>&1 && (set "RunAs=Yes")
if not defined RunAs if defined cscript (
  for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HCU%" "HKU\S-1-5-19" ""') do set "RunAs=%%i"
)
if defined RunAs (if "%RunAs%"=="#AccessDenied" (set "RunAs=No") else set "RunAs=Yes") else set "RunAs=No"
set "LocalAdmin="
if defined whoami 2>nul %whoami% /groups|>nul %findstr% /i "\<S-1-5-32-544\>" && set "LocalAdmin=Yes" || set "LocalAdmin=Yes"
if not defined LocalAdmin if defined wmic (
  for /f "tokens=1* delims==" %%i in (
    '%wmic% path Win32_Group WHERE "LocalAccount='TRUE' AND SID='S-1-5-32-544'" GET Name /value'
  ) do for /f "delims=" %%k in ("%%j") do (
    for /f "tokens=1* delims=:" %%l in ('2^>nul %gpresult% /r /scope user ^| %findstr% /n /c:"--------" /c:"%%k"') do (
      set "check="
      for /f "delims=- " %%n in ("%%m") do set "check=1"
      if not defined check (
        set "n=%%l"
        set "LocalAdmin=No"
      ) else for /f %%n in ('set /a n') do if %%n lss %%l set "LocalAdmin=Yes"
    )
  )
) else set "LocalAdmin=Not found"

:: International
for %%a in ("iDate" "iTime" "LocaleName") do set "%%~a="
if %RegUserInternational%==1 (call :RegInternational) else if defined cscript call :JsInternational
if not defined iDate set "iDate=0"
if not defined iTime set "iTime=1"
if not defined LocaleName call :MUI
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 set "hours=24"

:: ProductName
set "ProductName="
if %RegSysCurrentVersion%==1 (call :RegProductName) else if defined cscript call :JsProductName
if not defined ProductName for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os get Caption /value') do set "ProductName=%%j"

:: Extensions and DelayedExpansion
for %%a in (es eu ds du) do set "%%a=Disabled"
if %RegUserCMDproc%==1 (call :RegUserProc) else if defined cscript call :JsUserProc
if %RegSysCMDproc%==1 (call :RegSysProc) else if defined cscript call :JsSysProc
if not defined eu set "eu=%ex_noreg%"
if not defined du set "du=%de_noreg%"

:: Code Pages
set "OEMCP=" &set "ACP="
if %RegSysCodePage%==1 (call :RegCodePage) else if defined cscript call :JsCodePage
if not defined OEMCP for /f "tokens=2 delims=:" %%i in ('CHCP') do set /a "OEMCP=%%~ni"
if not defined ACP (
  if defined wmic (
    for /f "tokens=2 delims==" %%i in ('%wmic% os get CodeSet /value') do set /a "ACP=%%i"
  ) else set "ACP=Not found"
)

:: checks path
:: removes random double quotes, adds double quotes, removes trailing slash
:: http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell/5472168#5472168
set "p=%path:"=""%"
set "p=%p:^=^^%"
set "p=%p:&=^&%"
set "p=%p:|=^|%"
set "p=%p:<=^<%"
set "p=%p:>=^>%"
set "p=%p:;=^;^;%"
set p=%p:""="%
set "p=%p:"=""%"
set "p=%p:;;="";""%"
set "p=%p:^;^;=;%"
set "p=%p:""="%"
set "p=%p:"=""%"
set "p=%p:"";""=";"%"
set "p=%p:"""="%"
set "p=%p:\"="%"

:: Check for 64 bit windows.
set "bit="
if defined wmic (
  for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os GET OSArchitecture /value') do for /f "delims=" %%k in ("%%j") do set "bit=%%k"
)
if defined bit (echo "%bit%"|%find% "64" >nul && set "bit=64" || set "bit=32") else (
  echo "%PROCESSOR_ARCHITECTURE%"|%find% "86" >nul && set "bit=32" || (
    if exist "%SystemRoot%\SysWOW64\" (set "bit=64") else set "bit=32"
  )
)

:: RAM space
set "ram="
if defined wmic (
  for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os GET TotalVisibleMemorySize /value') do for /f "delims=" %%k in ("%%j") do set "ram=%%k"
)

:: get the DIR command format to display
for /f "delims=" %%a in ('dir "%SystemDrive%\pagefile.sys" /a ^|%find% ":"') do set "DirFormat=%%a"

set "pad=                          "
:: create the information file and send the information to the clipboard if clip is available
>>"%temp%\info.txt" (
  for /f "delims=" %%a in ('ver') do echo Windows version        :  %%a
  echo Product name           :  %ProductName%, %bit% bit
  echo Performance indicators :  Processor Cores: %NUMBER_OF_PROCESSORS%      Visible RAM: %ram% Bytes&echo(

  echo Date/Time format       :  %format% (%hours% hours^)     %date%  %time%
  echo Extensions             :  system: %es%  user: %eu%
  echo Delayed expansion      :  system: %ds%  user: %du%
  echo Locale name            :  %LocaleName%       Code Pages: OEM  %OEMCP%    ANSI %ACP%
  echo DIR  format            :  %DirFormat%
  echo Permissions            :  Elevated Admin=%RunAs%, Admin group=%LocalAdmin%&echo(

  rem report if path elements are missing
  for %%i in (%dirs%) do (
    set "found="
    setlocal EnableDelayedExpansion
    for %%j in ("!p!") do (
      endlocal
      if /i "%%~i"==%%j set "found=1"
      setlocal EnableDelayedExpansion
    )
    endlocal
    if not defined found echo(%pad%Missing from the PATH environment: %%~i
  )

  rem report if pathext elements are missing
  for %%i in (%ext%) do (
    set "found="
    for %%j in (%PATHEXT%) do (
      if /i "%%i"=="%%j" set "found=1"
    )
    if not defined found echo(%pad%Missing from the PATHEXT environment: %%~i
  )

  rem report if tools are missing
  for %%i in (%tools%) do for /f "tokens=1,2 delims=?" %%j in ("%%~i.exe?%%~i.com") do if "%%~$PATH:j%%~$PATH:k"=="" (
    echo(%pad%Missing from the tool collection:  %%i
  )
  echo [/code^]
)
if defined clip (%clip% < "%temp%\info.txt")
:: load the information into Notepad where it can also be copied to the clipboard
start "" %notepad% "%temp%\info.txt" & %ping% -n 2 127.0.0.1 >nul & del "%temp%\info.txt"

goto :eof

:: ~~~~~~~~~ Sub Routines ~~~~~~~~~
:RegInternational
for /f "tokens=1,2*" %%a in ('%reg% query "HKCU\%International%" /v "i???e" 2^>nul ^| %find% "REG_"') do set "%%~a=%%~c"
if %RegDefInternational%==1 for /f "tokens=1,2*" %%a in ('%reg% query "HKU\.DEFAULT\%International%" /v "i???e" 2^>nul ^| %find% "REG_"') do if not defined %%~a set "%%~a=%%~c"
for /f "tokens=1,2*" %%a in ('%reg% query "HKCU\%International%" /v "LocaleName" 2^>nul ^| %find% "REG_"') do set "%%~a=%%~c"
goto :eof

:JsInternational
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKCU%" "%international%" "iDate"') do set "iDate=%%i"
if defined iDate if "%iDate%"=="#NotFound" for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKU%" ".DEFAULT\%international%" "iDate"') do set "iDate=%%i"
if defined iDate if "%iDate:~,1%"=="#" set "iDate=0"
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKCU%" "%international%" "iTime"') do set "iTime=%%i"
if defined iTime if "%iTime%"=="#NotFound" for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKU%" ".DEFAULT\%international%" "iTime"') do set "iTime=%%i"
if defined iTime if "%iTime:~,1%"=="#" set "iTime=1"
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKCU%" "%international%" "LocaleName"') do set "LocaleName=%%i"
if defined LocaleName if "%LocaleName:~,1%"=="#" set "LocaleName="
goto :eof

:MUI
for /f "tokens=2 delims=={}" %%i in ('2^>nul %wmic% os get MUILanguages /value') do set "LocaleName=%%i"
if defined LocaleName (
  set "LocaleName=%LocaleName:"=%"
) else (
  setlocal EnableDelayedExpansion
  for /f %%i in ('dir /ad /b "%sys32%\??-??"^|%findstr% /x "[a-z][a-z]-[a-z][a-z]"') do (
    if exist "%sys32%\%%i\ulib.dll.mui" set "LocaleName=!LocaleName!,%%i"
  )
  if defined LocaleName (for /f %%j in ("!LocaleName:~1!") do (endlocal &set "LocaleName=%%j")) else endlocal
)
goto :eof

:RegProductName
for /f "tokens=2*" %%a in ('%reg% query "HKLM\%CurrentVersion%"^|%find% /i "ProductName"') do set "ProductName=%%b"
goto :eof

:JsProductName
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKLM%" "%CurrentVersion%" "ProductName"') do set "ProductName=%%i"
if defined ProductName if "%ProductName:~,1%"=="#" set "ProductName="
goto :eof

:RegUserProc
%reg% query "HKCU\%CMDproc%" /v "EnableExtensions" 2>nul|%find% "0x1">nul && set "eu=Enabled "
%reg% query "HKCU\%CMDproc%" /v "DelayedExpansion" 2>nul|%find% "0x1">nul && set "du=Enabled "
goto :eof

:JsUserProc
set "v="&for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKCU%" "%CMDproc%" "EnableExtensions"') do set "v=%%i"
if defined v if "%v:~,1%"=="#" (set "eu=") else set "eu=Enabled "
set "v="&for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKCU%" "%CMDproc%" "DelayedExpansion"') do set "v=%%i"
if defined v if "%v:~,1%"=="#" (set "du=") else set "du=Enabled "
goto :eof

:RegSysProc
%reg% query "HKLM\%CMDproc%" /v "EnableExtensions" 2>nul|%find% "0x1">nul && set "es=Enabled "
%reg% query "HKLM\%CMDproc%" /v "DelayedExpansion" 2>nul|%find% "0x1">nul && set "ds=Enabled "
goto :eof

:JsSysProc
set "v="&for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKLM%" "%CMDproc%" "EnableExtensions"') do set "v=%%i"
if defined v if "%v:~,1%"=="#" (set "es=") else set "es=Enabled "
set "v="&for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKLM%" "%CMDproc%" "DelayedExpansion"') do set "v=%%i"
if defined v if "%v:~,1%"=="#" (set "ds=") else set "ds=Enabled "
goto :eof

:RegCodePage
for /f "tokens=3" %%a in ('%reg% query "HKLM\%CodePage%" /v "OEMCP"') do set /a "OEMCP=%%a"
for /f "tokens=3" %%a in ('%reg% query "HKLM\%CodePage%" /v "ACP"') do set /a "ACP=%%a"
goto :eof

:JsCodePage
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKLM%" "%CodePage%" "OEMCP"') do set "OEMCP=%%i"
if defined OEMCP if "%OEMCP:~,1%"=="#" set "OEMCP="
for /f "delims=" %%i in ('%cscript% //nologo //e:jscript "%~fs0" "%HKLM%" "%CodePage%" "ACP"') do set "ACP=%%i"
if defined ACP if "%ACP:~,1%"=="#" set "ACP="
goto :eof

:: ~~~~~~~~~ JScript Part ~~~~~~~~~ */
var REG_NONE=0, REG_SZ=1, REG_EXPAND_SZ=2, REG_BINARY=3, REG_DWORD=4, REG_MULTI_SZ=7, REG_QWORD=11;
var KEY_QUERY_VALUE=0x00000001;

function regCheckAccess(strComputer, uHive, strRegPath, uRequiredAccess) {
  try {
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objAccessMethod = objReg.Methods_.Item("CheckAccess");
    var objAccessInParam = objAccessMethod.InParameters.SpawnInstance_();
    objAccessInParam.hDefKey = uHive;
    objAccessInParam.sSubKeyName = strRegPath;
    objAccessInParam.uRequired = uRequiredAccess;
    var objAccessOutParam = objReg.ExecMethod_(objAccessMethod.Name, objAccessInParam);
    if (objAccessOutParam.ReturnValue == 0) {
      if (objAccessOutParam.bGranted) {return true;}
    }
    return false;
  }
  catch(e) {return false;}
}

function regReadVal(strComputer, uHive, strRegPath, strValName) {
  try {
    var vRet = null, iType = -1;
    var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
    var objService = objLocator.ConnectServer(strComputer, "root\\default");
    objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
    var objReg = objService.Get("StdRegProv");
    var objEnumMethod = objReg.Methods_.Item("EnumValues");
    var objEnumInParam = objEnumMethod.InParameters.SpawnInstance_();
    objEnumInParam.hDefKey = uHive;
    objEnumInParam.sSubKeyName = strRegPath;
    var objEnumOutParam = objReg.ExecMethod_(objEnumMethod.Name, objEnumInParam);
    if (objEnumOutParam.ReturnValue == 0) {
      if (objEnumOutParam.sNames != null) {
        for (var i = 0; i <= objEnumOutParam.sNames.ubound(); i++) {
          if (objEnumOutParam.sNames.getItem(i).toLowerCase() == strValName.toLowerCase()) {
            iType = objEnumOutParam.Types.getItem(i);
            break;
          }
        }
      }
      else {if (strValName == "") {iType = REG_NONE;}}
      if (iType == -1) {return null;}
    }
    else {return null;}
    switch (iType) {
      case REG_SZ:
        var objGetMethod = objReg.Methods_.Item("GetStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
        break;
      case REG_EXPAND_SZ:
        var objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
        break;
      case REG_BINARY:
        var objGetMethod = objReg.Methods_.Item("GetBinaryValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray();}}
        break;
      case REG_DWORD:
        var objGetMethod = objReg.Methods_.Item("GetDWORDValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
        break;
      case REG_MULTI_SZ:
        var objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray();}}
        break;
      case REG_QWORD:
        var objGetMethod = objReg.Methods_.Item("GetQWORDValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
        break;
      case REG_NONE:
        var objGetMethod = objReg.Methods_.Item("GetStringValue");
        var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetBinaryValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray(); break;}}
        objGetMethod = objReg.Methods_.Item("GetDWORDValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
        objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray(); break;}}
        objGetMethod = objReg.Methods_.Item("GetQWORDValue");
        objGetInParam = objGetMethod.InParameters.SpawnInstance_();
        objGetInParam.hDefKey = uHive;
        objGetInParam.sSubKeyName = strRegPath;
        objGetInParam.sValueName = strValName;
        objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
        if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
      default:
        return null;
    }
    return vRet;
  }
  catch(e) {return null;}
}

if (regCheckAccess(".", parseInt(WScript.Arguments(0), 16), WScript.Arguments(1), KEY_QUERY_VALUE) === false) {
  WScript.Echo("#AccessDenied");
} else {
  var v = regReadVal(".", parseInt(WScript.Arguments(0), 16), WScript.Arguments(1), WScript.Arguments(2));
  if (v === null)
  {
    WScript.Echo("#NotFound");
  } else {
    WScript.Echo(v);
  }
}

/*
:eof
:: */

Possible output

Code: Select all

----------------------------------------------------------------------------
Windows version        :  Microsoft Windows [Version 10.0.10586]
Product name           :  Windows 10 Home, 32 bit
Performance indicators :  Processor Cores: 4      Visible RAM: 2023612 Bytes

Date/Time format       :  dd/mm/yy (24 hours)     21.08.2016  16:18:26,28
Extensions             :  system: Enabled   user: Enabled
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  de-DE       Code Pages: OEM  850    ANSI 1252
DIR  format            :  21.08.2016  12:30       738.197.504 pagefile.sys
Permissions            :  Elevated Admin=No, Admin group=Yes



Regards
aGerman

//EDIT
08/21/2016 changed behavior if tools were not found, removed restart, added SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
08/19/2016 use GPRESULT for local admin check, minor changes
08/18/2016 %NUMBER_OF_PROCESSORS% added again as well as RAM space; elevation check changed, minor corrections
08/17/2016 %NUMBER_OF_PROCESSORS% removed; detection of bitness and local admin improved

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

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

#39 Post by Compo » 17 Aug 2016 00:39

Could someone please explain the purpose of including %PROCESSOR_ARCHITECTURE% and %NUMBER_OF_PROCESSORS%. What information does it give which may affect the writing of a script solution? Surely an indication only of %Bit% is needed.

This is the only use I can see for %PROCESSOR_ARCHITECTURE%

Code: Select all

Set bit=32 bit
If %PROCESSOR_ARCHITECTURE:~-2% Equ 64 (If Exist "%SYSTEMROOT%\SysWOW64\" Set bit=64 bit)

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

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

#40 Post by aGerman » 17 Aug 2016 02:29

Compo wrote:Could someone please explain the purpose of including %PROCESSOR_ARCHITECTURE% and %NUMBER_OF_PROCESSORS%.

%NUMBER_OF_PROCESSORS% did foxidrive include. I don't know the purpose but I remember that we had some discussions about speed and number of parallel threads. But generally I agree with you that this information is not necessarily needed.

The existence of "%SystemRoot%\SysWOW64" is actually sufficient to detect a 64 bit system. However this directory could be created by hand on a 32 bit system too. That could be detected if you know the value of %PROCESSOR_ARCHITECTURE% with the following rules:
- if both processor and existence of SysWOW64 fit together we have only little uncertainties
- x86 processor does always mean that the OS is 32 bit even if SysWOW64 exists for whatever reason
- x64 processor together with the absence of SysWOW64 would most likely mean that the OS is 32 bit

I'll try to merge these informations and perhaps include an additional check using WMIC ...

Regards
aGerman

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

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

#41 Post by aGerman » 17 Aug 2016 07:41

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?

Regards
aGerman

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

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

#42 Post by Squashman » 17 Aug 2016 08:03

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

Darn! Why didn't Bill just write everything in Klingon and call it good.

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

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

#43 Post by aGerman » 17 Aug 2016 11:19

@Compo
I changed the code above accordingly.

@Squashman
I would be fine with Klingon if that was something we could rely on :lol: The MUI concept is poison for scripters :roll:

Regards
aGerman

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

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

#44 Post by foxidrive » 17 Aug 2016 20:47

Compo wrote:Could someone please explain the purpose of including %PROCESSOR_ARCHITECTURE% and %NUMBER_OF_PROCESSORS%. What information does it give which may affect the writing of a script solution? Surely an indication only of %Bit% is needed.


You included this aspect in your question and then showed it was actually good information. I commented on it anyway.

64 bit Windows isn't the same as 32 bit Windows when accessing different parts of Windows.


The number of cores only gives an indication of the machine capability.
This example is unlikely but it shows that there are situations that you can't readily think of: If a user comes back and says "This script took 8 hours to run here, how come?" and we can see he's running a single core CPU then appropriate questions can be asked.

I'm only answering your question and it doesn't bother me at all if cpu cores isn't included. This script was designed to provide a good range of general information and that data was already in an environment variable so I just shoved it in.

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

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

#45 Post by aGerman » 18 Aug 2016 05:06

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. Installed memory, frequency of the CPU, current usage of CPU and RAM, etc. are further indicators (but still an incomplete list). Thus, I think Compo's objection is valid. That's the reason why I already left it out.

Steffen

Post Reply