我需要创建一个 PowerShell 脚本,它将从文件中恢复语言设置和输入方法(重要)。一开始我以为会很好用Get-WinUserLanguageList | Export-CliXML ./mylist.xml
保存当前设置,然后 $List = Import-CliXML ./mylist.xml
, Set-WinUserLanguageList -LanguageList $List
,但是它不起作用,因为从 XML 导入变量的数据被反序列化,并且出现异常:
Set-WinUserLanguageList:无法绑定(bind)参数“LanguageList”。无法转换类型的“Microsoft.InternationalSettings.Commands.WinUserLanguage”值 输入“Deserialized.Microsoft.InternationalSettings.Commands.WinUserLanguage” “Microsoft.InternationalSettings.Commands.WinUserLanguage”。
我尝试使用 XML 但失败了,因此我创建了一个如下所示的解决方法:
[CmdletBinding()]
param (
[switch]$GenerateList
)
function Generate-List { # Generates language files to restore from.
$GoodList = Get-WinUserLanguageList
[string[]]$LanguageTags = $GoodList.LanguageTag
$LanguageTags | Out-File .\LanguageTags.txt
[string[]]$InputMethods = $GoodList.InputMethodTips
$InputMethods | Out-File .\InputMethods.txt
} # Exporting languages and corresponding input methods in separate files. Can be improved.
if ($GenerateList -eq $true) {
Generate-List
} # Invokes a function based on a switch parameter.
function RestoreFrom-List {
$GoodList = Get-WinUserLanguageList # Make our variable of a proper type
$GoodList.Clear() # Clear the variable contents
[string[]]$LanguageTags = Get-Content .\LanguageTags.txt
[string[]]$InputMethods = Get-Content .\InputMethods.txt
foreach ($language in $LanguageTags) { # This loop fills $GoodList with proper values
$index = $LanguageTags.IndexOf($language)
$GoodList.Add($language) # Add a language to the list
$GoodList[$index].InputMethodTips.Clear() # Remove default input method
$GoodList[$index].InputMethodTips.Add($InputMethods[$index]) # Add an input method from a corresponding position in the saved txt file
}
Set-WinUserLanguageList $GoodList -force # Restore system languages and input methods using a freshly created list
}
RestoreFrom-List
我是 PowerShell 新手,我确信这段代码很丑陋并且可以改进。另外,感觉 *-WinUserLanguageList cmdlet 使用起来很痛苦 - 您必须使用内部方法来更改数据,而不是通用的 Set-Property
.
到目前为止,我的脚本成功将语言/输入方法设置导出到两个(!)txt(!!)文件中,并且如果特定语言有两个或多个输入方法,则该脚本不起作用(因为它将输入方法转储到数组中)而不将它们与特定语言相关联,然后根据索引检索它们)。请帮助改进这一点。
最佳答案
决定重新审视我的旧问题并发布脚本的改进版本,该版本使用 JSON 来存储导出的语言。现在支持任意数量的语言和输入法,并且它们存储为结构正确的 JSON 文件。手写和拼写检查设置也被保留。
# Exports/imports windows language settings to/from a file
function ImportExport-Languages {
[CmdletBinding()]
param (
[switch]$GenerateList,
[string]$Path
)
## Export
function Export-Lang {
Get-WinUserLanguageList | ConvertTo-Json | Out-File $Path
}
## Import
function Import-Lang {
$importedFile = Get-Content $Path | ConvertFrom-Json
$langCollection = New-Object Microsoft.InternationalSettings.Commands.WinUserLanguage[] ""
foreach ($item in $importedFile)
{
$lang = [Microsoft.InternationalSettings.Commands.WinUserLanguage]::new($item.LanguageTag)
$lang.InputMethodTips.Clear() # clear default auto-generated input method
foreach ($inputMethod in $item.InputMethodTips)
{
$lang.InputMethodTips.Add($inputMethod)
}
$lang.Handwriting = $item.Handwriting
$lang.Spellchecking = $item.Spellchecking
$langCollection += $lang
}
Set-WinUserLanguageList $langCollection -Force
}
## Run
if ($GenerateList) { Export-Lang }
Import-Lang
}
ImportExport-Languages -Path "C:\MyFolder\MyFile.json" -GenerateList
关于powershell - 如何使用 powershell 脚本从保存的文件恢复 Windows 中的语言设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58697054/