#Requires -Version 5.1 <# .SYNOPSIS Adds the shared printer \\EA1196SV\EA - Follow Me in the context of EANET\viljaOH. .DESCRIPTION Run this script from an administrator account. It prompts for the domain user's password, starts a temporary PowerShell process as EANET\viljaOH with that user's profile loaded, and adds the printer in that user's context. NOTE: If the printer driver itself is not already installed and Windows requires administrator elevation for the driver, the child process may still fail with 0x800702e4 (ERROR_ELEVATION_REQUIRED). #> [CmdletBinding()] param( [string]$DomainUser = 'EANET\dptest', [string]$PrinterPath = '\\EA1196SV\EA - Follow Me' ) $ErrorActionPreference = 'Stop' Write-Host "Printer: $PrinterPath" -ForegroundColor Cyan Write-Host "Target user: $DomainUser" -ForegroundColor Cyan Write-Host # Make sure the local Print Spooler is running. $spooler = Get-Service -Name Spooler -ErrorAction Stop if ($spooler.Status -ne 'Running') { Write-Host "Starting Print Spooler..." -ForegroundColor Yellow Start-Service -Name Spooler } # Prompt securely for the target domain user's password. $credential = Get-Credential ` -UserName $DomainUser ` -Message "Enter the password for $DomainUser" if (-not $credential) { throw "No credentials were supplied." } # Build the command that will run as the domain user. $childScript = @" `$ErrorActionPreference = 'Stop' `$PrinterPath = '$($PrinterPath.Replace("'", "''"))' Write-Host "Running as: `$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)" -ForegroundColor Cyan Write-Host "Installing printer: `$PrinterPath" -ForegroundColor Cyan try { `$existing = Get-Printer -ErrorAction SilentlyContinue | Where-Object { `$_.Name -eq `$PrinterPath -or `$_.ConnectionName -eq `$PrinterPath -or (`$_.Name -like '*EA - Follow Me*') } if (`$existing) { Write-Host "Printer is already installed for this user." -ForegroundColor Yellow exit 0 } Add-Printer -ConnectionName `$PrinterPath -ErrorAction Stop Write-Host "Printer installed successfully for `$env:USERDOMAIN\`$env:USERNAME." -ForegroundColor Green exit 0 } catch { Write-Host "Printer installation failed." -ForegroundColor Red Write-Host `$_.Exception.Message -ForegroundColor Red if (`$_.Exception.HResult -eq -2147024156 -or `$_.Exception.Message -match '0x800702e4') { Write-Host "" Write-Host "0x800702e4 = elevation required." -ForegroundColor Yellow Write-Host "The domain-user context is correct, but the printer driver likely needs to be pre-installed by an administrator." -ForegroundColor Yellow } exit 1 } "@ # Encode the child script to avoid quoting issues. $bytes = [System.Text.Encoding]::Unicode.GetBytes($childScript) $encodedCommand = [Convert]::ToBase64String($bytes) Write-Host "Starting printer installation as $DomainUser..." -ForegroundColor Cyan $process = Start-Process ` -FilePath "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" ` -Credential $credential ` -LoadUserProfile ` -ArgumentList @( '-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', $encodedCommand ) ` -Wait ` -PassThru if ($process.ExitCode -eq 0) { Write-Host Write-Host "Done." -ForegroundColor Green } else { Write-Host Write-Host "The user-context installation returned exit code $($process.ExitCode)." -ForegroundColor Red exit $process.ExitCode }