Detect user UI Display Language from system account

Home Forums Scripting General Scripting Detect user UI Display Language from system account

This topic contains 4 replies, has 3 voices, and was last updated by Avatar Adalberto 7 years, 4 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Avatar
    KodiaK
    Member
    #159911

    Hello,

    I need a script to detect if the users display language is DE or EN.
    The reg key that holds this value is:
    HKEY_CURRENT_USERControl PanelDesktopPrefferedUILanguage with the value de-DE or en-US.

    The problem is that if i check this location from a system account there is no PrefferedUILanguage string because the string is a user specific setting.
    So i have to check in all the SIDS under HKEY_USERS for the PrefferedUILanguage string, and i don’t know how to do that.

    Can someone please help me out.

    Thank you.

    Rems
    Rems
    Moderator
    #228229

    Re: Detect user UI Display Language from system account

    The SIDS listed under HKEY_USERS are only the sids for users with unclosed sessions.

    However it is possible to load also other’s hives:
    reg.exe load “HKLMLoadUserHive” “C:Documents and Settings[name]NTUSER.DAT

    Don’t forget to unload them again when you’re done:
    reg.exe unload “HKLMLoadUserHive”

    Load the hives under HKLM (my choice) or under HKU.

    Sample:

    Code:
    @echo off

    setlocal
    Set “TempKey=HKLMHIVE LOADED TEMPORARY FOR EDITING”

    pushD “%userprofile%..” ||goto:noloading
    >”[COLOR=”DarkSlateBlue”]c:PreferredUILanguages.log[/COLOR]” 2>&1 call:FindHives
    PopD
    :noloading

    goto:EOF + + + Sub Routines + + +
    :FindHives
    for /f “tokens=*” %%* in (‘dir . /AD /B’) do (
    If exist “%CD%%%*NTUSER.DAT” (
    call:RegCmds “%TempKey% %%*” “%CD%%%*NTUSER.DAT”))
    exit /b 0

    :RegCmds
    echo.
    echo.— %date% %time% —
    echo.%~2
    reg.exe LOAD %*
    [SIZE=”3″]rem[/SIZE] [COLOR=”Red”][B]reg.exe ADD “%~1Control PanelDesktop” /v PreferredUILanguagesPending /t REG_MULTI_SZ /d “en-US” /F[/B][/COLOR]
    reg.exe QUERY “%~1Control PanelDesktop” |findstr /ric:”PreferredUILanguages>”
    reg.exe QUERY “%~1Control PanelDesktop” |findstr /ric:”PreferredUILanguagesPending>”
    reg.exe UNLOAD %1
    echo.
    exit /b 0

    Or, instead of using a one-time admin script, you can create a user logon script (GPO).
    It’ll query the registry for the values during every login.
    The script can change the PreferredUILanguagesPending (!!!) value as desired.
    If the value PreferredUILanguagesPending is not equal to the value of PreferredUILanguages it then forces the user session to log off. The next logon the UI-Language will be changed. Like the sample batch above, you can also just query the current values and output to a log file.

    /Rems

    Avatar
    Adalberto
    Member
    #386772

    Re: Detect user UI Display Language from system account

    Hello

    Thank you for the reply, but i don’t think i can use this solution.

    I need to check the value of the following reg key on W7 OS:

    HKCUControl PanelDesktopPreferredUILanguages.
    This value exist only if a language pack is installed and is de-DE if the DE lp is installed. I need to do this to determine what the Display Language is set to the respective user.

    However i cannot search this value from a script ran with system or admin rights. Because it will search in the HKCU of the system or admin account.

    So i need to check in the HKU hives.

    I have managed to come up with this script:

    Const HKEY_USERS = &H80000003
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const OverwriteExisting = True
    Dim strComputer, oReg, oWMISvc, regEx, objReg, fso, sh, StrKeyPath1, objFSO, LANG, strKeyPath, arrSubKeys, subkey, arrValues, strValue, sUsr

    strComputer = “.” ‘for local computer enter “.”

    Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & _
    strComputer & “rootdefault:StdRegProv”)
    Set oWMISvc = GetObject(“winmgmts:rootcimv2”)
    Set regEx = New RegExp
    Set FSO = CreateObject(“Scripting.FileSystemObject”)
    set sh = createObject(“Wscript.shell”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “rootdefault:StdRegProv”)

    regEx.Pattern = “^S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$”
    regEx.IgnoreCase = TRUE

    strKeyPath = “SOFTWAREMicrosoftWindows NTCurrentVersionProfileList”
    oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

    For Each subkey In arrSubKeys
    If regEx.Test(subkey)=TRUE Then
    sUsr = resolveSID(subkey)
    strKeyPath1 = subkey & “Control PanelDesktop”
    objReg.GetMultiStringValue HKEY_USERS, strKeyPath1, “PreferredUILanguages”, arrValues
    If IsNull(arrValues) Then
    LANG = 0
    Else
    For Each strValue In arrValues
    LANG = strValue
    Next
    End If
    End If
    Next

    If LANG = “de-DE” then
    sh.RegWrite “HKLMSoftwarerwt GmbHUILanguage”, “DE”, “REG_SZ”
    Else
    sh.RegWrite “HKLMSoftwarerwt GmbHUILanguage”, “nonDE”, “REG_SZ”
    End If

    Function resolveSID(sid)
    Dim strUser
    On Error Resume Next
    With oWMISvc
    With .Get(“Win32_SID.SID='” & sid & “‘”)
    strUser = .AccountName
    End With
    End With
    If len(strUser) = 0 Then
    resolveSID = Empty
    Else
    resolveSID = strUser
    End If
    End function[/CODE]

    It basically checks all the hives under HKU and if it finds the PreferredUILanguages value it stores it in a HKLM key with the vale DE for de-DE UI language, and nonDE for anything else.

    It has only one flaw:

    If I set the admin UI lang to DE and then log to a user account and leave the UI to english, then it doesn’t work. Because when i open the cmd as admin i load the admin hive under HKU. The admin hive has the PreferredUILanguages value set to de-DE (because of the UI lang change i did on admin). The user on which i am logged is set to en-US. However the script returns the value de-DE because it finds the value under the admin hive.

    I have to detect somehow which user is logged on. And i have to do this from a vbs script which is ran with system rights.

    I’ll try the load hive thing and i’ll hope i can manage to make it work somehow.[CODE]Const HKEY_USERS = &H80000003
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const OverwriteExisting = True
    Dim strComputer, oReg, oWMISvc, regEx, objReg, fso, sh, StrKeyPath1, objFSO, LANG, strKeyPath, arrSubKeys, subkey, arrValues, strValue, sUsr

    strComputer = “.” ‘for local computer enter “.”

    Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & _
    strComputer & “rootdefault:StdRegProv”)
    Set oWMISvc = GetObject(“winmgmts:rootcimv2”)
    Set regEx = New RegExp
    Set FSO = CreateObject(“Scripting.FileSystemObject”)
    set sh = createObject(“Wscript.shell”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “rootdefault:StdRegProv”)

    regEx.Pattern = “^S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$”
    regEx.IgnoreCase = TRUE

    strKeyPath = “SOFTWAREMicrosoftWindows NTCurrentVersionProfileList”
    oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

    For Each subkey In arrSubKeys
    If regEx.Test(subkey)=TRUE Then
    sUsr = resolveSID(subkey)
    strKeyPath1 = subkey & “Control PanelDesktop”
    objReg.GetMultiStringValue HKEY_USERS, strKeyPath1, “PreferredUILanguages”, arrValues
    If IsNull(arrValues) Then
    LANG = 0
    Else
    For Each strValue In arrValues
    LANG = strValue
    Next
    End If
    End If
    Next

    If LANG = “de-DE” then
    sh.RegWrite “HKLMSoftwarerwt GmbHUILanguage”, “DE”, “REG_SZ”
    Else
    sh.RegWrite “HKLMSoftwarerwt GmbHUILanguage”, “nonDE”, “REG_SZ”
    End If

    Function resolveSID(sid)
    Dim strUser
    On Error Resume Next
    With oWMISvc
    With .Get(“Win32_SID.SID='” & sid & “‘”)
    strUser = .AccountName
    End With
    End With
    If len(strUser) = 0 Then
    resolveSID = Empty
    Else
    resolveSID = strUser
    End If
    End function[/CODE]

    It basically checks all the hives under HKU and if it finds the PreferredUILanguages value it stores it in a HKLM key with the vale DE for de-DE UI language, and nonDE for anything else.

    It has only one flaw:

    If I set the admin UI lang to DE and then log to a user account and leave the UI to english, then it doesn’t work. Because when i open the cmd as admin i load the admin hive under HKU. The admin hive has the PreferredUILanguages value set to de-DE (because of the UI lang change i did on admin). The user on which i am logged is set to en-US. However the script returns the value de-DE because it finds the value under the admin hive.

    I have to detect somehow which user is logged on. And i have to do this from a vbs script which is ran with system rights.

    I’ll try the load hive thing and i’ll hope i can manage to make it work somehow.

    Rems
    Rems
    Moderator
    #228230

    Re: Detect user UI Display Language from system account

    KodiaK;264872 wrote:
    It basically checks all the hives under HKU

    What is the OS of the machine? Is the current user logged on with a console session? Is it a local user or domain user?

    If you run the script on a computer as an domain admin – then it should do something like:

    Code:
    ‘ Run as an administrator! (a domain admin if the current user is a domain user)

    Const HKU = &H80000003
    Const HKLM = &H80000002

    rem On Error Resume Next

    strComputer = “.” ‘ [I]or enter the name of a remote computer[/I]

    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)

    Set colItems = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”,,48)
    For Each objItem in colItems
    sUser = split(objItem.UserName,””)(1)
    sDomain = split(objItem.UserName,””)(0)
    exit for
    Next

    Set objAccount = objWMIService.Get _
    (“Win32_UserAccount.Name='” & sUser & “‘,Domain='” & sDomain & “‘”)

    strSID = objAccount.SID
    SubKey1 = strSID & “Control PanelDesktop”
    SubKey2 = “Softwarerwt GmbH”
    Lang = “nonDE”

    Set regEx = New RegExp
    regEx.Pattern = “^S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$”
    regEx.IgnoreCase = TRUE
    If (regEx.Test(strSID) = False) Then wscript.quit

    Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & _
    strComputer & “rootdefault:StdRegProv”)

    oReg.CreateKey HKLM, SubKey2

    oReg.GetMultiStringValue HKU, SubKey1, _
    “[COLOR=”DarkSlateBlue”]PreferredUILanguages[/COLOR]”, arrValues

    If NOT IsNull(arrValues) Then
    If instr(1,join(arrValues), “DE”,1) Then Lang = “DE”
    End If

    oReg.SetStringValue HKLM, SubKey2, “[COLOR=”darkslateblue”]UILanguage[/COLOR]”, Lang

    wscript.quit

    Maybe you can run the script as scheduled tasks that runs after user login.

    A totally different solution could be that you (or a startup script or a GPP) change the permissions on the ‘rwt GmbH’ subkey to allow users to edit the values.

    /Rems

    Avatar
    Adalberto
    Member
    #386773

    Re: Detect user UI Display Language from system account

    Thanks a million.
    It works like a charm.
    I see you have simplified my script very much :)
    I am very unskilled at scripting, and i did that script pretty much copy/paste style.
    I have tested it from psexec on a user account (both user and admin are local accounts)

    The end package will be installed via SCCM, but i suppose the script will work from SCCM too, right?

    I cannot use it at logon because i have to install an application in a different folder if the Display Language is set to Deutsch. So i use the script to detect the UI Language and based on that (the reg key set in HKLM) the msi package will install to a different folder if the UI is set to DE.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.