From 03cc8964b117d18059b36fe1e0f567b393f4dc7e Mon Sep 17 00:00:00 2001 From: poslop Date: Fri, 9 Jan 2026 09:15:55 -0600 Subject: [PATCH] init --- PrinterDriverInstall.cmd | 4 +++ PrinterDriverInstall.ps1 | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 PrinterDriverInstall.cmd create mode 100644 PrinterDriverInstall.ps1 diff --git a/PrinterDriverInstall.cmd b/PrinterDriverInstall.cmd new file mode 100644 index 0000000..7e79e99 --- /dev/null +++ b/PrinterDriverInstall.cmd @@ -0,0 +1,4 @@ +@echo off +REM Ensure PowerShell execution regardless of local policy +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "\\domain_name\SYSVOL\domain_name\scripts\Printers\XeroxDriverInstall.ps1" +exit /b %ERRORLEVEL% diff --git a/PrinterDriverInstall.ps1 b/PrinterDriverInstall.ps1 new file mode 100644 index 0000000..dee6e68 --- /dev/null +++ b/PrinterDriverInstall.ps1 @@ -0,0 +1,71 @@ +$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 +``