尝试使我们的新PC字体安装过程自动化。
要安装字体,Windows将.ttf,.otf等文件添加到C:\ Windows \ Fonts,然后在HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts中创建相应的注册表项。典型的注册表项如下所示:
Arial(TrueType)| Arial.ttf
为了自动执行此操作,我使用Get-ChildItem制作了两个数组:
$names = Get-ChildItem -Path "C:\corp\install\fonts" | Select-Object name | Out-String | ForEach-Object {$_ -Replace "----","" ` -Replace "Name","" ` -Replace ".otf","" ` -Replace ".ttf","" } | ForEach-Object { $_.Trim() }
$files = Get-ChildItem -Path "C:\corp\install\fonts" | Select-Object name | Out-String | ForEach-Object {$_ -Replace "----","" ` -Replace "Name","" } | ForEach-Object { $_.Trim() }
$ names中的每个$ name将是注册表项的名称,$ files中的每个$ file将是该注册表项的数据。
我将如何去做呢?我试图使用哈希表,PSObjects,嵌套的ForEach循环,但都无济于事。我在这里和其他地方都找不到完全符合这种情况的东西。
因为总是会有一个对应的值,所以错误检查并不是真正必要的。
修订的最终解决方案:
Write-Host "Installing corporate fonts..."
Copy-Item -Path "C:\corp\install\fonts\*" -Destination "C:\Windows\Fonts" -Force -Recurse
$fontList = @()
$fonts = Get-ChildItem "C:\corp\install\fonts" | Select-Object -ExpandProperty Name
ForEach ( $font in $fonts ) {
$fontList += [PSCustomObject] @{
Name = $font -Replace ".otf","" ` -Replace ".ttf",""
File = $font
} |
ForEach-Object {
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" -Name $_.Name -Value $_.File
}
}
最佳答案
我必须承认我不完全理解您的问题,因此,如果此答复不合时宜,请原谅我,但这是您要寻找的吗?一个表,其中包含两个数据?
Function CreateVariables {
$namevariables = @()
$filenames = ( Get-ChildItem "C:\corp\install\fonts" ).name
Foreach ( $name in $filenames ){
$namevariables += [PSCustomObject] @{
Name = $name -Replace "----","" ` -Replace "Name","" ` -Replace ".otf","" ` -Replace ".ttf",""
File = $name -Replace "----","" ` -Replace "Name",""
}
}
Return $namevariables
}
CreateVariables
关于arrays - 使用两个数组创建注册表项/值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56151923/