Powershell 与设置winuserlanguagelist命令相关的问题
我正在编写一个代码来修复Windows10上的键盘布局情况。有了自动化解决方案,我决定使用powershell。但问题是,我是一个新手,面临着一些问题。我设法挖掘了一个脚本来更改键盘布局,但它只更改为一种语言。当我尝试使用两种语言创建数组时:Powershell 与设置winuserlanguagelist命令相关的问题,powershell,Powershell,我正在编写一个代码来修复Windows10上的键盘布局情况。有了自动化解决方案,我决定使用powershell。但问题是,我是一个新手,面临着一些问题。我设法挖掘了一个脚本来更改键盘布局,但它只更改为一种语言。当我尝试使用两种语言创建数组时: $langlist=$lang_en,$lang_ru set-winuserlanguagelist $langlist 它只会返回下一个错误: Set-WinUserLanguageList : Cannot convert 'Microsoft.I
$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
请检查以下注释的代码段:
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
PS D:\PShell>问题是您使用新的WinUserLanguageList
两次,每次调用都返回一个列表,因此$langlist=$lang\u en,$lang\u ru
错误地创建了一个(单个项目)列表的数组,而不是一个包含两个项目的列表,这导致了(听起来很荒谬)您看到的类型不匹配错误
但是,非常尴尬的是,cmdlet新的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()
方法添加其他语言:
# 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
$langlist = (New-WinUserLanguageList en-gb)[0], (New-WinUserLanguageList ru)[0]
或者,为了避免调用.Add()
,您可以使用:
# 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
$langlist = (New-WinUserLanguageList en-gb)[0], (New-WinUserLanguageList ru)[0]
即使$langlist
在技术上是一个数组(一个[System.Object[]]
实例,其元素类型为Microsoft.InternationalSettings.Commands.WinUserLanguage
),将其传递给Set WinUserLanguageList
会起作用,因为它被隐式转换为所需的列表类型。问题不再是实际问题。问题背后的原因是变量的类型。我将其声明为错误的方式。正确的方式是:$langlist=New Object”System.Collections.Generic.list[Microsoft.InternationalSettings.Commands.WinUserLanguage]“
真正的问题是您创建了列表数组,而不是[Microsoft.InternationalSettings.Commands.WinUserLanguage]数组
实例。新的WinUserLanguageList
cmdlet的笨拙设计是罪魁祸首,这一点仍然值得在回答中加以说明。另外,请允许我给你一个语言提示:“实际”的意思可能是“不再相关”。谢谢你的解释。当我试图进一步理解错误时,我理解了这个问题。创建列表修复了问题,cmdlet可以根据我的需要工作。是的,我想结束这个问题,但不知道如何结束。但至少人们花时间向我解释,我真的很高兴听到。在cas中e这不仅仅是一个输入错误:它并没有创建一个列表来解决问题,而是一个WinUserLanguage
实例列表-这就是New WinUserLanguageList
应该允许您使用多种语言所做的-但目前不允许。至于结束您的问题:您可以删除它,但在本例中,这不是一个g当答案已经发布时(如前所述,你的问题是有用的,因为其他人可能会犯同样的错误),你的想法是好的。
# 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
$langlist = (New-WinUserLanguageList en-gb)[0], (New-WinUserLanguageList ru)[0]