72 lines
2.7 KiB
PowerShell
72 lines
2.7 KiB
PowerShell
$DriverInfPath = "\\domain_name\SYSVOL\domain_name\Drivers\Printer_Driver\driver_info.inf"
|
|
$DriverName = "Global Print Driver PCL6"
|
|
# must match Get-PrinterDriver name
|
|
|
|
$CentralLogDir = "\\domain_name\SYSVOL\domain_name\scripts\Printers\Logs"
|
|
$LocalLogDir = "C:\ProgramData\PrinterDeploy\Logs"
|
|
$LogFile = Join-Path $LocalLogDir "Deploy-PrinterDriver_$(Get-Date -Format 'yyyyMMdd').log"
|
|
|
|
|
|
New-Item -ItemType Directory -Path $LocalLogDir -Force | Out-Null
|
|
|
|
function Write-Log {
|
|
param([string]$Message, [string]$Level = "INFO")
|
|
$line = "[{0}] [{1}] {2}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Level, $Message
|
|
$line | Tee-Object -FilePath $LogFile -Append
|
|
try {
|
|
Write-EventLog -LogName Application -Source "PrinterDeployGPO" -EventId 1000 -EntryType Information -Message $line -ErrorAction SilentlyContinue
|
|
} catch {}
|
|
}
|
|
|
|
try {
|
|
if (-not [System.Diagnostics.EventLog]::SourceExists("PrinterDeployGPO")) {
|
|
New-EventLog -LogName Application -Source "PrinterDeployGPO"
|
|
}
|
|
} catch {}
|
|
|
|
Write-Log "Starting Deploy-PrinterDriver script."
|
|
|
|
if (-not (Test-Path $DriverInfPath)) {
|
|
Write-Log "Driver INF not found: $DriverInfPath" "ERROR"
|
|
exit 1
|
|
}
|
|
|
|
$existingDriver = Get-PrinterDriver -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq $DriverName }
|
|
if ($existingDriver) {
|
|
Write-Log "Driver '$DriverName' already installed. Skipping driver install."
|
|
} else {
|
|
Write-Log "Driver '$DriverName' not installed. Attempting installation via pnputil..."
|
|
$pnputilAddArgs = "/add-driver `"$DriverInfPath`" /install"
|
|
$process = Start-Process -FilePath "pnputil.exe" -ArgumentList $pnputilAddArgs -Wait -PassThru -WindowStyle Hidden
|
|
if ($process.ExitCode -eq 0) {
|
|
Write-Log "pnputil installed driver from INF successfully."
|
|
} else {
|
|
Write-Log "pnputil failed with ExitCode=$($process.ExitCode). Attempting fallback via Add-PrinterDriver..." "WARN"
|
|
try {
|
|
Add-PrinterDriver -Name $DriverName -InfPath $DriverInfPath -ErrorAction Stop
|
|
Write-Log "Add-PrinterDriver installed driver successfully."
|
|
} catch {
|
|
Write-Log "Add-PrinterDriver failed: $($_.Exception.Message)" "ERROR"
|
|
exit 2
|
|
}
|
|
}
|
|
}
|
|
|
|
if (-not $InstallPrinter) {
|
|
Write-Log "Configured to install driver only. Exiting."
|
|
goto :CopyLog
|
|
}
|
|
|
|
:CopyLog
|
|
try {
|
|
if (-not (Test-Path $CentralLogDir)) {
|
|
New-Item -ItemType Directory -Path $CentralLogDir -Force | Out-Null
|
|
}
|
|
$dest = Join-Path $CentralLogDir ("{0}_{1}.log" -f $env:COMPUTERNAME, (Get-Date -Format "yyyyMMddHHmmss"))
|
|
Copy-Item -Path $LogFile -Destination $dest -ErrorAction SilentlyContinue
|
|
} catch {}
|
|
|
|
Write-Log "Deploy-PrinterDriver script completed."
|
|
exit 0
|
|
``
|