いくつかのUnzipフォルダー操作が表示されますが、Fontsフォルダーの状況に合うソリューションを作成している人は実際には誰もいません。だから私は自分で書いた!実は、FontsフォルダーはShell.Folder.CopyHereメソッドを実装していますが、メソッドの2番目の引数に渡されたオーバーロードを尊重していません。なんで?知るか!「 TheOldnewThing 」 WindowsDeveloperブログのRaymondChenがそれを説明できると思いますが、答えはわかりません。したがって、フォントをコピーする前に、フォントをインテリジェントに探す必要があります。そうしないと、厄介なメッセージが表示されます。
私のコードでは、ワイルドカード検索を使用してフォント名の最初の4文字が一致するかどうかを確認することにより、フォントが存在するかどうかを確認します。フォントが存在しない場合は、このシステムにフォントをインストールするのはこれが初めてであると想定し、$FirstInstallという特別なフラグを設定します。
それ以降、スクリプトで$ FirstInstallがtrueの場合、すべてのフォントをソースフォントディレクトリにインストールします。その後の実行では、各フォントが一致するかどうかを確認し、一致する場合は、そのコピーを中止します。そうでない場合は、先に進んでコピーします。これまでのところ、これは私のクライアントのほとんどで機能しているようです。
どうぞ!
<#
.SYNOPSIS
Script to quietly handle the installation of fonts from a network source to a system
.DESCRIPTION
We Can't just move files into the %windir%\Fonts directory with a script, as a simple copy paste from command line doesn't trigger windows to note the new font
If we used that approach, the files would exist within the directory, but the font files woudln't be registered in windows, nor would applications
display the new font for use. Instead, we can make a new object of the Shell.Application type (effectively an invisible Windows Explorer Windows) and use its Copy method
Which is the functional equivalent of dragging an dropping font files into the Font folder, which does trigger the font to be installed the same as if you right clicked the font
and choose install.
.PARAMETER FontPath
The path of a folder where fonts reside on the network
.EXAMPLE
.\Install-Fonts.ps1 -FontPath "\\corp\fileshare\Scripts\Fonts"
Installing font...C:\temp\Noto\NotoSans-Bold.ttf
Installing font...C:\temp\Noto\NotoSans-BoldItalic.ttf
Installing font...C:\temp\Noto\NotoSans-Italic.ttf
Installing font...C:\temp\Noto\NotoSans-Regular.ttf
In this case, the fonts are copied from the network down to the system and installed silently, minus the logging seen here
import files needed for step 1, step 2, and step 5 of the migration process.
.EXAMPLE
.\Install-Fonts.ps1 -FontPath "\\corp\fileshare\Scripts\Fonts"
Font already exists, skipping
Font already exists, skipping
Font already exists, skipping
Font already exists, skipping
In this case, the fonts already existed on the system. Rather than display an annoying 'Overwrite font' dialog, we simply abort the copy and try the next file
.INPUTS
String.
.OUTPUTS
Console output
.NOTES
CREATED: 06/11/2015
Author: sowen@ivision.com
MODIFIED:06/11/2015
Author: sowen@ivision.com -Reserved...
#>
param
(
[Parameter(Mandatory)][string]$FontPath="C:\temp\Noto"
)
#0x14 is a special system folder pointer to the path where fonts live, and is needed below.
$FONTS = 0x14
#Make a refrence to Shell.Application
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
ForEach ($font in (dir $fontsPath -Recurse -Include *.ttf,*.otf)){
#check for existing font (to suppress annoying 'do you want to overwrite' dialog box
if ((($objShell.NameSpace($FONTS).Items() | where Name -like "$($font.BaseName.Split('-')[0].substring(0,4))*") | measure).Count -eq 0){
$firstInstall = $true}
if ($firstInstall -ne $true) {Write-Output "Font already exists, skipping"}
else{
$objFolder.CopyHere($font.FullName)
Write-Output "Installing font...$($font.FullName)"
$firstInstall = $true
}
}
.\Install-Fonts.ps1 -FontPath "\\corp\fileshare\Scripts\Fonts"