Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Installs fonts to the local machine when run.
- .Description
- Installs all fonts in a folder to the local system. This script is intended to be run by group policy to install fonts on system startup.
- After startup this script behaves like a service, and checks for new additions or deletions from the remote lists every 30 minutes and acts
- accordingly.
- .NOTES
- File Name : font_install.ps1
- Author : JL421
- #>
- #====================#
- #Load Required Assemblies#
- #====================#
- #This Assembly allows for faster and less resource intensive file name retrieval.#
- [reflection.assembly]::loadwithpartialname("Microsoft.VisualBasic") | Out-Null
- #====================#
- #Set Initial Variables#
- #====================#
- #Numeric Representation of %WinDir%\Fonts#
- $FONTS = 0x14
- #Creating the Shell object to copy and install fonts.#
- $objShell = New-Object -ComObject Shell.Application
- $objFolder = $objShell.Namespace($FONTS)
- $LocalFontDir = "C:\Windows\Fonts\"
- $LocalFontDirRegex = "C:\\Windows\\Fonts\\"
- #Sets the Division Code to the First Argument in script call if it was entered. If not it gets the division code from the first 3 characters of the Computer Name.#
- $Division = IF ($args[1] -ne $null)
- {
- $args[1]
- }
- ELSE
- {
- $env:COMPUTERNAME.Substring(0,3)
- }
- #Sets the Department Code to the Second Argument in script call if it was entered. If not it get the department code from the 4th through 7th characters of the Computer Name.#
- $Department = IF ($args[2] -ne $null)
- {
- $args[2]
- }
- ELSE
- {
- $env:COMPUTERNAME.Substring(3,4)
- }
- #Sets the local font location and the location with the \ Regex'ed out.#
- $LocalFontDir = "C:\Windows\Fonts\"
- $LocalFontDirRegex = "C:\\Windows\\Fonts\\"
- $BaseFontDir = "\\server\basefontfolder\"
- $BaseFontDirRegex =
- "\\\\server\\basefontfolder\\"
- #Sets the Division and Department Directories and Regexes based on the provided Division and Department Codes.#
- IF ($Division -eq "D01")
- {
- $DivisionFontDir = $BaseFontDir + "\division1folder"
- $DivisionFontDirRegex = $BaseFontDirRegex + "division1folder\\"
- }
- ELSEIF ($Division -eq "D02")
- {
- $DivisionFontDir = $BaseFontDir + "\division2folder"
- $DivisionFontDirRegex = $BaseFontDirRegex + "division2folder\\"
- }
- IF ($Department -like "AD*" -or $Department -like "ADV*")
- {
- $DepartmentFontDir = $DivisionFontDir + "\Advertising"
- $DepartmentFontDirRegex = $DivisionFontDirRegex + "Advertising\\"
- }
- ELSEIF ($Department -like "CS*")
- {
- $DepartmentFontDir = $DivisionFontDir + "\CreativeServices"
- $DepartmentFontDirRegex = $DivisionFontDirRegex + "CreativeServices\\"
- }
- #Sets the Exclusion Font and Regex lists. (Fonts that may be installed on a specific machine that should not be removed.)#
- $ExclusionFontDir = $DepartmentFontDir + "\Exclusions"
- $ExclusionFontDirRegex = $DepartmentFontDirRegex + "Exclusions\\"
- #====================#
- #Define Script Functions#
- #====================#
- #Gets the registry name of a font based on the font file name.#
- function Get-RegistryStringNameFromValue([string] $keyPath, [string] $valueData)
- {
- $pattern = [Regex]::Escape($valueData)
- foreach($property in (Get-ItemProperty $keyPath).PsObject.Properties)
- {
- #Skip the property if it was one PowerShell added
- if(($property.Name -eq "PSPath") -or
- ($property.Name -eq "PSChildName"))
- {
- continue
- }
- #Search the text of the property
- $propertyText = "$($property.Value)"
- if($propertyText -match $pattern)
- {
- "$($property.Name)"
- }
- }
- }
- #Adds or Deletes fonts from the system based on arguments presented.#
- #First Argument Function: Font List being input into function.#
- #Second Argument Functions: Calls add or delete function - 0 = Delete, 1 = Add.#
- #Third Argument Functions: Tells the function if the input list is the Base (0), Division (1), or Department (2) font list.
- function FontAddDelete
- {
- IF ($args[1] -eq 1)
- {
- ForEach ($i in $args[0])
- {
- IF ($args[2] -eq 0)
- {
- $FontLocation = $BaseFontDir + "\" + $i
- $objFolder.CopyHere($FontLocation)
- }
- ELSEIF ($args[2] -eq 1)
- {
- $FontLocation = $DivisionFontDir + "\" + $i
- $objFolder.CopyHere($FontLocation)
- }
- ELSEIF ($args[2] -eq 2)
- {
- $FontLocation = $DepartmentFontDir + "\" + $i
- $objFolder.CopyHere($FontLocation)
- }
- }
- }
- ELSEIF ($args[1] -eq 0)
- {
- ForEach ($i in $args[0])
- {
- IF ($i.Substring($i.Length - 4) -eq ".pfm")
- {
- $RegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Type 1 Installer\Type 1 Fonts"
- $KeyName = Get-RegistryStringNameFromValue $RegistryKey $i
- Remove-ItemProperty -Path $RegistryKey -Name $KeyName
- $FontPFM = $LocalFontDir + "\" + $i
- $FontPFB = $LocalFontDir + "\" + $i.Substring(0,$i.Length - 4) + ".pfb"
- Remove-Item $FontPFM
- Remove-Item $FontPFB
- }
- ELSEIF ($i.Substring($i.Length -4) -eq ".fon" -or $i.Substring($i.Length -4) -eq ".otf" -or $i.Substring($i.Length -4) -eq ".ttc" -or $i.Substring($i.Length -4) -eq ".ttf")
- {
- $RegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
- $KeyName = Get-RegistryStringNameFromValue $RegistryKey $i
- Remove-ItemProperty -Path $RegistryKey -Name $KeyName
- $FontLocation = $LocalFontDir + "\" + $i
- Remove-Item $FontLocation
- }
- }
- }
- }
- Function Process
- {
- #Gets the list of font files with the extensions: .pfm (Type 1 Font - Bitstream), .fon (Font Files), .otf (OpenTypeFonts), .ttc (TrueType Collections), .ttf (TrueType Fonts).#
- #It also removes the path from the file name to only return the file name.#
- $LocalFontList = [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles($LocalFontDir) | ForEach {$_ -replace $LocalFontDirRegex, ""} | Where {$_.Substring($_.Length -4) -eq ".pfm" -or $_.Substring($_.Length -4) -eq ".fon" -or $_.Substring($_.Length -4) -eq ".otf" -or $_.Substring($_.Length -4) -eq ".ttc" -or $_.Substring($_.Length -4) -eq ".ttf"}
- $BaseFontList = [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles($BaseFontDir) | ForEach {$_ -replace $BaseFontDirRegex, ""} | Where {$_.Substring($_.Length -4) -eq ".pfm" -or $_.Substring($_.Length -4) -eq ".fon" -or $_.Substring($_.Length -4) -eq ".otf" -or $_.Substring($_.Length -4) -eq ".ttc" -or $_.Substring($_.Length -4) -eq ".ttf"}
- $DivisionFontList = [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles($DivisionFontDir) | ForEach {$_ -replace $DivisionFontDirRegex, ""} | Where {$_.Substring($_.Length -4) -eq ".pfm" -or $_.Substring($_.Length -4) -eq ".fon" -or $_.Substring($_.Length -4) -eq ".otf" -or $_.Substring($_.Length -4) -eq ".ttc" -or $_.Substring($_.Length -4) -eq ".ttf"}
- $DepartmentFontList = [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles($DepartmentFontDir) | ForEach {$_ -replace $DepartmentFontDirRegex, ""} | Where {$_.Substring($_.Length -4) -eq ".pfm" -or $_.Substring($_.Length -4) -eq ".fon" -or $_.Substring($_.Length -4) -eq ".otf" -or $_.Substring($_.Length -4) -eq ".ttc" -or $_.Substring($_.Length -4) -eq ".ttf"}
- $ExclusionFontList = [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles($ExclusionFontDir) | ForEach {$_ -replace $ExclusionFontDirRegex, ""} | Where {$_.Substring($_.Length -4) -eq ".pfm" -or $_.Substring($_.Length -4) -eq ".fon" -or $_.Substring($_.Length -4) -eq ".otf" -or $_.Substring($_.Length -4) -eq ".ttc" -or $_.Substring($_.Length -4) -eq ".ttf"}
- #Creates a list of all fonts to decide what needs to be deleted. This list includes the Exclusion list in order to exclude it from deletion.#
- $AllFontList = $BaseFontList + $DivisionFontList + $DepartmentFontList | Sort
- #Creates a list of local fonts with the underscore characters removed. Windows will tourniquet underscores at random length if multiple appear in a font file name.#
- #This allows us to keep fonts with underscores in the name without replacing them each time the script is run.#
- $LocalFontListNoUS = $LocalFontList | ForEach {$_ -replace "_", ""}
- IF ($LocalFontListNoUS -ne $null)
- {
- IF ($AllFontList -ne $null)
- {
- #Creates an AllFontList without underscores to match against the local list.#
- $AllFontListNoUS = $AllFontList | ForEach {$_ -replace "_", ""}
- #If the Compare shows the font is local, but not in either the Base, Division, Department, or Exclusion Lists it marks the font for Deletion.#
- $FontDeleteListNoUS = Compare-Object $AllFontListNoUS $LocalFontListNoUS | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
- IF ($FontDeleteListNoUS -ne $null)
- {
- #Defines a blank array for the Font Delete List so the next function can add items to it.#
- $FontDeleteList = @()
- ForEach ($i in $FontDeleteListNoUS)
- {
- $FontLike = $i.Substring(0,$i.Length -4) + "*"
- $Font = $LocalFontList -like $FontLike
- #Sets the Reboot Bit if there is a match.#
- IF ($Font -ne $null)
- {
- $RebootNeeded = 1
- }
- $FontDeleteList = $FontDeleteList + $Font
- }
- IF ($FontDeleteList -ne $null)
- {
- FontAddDelete $FontDeleteList 0
- }
- }
- }
- }
- IF ($BaseFontList -ne $null)
- {
- #Creates a BaseFontList without underscores to match against the local list.#
- $BaseFontListNoUS = $BaseFontList | ForEach {$_ -replace "_", ""}
- IF ($LocalFontList -eq $null) #If the Local Font List is empty, add all fonts in the Base font list. (This should never happen!)#
- {
- FontAddDelete $BaseFontList 1 0
- }
- Else
- {
- #If the Compare shows the font is in the Base list, but not on the local list, it marks it for Installation.#
- $BaseFontAddNoUS = Compare-Object $BaseFontListNoUS $LocalFontListNoUS | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject
- IF ($BaseFontAddNoUS -ne $null)
- {
- #Defines a blank array for the Base Font Add list so the next function can add items to it.#
- $BaseFontAdd = @()
- ForEach ($i in $BaseFontAddNoUS)
- {
- $FontLike = $i.Substring(0,$i.Length -4) + "*"
- $Font = $BaseFontList -like $FontLike
- #Sets the Reboot Bit if there is a match.#
- IF ($Font -ne $null)
- {
- $RebootNeeded = 1
- }
- $BaseFontAdd = $BaseFontAdd + $Font
- }
- IF ($BaseFontAdd -ne $null)
- {
- FontAddDelete $BaseFontAdd 1 0
- }
- }
- }
- }
- IF ($DivisionFontList -ne $null)
- {
- #Creates a DivisionFontList without underscores to match against the local list.#
- $DivisionFontListNoUS = $DivisionFontList | ForEach {$_ -replace "_", ""}
- IF ($LocalFontList -eq $null) #If the Local Font List is empty, add all fonts in the Division font list. (This should never happen!)#
- {
- FontAddDelete $DivisionFontList 1 1
- }
- Else
- {
- #If the Compare shows the font is in the Division list, but not on the local list, it marks it for Installation.#
- $DivisionFontAddNoUS = Compare-Object $DivisionFontListNoUS $LocalFontListNoUS | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject
- IF ($DivisionFontAddNoUS -ne $null)
- {
- #Defines a blank array for the Division Font Add list so the next function can add items to it.#
- $DivisionFontAdd = @()
- ForEach ($i in $DivisionFontAddNoUS)
- {
- $FontLike = $i.Substring(0,$i.Length -4) + "*"
- $Font = $DivisionFontList -like $FontLike
- #Sets the Reboot Bit if there is a match.#
- IF ($Font -ne $null)
- {
- $RebootNeeded = 1
- }
- $DivisionFontAdd = $DivisionFontAdd + $Font
- }
- IF ($DivisionFontAdd -ne $null)
- {
- FontAddDelete $DivisionFontAdd 1 1
- }
- }
- }
- }
- IF ($DepartmentFontList -ne $null)
- {
- #Creates a DepartmentFontList without underscores to match against the local list.#
- $DepartmentFontListNoUS = $DepartmentFontList | ForEach {$_ -replace "_", ""}
- IF ($LocalFontList -eq $null) #If the Local Font List is empty, add all fonts in the Department font list. (This should never happen!)#
- {
- FontAddDelete $DepartmentFontList 1 2
- }
- Else
- {
- #If the Compare shows the font is in the Department list, but not on the local list, it marks it for Installation.#
- $DepartmentFontAddNoUS = Compare-Object $DepartmentFontListNoUS $LocalFontListNoUS | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject
- IF ($DepartmentFontAddNoUS -ne $null)
- {
- #Defines a blank array for the Department Font Add list so the next function can add items to it.#
- $DepartmentFontAdd = @()
- ForEach ($i in $DepartmentFontAddNoUS)
- {
- $FontLike = $i.Substring(0,$i.Length -4) + "*"
- $Font = $DepartmentFontList -like $FontLike
- #Sets the Reboot Bit if there is a match.#
- IF ($Font -ne $null)
- {
- $RebootNeeded = 1
- }
- $DepartmentFontAdd = $DepartmentFontAdd + $Font
- }
- IF ($DepartmentFontAdd -ne $null)
- {
- FontAddDelete $DepartmentFontAdd 1 2
- }
- }
- }
- }
- IF ($RebootNeeded -eq 1)
- {
- #If the reboot bit is set, the system will notify the user that they have 5 minutes to save their work and reboot on their own, or the system will reboot automatically.#
- #This allows any changes made to be applied.#
- Shutdown /r /t 300 /f /c "Your Computer needs to be rebooted to finish installing or removing Fonts. Please save your work and restart your computer now. Your computer will automatically restart in 5 minutes."
- }
- }
- #====================#
- #Running Script#
- #====================#
- IF ($args[0] -eq 1)
- {
- Do
- {
- Process
- Start-Sleep 1800
- }
- While ($null -eq $null)
- }
- ELSE
- {
- Process
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement