Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Created October 20, 2019 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p0w3rsh3ll/5d39be94af9d6d36a4146b0a2fa21f6a to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/5d39be94af9d6d36a4146b0a2fa21f6a to your computer and use it in GitHub Desktop.
#Requires -Version 3.0
#Requires -RunAsAdministrator
Function Install-DefaultKeyboardLayout {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string[]]$ComputerName='localhost',
[Parameter()]
[string]$FirstLanguage='fr-FR',
[Parameter()]
[string]$SecondLanguage='en-GB',
[Parameter()]
[switch]$AsJob
)
Begin {
# Validate entered languages once
"$($FirstLanguage)","$($SecondLanguage)" |
ForEach-Object {
$l = "$($_)"
Write-Verbose -Message "Dealing with language $($l)"
try {
$test = New-WinUserLanguageList -Language "$($_)" -Verbose -ErrorAction Stop
if($null -eq (($test).InputMethodTips)) {
Throw "Input language $($l) isn't recognized as a valid language"
} else {
Write-Verbose -Message "Valid input language detected: $($_)"
}
} catch {
Throw "Testing Input language $($l) went wrong because $($_.Exception.Message)"
}
}
# Intialize a scriptblock
$s = {
Param($Verbose)
if ($null -eq $Verbose) { $Verbose = $false }
$lar = (
(New-WinUserLanguageList -Language $using:FirstLanguage)+
(New-WinUserLanguageList -Language $using:SecondLanguage)
)
$mHT = @{
Message = 'About to set {0} and {1} on computer {2}' -f "$($using:FirstLanguage)",
"$($using:SecondLanguage)",$($using:c)
}
Write-Verbose @mHT -Verbose:$Verbose
# Write-Verbose -Message "Verbose param is to: -$($Verbose)-" -Verbose
try {
$errHT = @{ ErrorAction = 'Stop' }
$aHT = @{
Execute = 'C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe'
Argument = '-Command "Set-WinUserLanguageList -LanguageList {0} -Force"' -f $lar
}
$HT = @{
TaskName = 'Set-Logon-KeyboardLayout'
User = 'S-1-5-18' #'nt authority\system'
Force = [switch]::Present
Action = (New-ScheduledTaskAction @aHT @errHT)
}
Register-ScheduledTask @HT @errHT |
Start-ScheduledTask @errHT
Write-Verbose -Message "Successfully set default keyboard layout on computer $($using:c)" -Verbose:$Verbose
} catch {
Write-Warning -Message "Failed because $($_.Exception.Message)"
}
}
}
Process {
$v = ($PSBoundParameters['Verbose'])
$ComputerName |
ForEach-Object {
$c = $_
Write-Verbose -Message "Dealing with computer $($c)"
try {
if ($v) {
Invoke-Command -ComputerName "$($c)" -ScriptBlock $s -ErrorAction Stop -AsJob:$AsJob -ArgumentList @($v)
} else {
Invoke-Command -ComputerName "$($c)" -ScriptBlock $s -ErrorAction Stop -AsJob:$AsJob
}
} catch {
Write-Warning -Message "Failed to invoke command on computer: $($c) because $($_.Exception.Message)"
}
}
}
End {}
<#
.SYNOPSIS
Set the keyobard layout at logon
.DESCRIPTION
Can set two keyboard layouts at logon (available before a user logs on).
.PARAMETER ComputerName
The list of remote computers to target and modify
.PARAMETER FirstLanguage
The first keyboard layout to set
.PARAMETER SecondLanguage
The second keyboard layout to set
.EXAMPLE
Install-DefaultKeyboardLayout
Set the keyboard layouts on the local computer to the default values: 1rst: fr-FR, 2nd en-GB
.EXAMPLE
Install-DefaultKeyboardLayout -Verbose
Set the keyboard layouts on the local computer to the default values: 1rst: fr-FR, 2nd en-GB
and shows a verbose stream of what happens
.EXAMPLE
Install-DefaultKeyboardLayout -FirstLanguage fr-FR -Verbose
Set the keyboard layouts on the local computer explicitly to french for the first keyboard layout,
uses the default value for the 2nd one (en-GB) and shows a verbose stream of what happens
.EXAMPLE
Install-DefaultKeyboardLayout -SecondLanguage en-GB -Verbose
Set the keyboard layouts on the local computer explicitly to english for the second keyboard layout,
uses the default value for the 1rst one (fr-FR) and shows a verbose stream of what happens
.EXAMPLE
Install-DefaultKeyboardLayout -ComputerName 'localhost' -Verbose
Set the keyboard layouts targeting the explicitely specified target computer
using the the default values: 1rst: fr-FR, 2nd en-GB
and shows a verbose stream of what happens
.EXAMPLE
$HT = @{ FirstLanguage = 'en-GB' ; SecondLanguage = 'fr-FR'}
Install-DefaultKeyboardLayout -ComputerName 'target1','target2' @HT -Verbose
Create a hashtable to define the first and second keyboard layouts to set
Set the keyboard layouts on the 2 remote computers and shows a verbose stream
.EXAMPLE
$HT = @{ FirstLanguage = 'en-GB' ; SecondLanguage = 'fr-FR'}
'target1','target2' |
Install-DefaultKeyboardLayout @HT -Verbose -AsJob
Get-Job | Receive-Job
Create a hashtable to define the first and second keyboard layouts to set
Set the keyboard layouts on the 2 remote computers as a job and shows a verbose stream
#>
}
Export-ModuleMember -Function 'Install-DefaultKeyboardLayout'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment