与set-winuserlanguagelist命令相关的问题/问题

时间:2018-03-04 19:32:40

标签: powershell

我正在构建一个代码来修复Windows 10上的键盘布局情况。通过自动化解决方案,我决定使用PowerShell。但问题是,我在其中很新,并面临某些问题。我设法挖掘一个脚本来更改键盘布局,但它只更改为一种语言。当我尝试使用2种语言创建数组时:

$langlist=$lang_en,$lang_ru
set-winuserlanguagelist $langlist

它只是让我回到下一个错误:

Set-WinUserLanguageList : Cannot convert 'Microsoft.InternationalSettings.Commands.WinUserLanguage' to the type
'Microsoft.InternationalSettings.Commands.WinUserLanguage' required by parameter 'LanguageList'. Specified method is
not supported.
At line:1 char:25
+ set-winuserlanguagelist $langlist
+                         ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-WinUserLanguageList], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.InternationalSettings.Commands.SetWinUserLanguageListCommand

当我尝试使用下一个命令$test = Get-WinUserLanguageList时,该命令适用于set-winuserlanguagelist

完整的脚本:

$keys='0809:00020409', '0419:00000419'

$lang_en=new-winuserlanguagelist en-gb
$lang_en[0].inputmethodtips.clear()
$lang_en[0].inputmethodtips.add($keys[0])

$lang_ru=new-winuserlanguagelist ru
$lang_ru[0].inputmethodtips.clear()
$lang_ru[0].inputmethodtips.add($keys[1])

$langlist=$lang_en,$lang_ru

set-winuserlanguagelist $langlist

2 个答案:

答案 0 :(得分:2)

请检查以下评论代码段:

PS D:\PShell> ### Type mismatch

$langlist=$lang_en,$lang_ru

### Note the difference in type:

$langlist.gettype().Name                   ### Object[]

(Get-WinUserLanguageList).gettype().Name   ### List`1
Object[]
List`1
PS D:\PShell> ### Use the following:

$langlist = Get-WinUserLanguageList
$langlist.Clear()
$langlist.Add($lang_en[0])
$langlist.Add($lang_ru[0])

$langlist.gettype().Name                   ### List`1
List`1
PS D:\PShell> <### The next cmdlet should work now:

set-winuserlanguagelist $langlist

<##>

答案 1 :(得分:1)

问题在于您使用$langlist = $lang_en, $lang_ru 两次每个调用返回一个列表,以便{{ 1}} 错误地创建了(单项)列表的数组 而不是包含两个项目的单个列表,这导致了(无意义的)你看到的类型不匹配错误。

非常笨拙但是,cmdlet New-WinUserLanguageList只允许您指定一种语言,即使它返回列表类型[Collections.Generic.List[Microsoft.InternationalSettings.Commands.WinUserLanguage]])。

也就是说,以下应该可以工作,但不会:

# Try to create the list with *2* entries
$langlist = New-WinUserLanguageList en-gb, ru  # !! Doesn't work, parameter type is [string]

相反,您必须使用 1 语言进行初始化,然后使用.Add()方法添加以后的其他:< / p>

# Create the list with initially just 'en-gb'...
$langlist = New-WinUserLanguageList en-gb

# ... and then add the other language, 'ru'
# Because the list is strongly typed, it is sufficient to pass the language
# identifier, which implicitly creates a new
# [Microsoft.InternationalSettings.Commands.WinUserLanguage] instance.
$langlist.Add('ru')

# Now you can modify the properties of $langlist[0] (en-gb)
# and $langlist[1] (ru)
# ...

# ... and pass the list of modified languages to Set-WinUserLanguageList:
Set-WinUserLanguageList $langlist

或者,以避免.Add()来电,您可以使用

$langlist = (New-WinUserLanguageList en-gb)[0], (New-WinUserLanguageList ru)[0]

即使$langlist在技术上是一个数组(一个[System.Object[]]实例,其元素的类型为Microsoft.InternationalSettings.Commands.WinUserLanguage),但将其传递给Set-WinUserLanguageList有效,因为隐式转换为所需的列表类型。