【问题标题】:Installing System Font with Powershell使用 Powershell 安装系统字体
【发布时间】:2013-04-08 01:02:02
【问题描述】:

我有一个文件夹,里面装满了自定义字体的 TTF 文件。我需要使用 powershell 脚本将它们安装为系统字体(这是在 Windows Server 2008 R2 上)。有人知道如何在powershell中做到这一点吗? 谢谢!

【问题讨论】:

    标签: powershell windows-server-2008 truetype


    【解决方案1】:

    这很简单。看看下面的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 是什么意思?这是字体命名空间的值吗?
    • 是的,我用答案和链接编辑了代码,请查看。
    • 完美运行。谢谢!
    • 这将在已经安装字体的情况下弹出一个确认对话框 - 不适合脚本大量安装字体。
    • 这显示了不能隐藏的对话框,它也作为用户(appdata)安装,而不是作为系统windows\fonts
    【解决方案2】:

    只是想发布一个不需要将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 不适用于我,只能在交互模式下使用。有什么先决条件吗?
    【解决方案3】:

    使用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
    

    拆卸与安装相反。唯一的缺点是在安装字体之后和卸载之前如果应用程序引用了它,则需要重新启动。

    【讨论】:

      【解决方案4】:

      众所周知,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
              }
      #>
      

      【讨论】:

      • 已知 Shell 代码在 Remote 和 Build 代理上失败 - 如果使用 shell 的 comobjects 失败并且您正在通过 Remote 或 Build 代理进行审查,那么您将需要使用框架类来执行此操作 -参考:codewrecks.com/blog/index.php/2016/05/27/…
      • 请补充说明
      • 查看我在上一条评论中提供的链接 - 它包含您需要知道的一切
      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2017-05-18
      • 2018-05-25
      • 1970-01-01
      • 2018-07-30
      • 2014-10-06
      • 2020-06-19
      • 2014-05-06
      相关资源
      最近更新 更多