脚本安装字体powershell

时间:2014-11-04 13:13:39

标签: powershell fonts

在检查字体是否存在后,我有一个安装字体的脚本。但是,我无法验证字体是否存在。

$FONTS = 0x14;

$FromPath = "c:\fonts";

$ObjShell = New-Object -ComObject Shell.Application;
$ObjFolder = $ObjShell.Namespace($FONTS);

$CopyOptions = 4 + 16;
$CopyFlag = [String]::Format("{0:x}", $CopyOptions);

foreach($File in $(Get-ChildItem -Path $FromPath)){
    If ((Test-Path "c:\windows\fonts\$($File.name)") -eq $False)
    { }
    Else
    {
        $CopyFlag = [String]::Format("{0:x}", $CopyOptions);
        $ObjFolder.CopyHere($File.fullname, $CopyOptions);

        New-ItemProperty -Name $File.fullname -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $File 
    }
}

2 个答案:

答案 0 :(得分:2)

您的if语句错误

If ((Test-Path "c:\windows\fonts\$($File.name)") -eq $False)
如果文件存在,

Test-Path会返回true,如果不存在,则返回false。因此,如果文件不存在,则表示false -eq false = true,因此不执行任何操作。复制项目的代码只有在已存在的情况下才会被调用。

解决方案:

If (Test-Path "c:\windows\fonts\$($File.name)")

答案 1 :(得分:2)

保罗是对的。基本上你的If条件是倒退的。

如果(Test-Path" c:\ windows \ fonts \ $($ File.name)")  相当于 如果((Test-Path" c:\ windows \ fonts \ $($ File.name)") - eq $ True)

要么会工作,尽管第二个会不必要地复杂。

相关问题