【发布时间】:2013-04-08 01:02:02
【问题描述】:
我有一个文件夹,里面装满了自定义字体的 TTF 文件。我需要使用 powershell 脚本将它们安装为系统字体(这是在 Windows Server 2008 R2 上)。有人知道如何在powershell中做到这一点吗? 谢谢!
【问题讨论】:
标签: powershell windows-server-2008 truetype
我有一个文件夹,里面装满了自定义字体的 TTF 文件。我需要使用 powershell 脚本将它们安装为系统字体(这是在 Windows Server 2008 R2 上)。有人知道如何在powershell中做到这一点吗? 谢谢!
【问题讨论】:
标签: powershell windows-server-2008 truetype
这很简单。看看下面的sn-p:
$FONTS = 0x14
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$objFolder.CopyHere("C:\test\Myfont.ttf")
它不应该需要重新启动/注销...
0x14 值是特殊文件夹的 CLSID。
此外,我刚刚发现本教程解释了上述每个步骤:
http://windowsitpro.com/scripting/trick-installing-fonts-vbscript-or-powershell-script
【讨论】:
只是想发布一个不需要将0x14 硬编码到脚本中的替代方案。将文件对象传递给函数,它只会根据文件所在的位置运行“安装”:
Function Install-Font {
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][System.IO.FileSystemInfo[]]$File
)
$shell = New-Object -ComObject Shell.Application
$File | % {
$Fonts = $shell.NameSpace($_.Directory.Name)
$font = $Fonts.ParseName($_.Name)
$font.InvokeVerb("Install")
}
}
【讨论】:
$shell = New-Object -ComObject Shell.Application 不适用于我,只能在交互模式下使用。有什么先决条件吗?
使用Shell.Application COM 对象在 Server Core 上不起作用(至少在 2012 R2 上不起作用)。
我只需将字体文件复制到C:\Windows\Fonts(在本例中为 times.ttf),然后使用 PowerShell 添加相应的注册表项即可成功:
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' -Name 'Times New Roman (TrueType)' -PropertyType String -Value times.ttf
拆卸与安装相反。唯一的缺点是在安装字体之后和卸载之前如果应用程序引用了它,则需要重新启动。
【讨论】:
众所周知,Shell 代码在 Remote 和 Build 代理上失败 - 如果使用 shell 的 comobjects 失败并且您正在通过 Remote 或 Build 代理进行审查,那么您将需要使用框架类来执行此操作 (reference)
## Add or Remove Font Files - only tested with TTF font files thus far
#<#
#=======================================================================================================
# ADD or REMOVE MULTIPLE FONT FILES [Using ComObjects]
#=======================================================================================================
# This code will install or uninstall a font using ComObject
# You Must Modify the following variables in order to work
# (A) $dirFiles ==> This is the source folder path that contains all of your font files
# (B) $InstallOrUninstall ==> $true = Install Font ... $false = UnInstall Font
#=======================================================================================================
# Define Working Variables
$dirFiles = "C:\Temp\Fonts"
$InstallOrUninstall = $false # $true = Install = 1 ...or... $false = UnInstall = 0
$srcFontFiles = Get-ChildItem "$($dirFiles)\Fonts"
$Fonts = (New-Object -ComObject Shell.Application).Namespace(0x14)
# Copy each file into the Font Folder or Delete it - Depends on the $InstallOrUninstall variable setting
ForEach($srcFontFile in $srcFontFiles)
{
$srcFontFileName = $srcFontFile.name
$srcFontFileFullPath = $srcFontFile.fullname
$targFonts = "C:\Windows\Fonts\$($srcFontFileName)"
If (Test-Path $targFonts -PathType any) { Remove-Item $targFonts -Recurse -Force } # UnInstall Font
If ((-not(Test-Path $targFonts -PathType container)) -and ($InstallOrUninstall -eq $true)) { $fonts.CopyHere($srcFontFileFullPath, 16) } # Install Font
}
#>
【讨论】: