2

I have an issue with my script where I get the list of all member of the local admin group and by checking the objectclass, I am able to only focus on the user. The problem is that it will not work in french or in german, because the word used to define the object class will not be the same (user in english, utilisateur in french, usario in spanish...). enter image description here

Is there any powershell command that can help me avoid taping the word "User" in every existing languages?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Noemie
  • 121
  • 1
  • 9
  • To summarize, so that the [source code of helper function `Use-Culture`](https://stackoverflow.com/a/59911341/45375) needn't be duplicated here, without explanation (you can then delete your answer): The current thread's _UI culture_ must temporarily be set to `en-US` (US-English) to ensure use of English identifiers, which works as follows with `Use-Culture`: `Use-Culture en-US { Get-LocalGroupMember -SID S-1-5-32-544 }` – mklement0 Apr 08 '22 at 13:47
  • An ad hoc solution, if you don't want to define a function: `$prev=[cultureinfo]::CurrentUICulture; [cultureinfo]::CurrentUICulture='en-US'; Get-LocalGroupMember -SID S-1-5-32-544; [cultureinfo]::CurrentUICulture=$prev` – mklement0 Apr 08 '22 at 13:47

1 Answers1

1
function Use-Culture
{    
  param(
    [Parameter(Mandatory)] [cultureinfo] $Culture,
    [Parameter(Mandatory)] [scriptblock] $ScriptBlock
  )
  # Note: In Windows 10, a culture-info object can be created from *any* string.
  #        However, an identifier that does't refer to a *predefined* culture is 
  #        reflected in .LCID containing 4096 (0x1000)
  if ($Culture.LCID -eq 4096) { Throw "Unrecognized culture: $($Culture.DisplayName)" }

  # Save the current culture / UI culture values.
  $PrevCultures = [Threading.Thread]::CurrentThread.CurrentCulture, [Threading.Thread]::CurrentThread.CurrentUICulture

  try {
    # (Temporarily) set the culture and UI culture for the current thread.
    [Threading.Thread]::CurrentThread.CurrentCulture = [Threading.Thread]::CurrentThread.CurrentUICulture = $Culture

    # Now invoke the given code.
    & $ScriptBlock

  }    
  finally {
    # Restore the previous culture / UI culture values.
    [Threading.Thread]::CurrentThread.CurrentCulture = $PrevCultures[0]
    [Threading.Thread]::CurrentThread.CurrentUICulture = $PrevCultures[1]
  }
}

Use-Culture en-US { Get-LocalGroupMember -SID S-1-5-32-544 }

Thanks to this : Temporarily change powershell language to English?

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Noemie
  • 121
  • 1
  • 9