简体   繁体   中英

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 ((Test-Path "c:\windows\fonts\$($File.name)") -eq $False)

Test-Path returns true if the file exists and false if it doesnt. So in the case the file does not exist you have false -eq false = true so nothing is executed. 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 (Test-Path "c:\\windows\\fonts\\$($File.name)") is equivalent to If ((Test-Path "c:\\windows\\fonts\\$($File.name)") -eq $True)

Either will work, though the second is unnecessarily more complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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