Misc/Add-FontsToNavContainer.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<#
 .Synopsis
  Copy Font(s) to a NAV/BC container
 .Description
  Copies and registers missing fonts in a container to use in report printing or preview
 .Parameter containerName
  Name of the container to which you want to copy fonts
 .Parameter path
  Path to fonts to copy and register in the container
 .Example
  Add-FontsToNavContainer
 .Example
  Add-FontsToNavContainer -containerName test2
 .Example
  Add-FontsToNavContainer -containerName test2 -path "C:\Windows\Fonts\ming*.*"
 .Example
  Add-FontsToNavContainer -containerName test2 -path "C:\Windows\Fonts\mingliu.ttc"
#>

function Add-FontsToNavContainer {
   Param (
        [Parameter(Mandatory=$false)]
        [string] $containerName = "navserver", 
        [Parameter(Mandatory=$false)]
        [string] $path = "C:\Windows\Fonts"
    )

    $ExistingFonts = Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock {
        $fontsFolderPath = "C:\Windows\Fonts"
        Get-ChildItem -Path $fontsFolderPath | % { $_.Name }
    }

    Get-ChildItem $path -ErrorAction Ignore | % {
        if (!$ExistingFonts.Contains($_.Name) -and $_.Extension -ne ".ini") {

            try
            {
                $WindowsFontPath = Join-Path "c:\Windows\Fonts" $_.Name
                $fullName = $_.FullName
                Copy-FileToNavContainer -containerName $containerName -localPath $fullName -containerPath $WindowsFontPath

                Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock { Param($path)

#*******************************************************************
# Load C# code
#*******************************************************************
$fontCSharpCode = @'
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
 
namespace FontResource
{
    public class AddRemoveFonts
    {
        [DllImport("gdi32.dll")]
        static extern int AddFontResource(string lpFilename);
 
        public static int AddFont(string fontFilePath) {
            try
            {
                return AddFontResource(fontFilePath);
            }
            catch
            {
                return 0;
            }
        }
    }
}
'@


                    Add-Type $fontCSharpCode
                    
                    # Create hashtable containing valid font file extensions and text to append to Registry entry name.
                    $hashFontFileTypes = @{}
                    $hashFontFileTypes.Add(".fon", "")
                    $hashFontFileTypes.Add(".fnt", "")
                    $hashFontFileTypes.Add(".ttf", " (TrueType)")
                    $hashFontFileTypes.Add(".ttc", " (TrueType)")
                    $hashFontFileTypes.Add(".otf", " (OpenType)")
                    $fontRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
        
                    $fileDir  = split-path $path
                    $fileName = split-path $path -leaf
                    $fileExt = (Get-Item $path).extension
                    $fileBaseName = $fileName -replace($fileExt ,"")
            
                    $shell = new-object -com shell.application
                    $myFolder = $shell.Namespace($fileDir)
                    $fileobj = $myFolder.Items().Item($fileName)
                    $fontName = $myFolder.GetDetailsOf($fileobj,21)
            
                    if ($fontName -eq "") { $fontName = $fileBaseName }
            
                    $retVal = [FontResource.AddRemoveFonts]::AddFont($path)
            
                    if ($retVal -eq 0) {
                        Write-Host -ForegroundColor Red "Font `'$($path)`'`' installation failed"
                    } else {
                        Write-Host -ForegroundColor Green "Font `'$($path)`' installed successfully"
                        Set-ItemProperty -path "$($fontRegistryPath)" -name "$($fontName)$($hashFontFileTypes.item($fileExt))" -value "$($fileName)" -type STRING
                    }
                } -ArgumentList $WindowsFontPath
            }
            catch
            {
                Write-Host -ForegroundColor Red "Font `'$($fullName)`' exception when installing"
                $error.clear()
            }
        }
    }
}
Set-Alias -Name Add-FontsToBCContainer -Value Add-FontsToNavContainer
Export-ModuleMember -Function Add-FontsToNavContainer -Alias Add-FontsToBCContainer