简体   繁体   English

脚本安装字体Powershell

[英]Script install fonts powershell

I have a script which installs a font, after checking whether the fonts exists or not. 在检查字体是否存在之后,我有一个安装字体的脚本。 However, I'm having trouble validating the existence of the font. 但是,我在验证字体的存在方面遇到困难。

$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 
    }
}

Your if statement is wrong 您的if陈述有误

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

Test-Path returns true if the file exists and false if it doesnt. 如果文件存在,则Test-Path返回true否则,返回false So in the case the file does not exist you have false -eq false = true so nothing is executed. 因此,在文件不存在的情况下,您具有false -eq false = true因此不会执行任何操作。 Your code to copy the item only gets called if it already exists. 复制该项目的代码仅在该项目已存在时被调用。

Solution: 解:

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

Paul is correct. 保罗是正确的。 Basically your If condition was backwards. 基本上,您的If条件是向后的。

If (Test-Path "c:\\windows\\fonts\\$($File.name)") is equivalent to If ((Test-Path "c:\\windows\\fonts\\$($File.name)") -eq $True) If(Test-Path“ c:\\ windows \\ fonts \\ $($ File.name)”)等同于If((Test-Path“ c:\\ windows \\ fonts \\ $($ File.name)”)-eq $ True)

Either will work, though the second is unnecessarily more complex. 哪一种都可以,尽管第二种不必要地更复杂。

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

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