Files
shopdb/PRINTER_INSTALLER_FIX_2025-11-20.md
cproudlock 08d95f579a Phase 2 Migration: Complete PC consolidation and fixes
## Phase 2 Migration Complete
Successfully migrated all 286 active PCs from pc table to machines table.

### Migration Scripts Added/Updated:
- **Phase 1.0**: Added ensure_all_machinetypes.sql (machinetypes 15-20)
- **Phase 1.5**: Added migrate_equipment_ips_to_communications.sql
- **Phase 2**: Updated 01_migrate_pcs_to_machines.sql for duplicate handling
- **Phase 2**: Updated 08_update_schema_for_api.sql (rename pcid→machineid)
- **Phase 2 Fixes**: Added FIX_migrate_remaining_pcs.sql (60 unmigrated PCs)
- **Phase 2 Fixes**: Added FIX_pc_machine_types.sql

### Network Devices View Updated:
- **CREATE_vw_network_devices_with_fqdn.sql**: Complete rewrite for Phase 2
  - Infrastructure devices (IDF, Server, Switch, Camera, Access Point) query machines table
  - Printers remain in separate printers table (has fqdn column)
  - UNION approach: machines (machinetypeid 15-19) + printers table

### Documentation Added:
- DATA_MIGRATION_EXPLAINED.md - Full migration architecture
- PRODUCTION_MIGRATION_PLAN.md - Production deployment plan
- VIEWS_MIGRATION_ANALYSIS.md - Views requiring updates
- PRINTER_INSTALLER_FIX_2025-11-20.md - Printer installer fixes
- SCHEMA_COMPARISON_REPORT_2025-11-20.md - Phase 2 schema comparison

### ASP Files Updated:
- api_printers.asp - Printer API fixes
- displaynotifications.asp - UI improvements
- install_printer.asp - Installer fixes
- v2/api_printers.asp - V2 API updates
- v2/install_printer.asp - V2 installer updates

### Migration Results (DEV):
- Total machines: 523 (237 equipment + 286 PCs)
- Communications: 1,309
- Warranties: 212
- Machine relationships: 201
- PC migration: 286/286 ✓
- Duplicate PCs removed: 166 duplicates cleaned

### Key Achievements:
✓ All 286 active PCs migrated to machines table
✓ Network devices view updated for Phase 2 architecture
✓ pc_to_machine_id_mapping table populated (286 entries)
✓ Duplicate PC records cleaned (452→286)
✓ Schema updates for API compatibility (pcid→machineid)

### Next Steps:
- Update PHP Dashboard API for Phase 2 schema (CRITICAL - see POWERSHELL_API_PHASE2_ISSUES.md)
- Update PowerShell scripts for Phase 2 schema
- Test Update-PC-CompleteAsset-Silent.bat
- Production deployment planning

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-21 09:15:47 -05:00

9.6 KiB

Printer Installer Batch File Fix

Date: 2025-11-20 Issue: Batch file download from displayprinters/displayprinter pages fails with PowerShell command errors Status: FIXED


Problem Description

When downloading the printer installer batch file (e.g., Install_CSF08-LaserJet-4001.bat) from the displayprinters or displayprinter pages, the batch file would error out with messages like:

'Write-Host' is not recognized as an internal or external command,
operable program or batch file.
'$printerName' is not recognized as an internal or external command,
operable program or batch file.

Root Cause

The ASP code was generating a PowerShell command split across multiple lines in the batch file:

powershell -NoProfile -ExecutionPolicy Bypass -Command "
    Write-Host 'Installing printer with universal driver...' -ForegroundColor Cyan;

    $printerName = 'CSF08-LaserJet-4001';
    $address = 'Printer-10-80-92-58.printer.geaerospace.net';
    ...
"

The CMD batch file interpreter doesn't support multi-line PowerShell commands in this format. It would try to execute the PowerShell code as batch commands, resulting in errors.


Solution

The PowerShell command is now built as a single line with semicolons separating statements:

powershell -NoProfile -ExecutionPolicy Bypass -Command "Write-Host 'Installing printer with universal driver...' -ForegroundColor Cyan; $printerName = 'CSF08-LaserJet-4001'; $address = 'Printer-10-80-92-58.printer.geaerospace.net'; $driverName = 'HP Universal Printing PCL 6'; $portName = 'IP_' + $address; $driver = Get-PrinterDriver -Name $driverName -ErrorAction SilentlyContinue; if (-not $driver) { Write-Host 'ERROR: Universal driver not found!' -ForegroundColor Red; Write-Host 'Please install ' $driverName ' from Windows Update or driver package' -ForegroundColor Yellow; exit 1; }; $port = Get-PrinterPort -Name $portName -ErrorAction SilentlyContinue; if (-not $port) { Write-Host 'Creating printer port...' -ForegroundColor Yellow; Add-PrinterPort -Name $portName -PrinterHostAddress $address -ErrorAction Stop; Write-Host 'Port created successfully' -ForegroundColor Green; } else { Write-Host 'Port already exists' -ForegroundColor Green; }; $existingPrinter = Get-Printer -Name $printerName -ErrorAction SilentlyContinue; if (-not $existingPrinter) { Write-Host 'Adding printer...' -ForegroundColor Yellow; Add-Printer -Name $printerName -DriverName $driverName -PortName $portName -ErrorAction Stop; Write-Host 'Printer installed successfully!' -ForegroundColor Green; } else { Write-Host 'Printer already exists' -ForegroundColor Green; }"

Files Modified

1. /home/camp/projects/windows/shopdb/install_printer.asp

Lines: 180-223 Changes:

  • Added Dim psCommand variable declaration
  • Built PowerShell command as a single concatenated string
  • All PowerShell statements joined with semicolons
  • Single Response.Write() call to output the complete command

2. /home/camp/projects/windows/shopdb/v2/install_printer.asp

Lines: 180-223 Changes:

  • Same fix applied to v2 version

Code Changes Summary

Before (Lines 181-224):

' Generate PowerShell script to install printer
Response.Write("powershell -NoProfile -ExecutionPolicy Bypass -Command """ & vbCrLf)
Response.Write("    Write-Host 'Installing printer with universal driver...' -ForegroundColor Cyan;" & vbCrLf)
Response.Write("    " & vbCrLf)
Response.Write("    $printerName = '" & Replace(printer("name"), "'", "''") & "';" & vbCrLf)
' ... 40+ more lines of multi-line output ...

After (Lines 180-223):

' Build PowerShell command as single line
Dim psCommand
psCommand = "Write-Host 'Installing printer with universal driver...' -ForegroundColor Cyan; "
psCommand = psCommand & "$printerName = '" & Replace(printer("name"), "'", "''") & "'; "
psCommand = psCommand & "$address = '" & Replace(printer("address"), "'", "''") & "'; "
' ... build complete command as single string ...

' Write PowerShell command as single line
Response.Write("powershell -NoProfile -ExecutionPolicy Bypass -Command """ & psCommand & """" & vbCrLf)

Testing Instructions

Test 1: Download from displayprinters.asp

  1. Navigate to http://192.168.122.151:8080/displayprinters.asp
  2. Find any HP printer (e.g., CSF08-LaserJet-4001)
  3. Click the download installer button
  4. Save the .bat file to Downloads
  5. Open CMD and run the batch file
  6. Expected: PowerShell commands execute successfully, printer installs

Test 2: Download from displayprinter.asp

  1. Navigate to http://192.168.122.151:8080/displayprinter.asp?printerid=X
  2. Click the download installer button
  3. Save the .bat file to Downloads
  4. Open CMD and run the batch file
  5. Expected: PowerShell commands execute successfully, printer installs

Test 3: Verify Universal Driver Detection

For HP Printers:

  • Should use "HP Universal Printing PCL 6"
  • Checks if driver exists before proceeding
  • Errors with clear message if driver not installed

For Xerox Printers:

  • Should use "Xerox Global Print Driver PCL6"

For Generic/Unknown:

  • Should use "Generic / Text Only"

Expected Output

Successful Installation

========================================
 GE Aerospace Printer Installer
========================================

Installing 1 printer(s)...

----------------------------------------
Installing: CSF08-LaserJet-4001
CSF Name: CSF08
Model: LaserJet Pro 4001n
Address: Printer-10-80-92-58.printer.geaerospace.net
----------------------------------------

Using universal driver: HP Universal Printing PCL 6

Installing printer with universal driver...
Port already exists
Printer already exists

========================================
Installation Complete!
========================================

Press any key to continue . . .

Driver Not Found

ERROR: Universal driver not found!
Please install  HP Universal Printing PCL 6  from Windows Update or driver package

Issue Type

  • Bug Fix
  • Batch File Generation
  • PowerShell Integration

Affected Functionality

  • Printer installer downloads (universal driver method)
  • Printers without custom installpath in database

Not Affected

  • Printers with custom installpath (uses different code path)
  • Manual printer installation
  • Printer display/search functionality

Technical Details

Why Single-Line PowerShell?

When using powershell.exe -Command "...", the command must be:

  1. A single line, OR
  2. Use proper batch line continuation with ^, OR
  3. Use -File with a .ps1 script

The original code attempted multi-line without proper continuation, causing the batch interpreter to misinterpret PowerShell code as batch commands.

Alternative Solutions Considered

Option 1: Use caret (^) for line continuation

powershell -Command "Write-Host 'Test'; ^
    $var = 'value'; ^
    Do-Something"

Rejected: More complex escaping, harder to maintain

Option 2: Generate temporary .ps1 file

echo $printerName = 'Test' > %TEMP%\install.ps1
echo Add-Printer ... >> %TEMP%\install.ps1
powershell -File %TEMP%\install.ps1

Rejected: More file I/O, cleanup required, potential security issues

Option 3: Single-line command (CHOSEN)

powershell -Command "Write-Host 'Test'; $var = 'value'; Do-Something"

Selected: Simplest, most reliable, no temp files needed


Validation

Code Review Checklist

  • Both install_printer.asp files updated (main and v2)
  • PowerShell command built as single string
  • All statements joined with semicolons
  • Single quotes properly escaped with Replace()
  • Error handling preserved
  • Driver detection logic maintained
  • Port creation logic maintained
  • Printer installation logic maintained

Testing Checklist

  • Test HP printer download and install
  • Test Xerox printer download and install
  • Test printer without universal driver
  • Test printer already installed scenario
  • Test port already exists scenario
  • Verify error messages display correctly
  • Verify batch file self-deletes after execution

Deployment

Deployment Status

  • Code changes complete
  • Both files (main and v2) updated
  • Testing pending
  • Production deployment pending

Rollback Plan

If issues arise, revert to previous version:

git checkout HEAD~1 install_printer.asp
git checkout HEAD~1 v2/install_printer.asp

Additional Notes

Printers with Custom Installers

Printers with a value in the installpath database field use a different code path and are NOT affected by this bug. They download and execute a custom .exe installer.

Universal Driver Availability

The universal drivers must be installed on the target PC:

  • HP Universal Printing PCL 6 - Available via Windows Update
  • Xerox Global Print Driver PCL6 - Requires vendor download

If the driver is not installed, the script will error with clear instructions.


Author: Claude Code Reviewed By: Pending Status: Ready for Testing Production Ready: After successful testing


Quick Reference

Bug: Multi-line PowerShell commands in batch file Fix: Build PowerShell command as single line with semicolons Impact: Universal driver printer installations Files: install_printer.asp (both main and v2) Testing: Download .bat file from printer pages and execute