简体   繁体   English

无法在 Windows 10 上安装 fonts 和 Powershell

[英]Cannot install fonts with Powershell on Windows 10

On my work computer, I don't have admin privileges.在我的工作计算机上,我没有管理员权限。

Installing new fonts cannot be done "the easy way".安装新的 fonts 不是“简单的方法”。

At the time I was using Windows 7, I managed to run a PowerShell script that was launched at session startup and that installed the fonts from a given folder. At the time I was using Windows 7, I managed to run a PowerShell script that was launched at session startup and that installed the fonts from a given folder.

Here is the code I used:这是我使用的代码:

add-type -name Session -namespace "" -member @"
[DllImport("gdi32.dll")]
public static extern int AddFontResource(string filePath);
"@

$FontFolder = "C:\Users\myusername\Documents\Fonts"

$null = foreach($font in Get-ChildItem -Path $FontFolder -Recurse -Include *.ttf, *.otg, *.otf) {
    Write-Host "Installing : $($font.FullName)"
    $result = [Session]::AddFontResource($font.FullName)
    Write-Host "Installed $($result) fonts"
}

Now that I have switched to Windows 10, I thought I could go back to installing fonts "the easy way", as it is supposed to be possible to install fonts for your user without admin privileges. Now that I have switched to Windows 10, I thought I could go back to installing fonts "the easy way", as it is supposed to be possible to install fonts for your user without admin privileges.

This however still does not work: there is a popup window saying that "The requested file is not a valid font file".然而这仍然不起作用:弹出 window 说“请求的文件不是有效的字体文件”。 One solution is apparently to start the Windows firewall , which of course is not allowed by my administrator... but it is already running (see Edit below) 一种解决方案显然是启动 Windows 防火墙我的管理员当然不允许这样做......但它已经在运行(请参阅下面的编辑)

Back to the PowerShell then.然后回到 PowerShell。 The script unfortunately does not work anymore and does not provide any interesting pointers to where the problem comes from:不幸的是,该脚本不再起作用,并且没有提供任何有趣的指向问题出处的指针:

Installing : C:\Users\myusername\Documents\Fonts\zilla-slab\ZillaSlab-SemiBold.otf
Installed 0 fonts
Installing : C:\Users\myusername\Documents\Fonts\zilla-slab\ZillaSlab-SemiBoldItalic.otf
Installed 0 fonts
Installing : C:\Users\myusername\Documents\Fonts\zilla-slab\ZillaSlabHighlight-Bold.otf
Installed 0 fonts

I tried using a try catch, but still have no identified error:我尝试使用 try catch,但仍然没有发现错误:

add-type -name Session -namespace "" -member @"
[DllImport("gdi32.dll")]
public static extern int AddFontResource(string filePath);
"@

$FontFolder = "C:\Users\myusername\Documents\Fonts"

$null = foreach($font in Get-ChildItem -Path $FontFolder -Recurse -Include *.ttf, *.otg, *.otf) {
    try {
        Write-Host "Installing : $($font.FullName)"
        $result = [Session]::AddFontResource($font.FullName)
        Write-Host $result
    }
    catch {
        Write-Host "An error occured installing $($font)"
        Write-Host "$($error)"
        Write-Host "$($error[0].ToString())"
        Write-Host ""
        1
    }   
}

And the resulting output以及由此产生的 output

Installing : C:\Users\myusername\Documents\Fonts\zilla-slab\ZillaSlabHighlight-Bold.otf
0
Installing : C:\Users\myusername\Documents\Fonts\zilla-slab\ZillaSlabHighlight-Regular.otf
0
Installing : C:\Users\myusername\Documents\Fonts\ZillaSlab-Light.otf
0

Any idea how to solve this issue?知道如何解决这个问题吗?

Edit:编辑:

Regarding the status of the security applications, here is the McAfee status:关于安全应用程序的状态,这里是 McAfee 状态:

McAfee Data Exchange Layer OK 
McAfee DLP Endpoint OK 
Programme de mise à jour McAfee OK 
McAfee Endpoint Security OK 

"Programme de mise à jour" means "update program" in French. “Programme de mise à jour”在法语中意为“更新程序”。

I also checked the list of running services:我还检查了正在运行的服务列表:

  • mpssvc service (Windows defender firewall) is running mpssvc 服务(Windows Defender 防火墙)正在运行
  • mfefire (McAfee Firewall core service) is not running mfefire(McAfee 防火墙核心服务)未运行

Edit2:编辑2:

My last attempt is the following:我的最后一次尝试如下:

  • I copied the font file manually to the $($env:LOCALAPPDATA)\Microsoft\Windows\Fonts\ folder我将字体文件手动复制到 $($env:LOCALAPPDATA)\Microsoft\Windows\Fonts\ 文件夹
  • Using regedit, I added the entry as shown below使用 regedit,我添加了如下所示的条目

在此处输入图像描述

I restarted.我重新开始了。 Still no Bebas font in WordPad or Publisher WordPad 或 Publisher 中仍然没有 Bebas 字体

Here's how I do it with a com object.这是我使用 com object 的方法。 This works for me as non-admin based on Install fonts without administrative privileges .这适用于我作为非管理员基于Install fonts without administratorprivilege I can see the fonts installed to "$env:LOCALAPPDATA\Microsoft\Windows\Fonts" in the Fonts area under Settings.我可以在设置下的 Fonts 区域中看到 fonts 安装到“$env:LOCALAPPDATA\Microsoft\Windows\Fonts”。 I have Windows 10 20H2 (it should work in 1803 or higher).我有 Windows 10 20H2(它应该在 1803 或更高版本中工作)。 I also see the fonts installed in Wordpad.我还看到写字板中安装了 fonts。

$Destination = (New-Object -ComObject Shell.Application).Namespace(20)

$TempFolder = "$($env:windir)\Temp\Fonts\"

New-Item -Path $TempFolder -Type Directory -Force | Out-Null

Get-ChildItem -Path $PSScriptRoot\fonts\* -Include '*.ttf','*.ttc','*.otf' | 
  ForEach {
    If (-not(Test-Path "$($env:LOCALAPPDATA)\Microsoft\Windows\Fonts\$($_.Name)")) {

        $Font = "$($env:windir)\Temp\Fonts\$($_.Name)"

        Copy-Item  $($_.FullName) -Destination $TempFolder
        
        $Destination.CopyHere($Font)

        Remove-Item $Font -Force

    } else { "font $($env:LOCALAPPDATA)\Microsoft\Windows\Fonts\$($_.Name) already installed" }

}

Example REG_SZ registry entry: REG_SZ 注册表项示例:

dir 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Fonts*' | ft -a


    Hive: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion


Name  Property
----  --------
Fonts Nunito Black (TrueType) : C:\Users\myuser\AppData\Local\Microsoft\Windows\Fonts\Nunito-Black.ttf

You can install fonts on windows using following powershell scripts.您可以使用以下 powershell 脚本在 windows 上安装 fonts。

param(
[Parameter(Mandatory=$true,Position=0)]
[ValidateNotNull()]
[array]$pcNames,
[Parameter(Mandatory=$true,Position=1)]
[ValidateNotNull()]
[string]$fontFolder
)
$padVal = 20
$pcLabel = "Connecting To".PadRight($padVal," ")
$installLabel = "Installing Font".PadRight($padVal," ")
$errorLabel = "Computer Unavailable".PadRight($padVal," ")
$openType = "(Open Type)"
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
$objShell = New-Object -ComObject Shell.Application
if(!(Test-Path $fontFolder))
{
    Write-Warning "$fontFolder - Not Found"
}
else
{
    $objFolder = $objShell.namespace($fontFolder)
    foreach ($pcName in $pcNames)
    {
        Try{
            Write-Output "$pcLabel : $pcName"
            $null = Test-Connection $pcName -Count 1 -ErrorAction Stop
            $destination = "\\",$pcname,"\c$\Windows\Fonts" -join ""
            foreach ($file in $objFolder.items())
            {
                $fileType = $($objFolder.getDetailsOf($file, 2))
                if(($fileType -eq "OpenType font file") -or ($fileType -eq "TrueType font file"))
                {
                    $fontName = $($objFolder.getDetailsOf($File, 21))
                    $regKeyName = $fontName,$openType -join " "
                    $regKeyValue = $file.Name
                    Write-Output "$installLabel : $regKeyValue"
                    Copy-Item $file.Path  $destination
                    Invoke-Command -ComputerName $pcName -ScriptBlock { $null = New-ItemProperty -Path $args[0] -Name $args[1] -Value $args[2] -PropertyType String -Force } -ArgumentList $regPath,$regKeyname,$regKeyValue
                }
            }
        }
        catch{
            Write-Warning "$errorLabel : $pcName"
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM