#prtdrv.ps1 - Printer driver installation script (called from prtinst.ps1) param( [string]$drivername ,[uint64]$driververnew ,[string]$driverfileprefix ,[string]$driverfileinstaller ,[byte]$driverdodownload ) Set-Variable -Name FNPRTDRV_DRIVER_CHECK_RC_INSTALLED -Value 1 -Option Constant -Visibility Private Set-Variable -Name FNPRTDRV_DRIVER_CHECK_RC_INSTALL -Value 2 -Option Constant -Visibility Private Set-Variable -Name FNPRTDRV_DRIVER_VERSIONCHECK_RC_NOACTION -Value 0 -Option Constant -Visibility Private Set-Variable -Name FNPRTDRV_DRIVER_VERSIONCHECK_RC_UPGRADE -Value 1 -Option Constant -Visibility Private #---------------------------------------------- # Function: fnprtdrv_driver_check # Returns: $RC_ERROR - error # $FNPRTDRV_DRIVER_CHECK_RC_INSTALL - install driver # $FNPRTDRV_DRIVER_CHECK_RC_INSTALLED - driver already installed #---------------------------------------------- function fnprtdrv_driver_check ([string]$drvname, [uint64]$drvvernew, [string]$drvfileprefix, [string]$drvfileinstaller, [byte]$drvdodownload) { if (Get-PrinterDriver | Where-Object {$_.Name -eq $drvname}) { Write-Host "Driver"$drvname" already installed" if ($drvvernew -eq "") { return $FNPRTDRV_DRIVER_CHECK_RC_INSTALLED } else { # if driver version was specified $returncode = fnprtdrv_driver_versioncheck -drvname $drvname -drvvernew $drvvernew if ($returncode -eq $FNPRTDRV_DRIVER_VERSIONCHECK_RC_UPGRADE) { if ($drvdodownload -eq 1) { if ((fnprtdrv_driver_download -drvname $drvname -drvfileprefix $drvfileprefix -drvfileinstaller $drvfileinstaller) -eq $RC_ERROR) { return $RC_ERROR } } # I think we need to install the driver after we upgrade return $FNPRTDRV_DRIVER_CHECK_RC_INSTALL } } } else { if ($drvdodownload -eq 1) { if ((fnprtdrv_driver_download -drvname $drvname -drvfileprefix $drvfileprefix -drvfileinstaller $drvfileinstaller) -eq $RC_ERROR) { return $RC_ERROR } } return $FNPRTDRV_DRIVER_CHECK_RC_INSTALL } } #---------------------------------------------- # Function: fnprtdrv_driver_versioncheck # Returns: $FNPRTDRV_DRIVER_VERSIONCHECK_RC_NOACTION - install driver # $FNPRTDRV_DRIVER_VERSIONCHECK_RC_UPGRADE - driver already installed #---------------------------------------------- function fnprtdrv_driver_versioncheck ([string]$drvname, [uint64]$drvvernew) { # if driver version was specified $drvvercurrent = (Get-PrinterDriver -Name $drvname).DriverVersion if ($drvvercurrent -eq $drvvernew) { Write-Host ("Same driver version already installed (" + $drvvercurrent + ")") } elseif ($drvvernew -lt $drvvercurrent) { Write-Host ("Specified driver version (" + $drvvernew + ") is lower than the current one (" + $drvvercurrent + ")") } else { Write-Host ("Specified driver version (" + $drvvernew + ") is higher than the current one (" + $drvvercurrent + ")") if ((Read-Host "Enter Y to attempt upgrade") -eq "Y") { return $FNPRTDRV_DRIVER_VERSIONCHECK_RC_UPGRADE } } return $FNPRTDRV_DRIVER_VERSIONCHECK_RC_NOACTION } #---------------------------------------------- # Function: fnprtdrv_driver_download # Returns: #---------------------------------------------- function fnprtdrv_driver_download ([string]$drvname, [string]$drvfileprefix, [string]$drvfileinstaller) { #specify paths with trailing slash $dfile_dir_remote = "http://support.ddssoft.com/download/Other_Files/hp/" $dfile_dir_local = $env:temp + "\ddsprtinst\" $dfile_dir_extract = $dfile_dir_local + "ext\" if ([environment]::Is64BitOperatingSystem) { Write-Host "64-bit operating system detected" $dfile_filename_sha = $drvfileprefix + "64.sha" $dfile_filename_driver = $drvfileprefix + "64.zip" $dfile_filename_installer = $drvfileinstaller + "64.exe" } else { Write-Host "32-bit operating system detected" $dfile_filename_sha = $drvfileprefix + "32.sha" $dfile_filename_driver = $drvfileprefix + "32.zip" $dfile_filename_installer = $drvfileinstaller + "32.exe" } if ((Test-Path ($dfile_dir_local)) -eq $false) { #if directory doesn't exist create it New-Item $dfile_dir_local -type directory Write-Host "Created directory" $dfile_dir_local } else { Write-Host "Directory already exists" $dfile_dir_local if ((Test-Path ($dfile_dir_extract)) -eq $true) { #if the extract folder exists empty it Remove-Item ($dfile_dir_extract + "*") } } if ((fnprtdrv_driver_wget_checksum -remotefile ($dfile_dir_remote + $dfile_filename_sha) -outfile ($dfile_dir_local + $dfile_filename_sha)) -eq $false) { #if checksum download fails return $RC_ERROR } if ((Test-Path ($dfile_dir_local + $dfile_filename_driver)) -eq $true) { Write-Host "Driver file found in temp" if ((fnprtdrv_driver_checksum -pathsha ($dfile_dir_local + $dfile_filename_sha) -pathfile ($dfile_dir_local + $dfile_filename_driver)) -eq $RC_SUCCESS) { fnprtdrv_driver_unzip -file ($dfile_dir_local + $dfile_filename_driver) -path ($dfile_dir_extract) if (fnprtdrv_driver_runinstall -file ($dfile_dir_extract + $dfile_filename_installer) -eq $RC_ERROR) { return $RC_ERROR } return $RC_SUCCESS } } if ((fnprtdrv_driver_wget_driver -remotefile ($dfile_dir_remote + $dfile_filename_driver) -outfile ($dfile_dir_local + $dfile_filename_driver)) -eq $false) { #if driver download fails return $RC_ERROR } elseif ((fnprtdrv_driver_checksum -pathsha ($dfile_dir_local + $dfile_filename_sha) -pathfile ($dfile_dir_local + $dfile_filename_driver)) -eq $RC_ERROR) { #if checksum fails return $RC_ERROR } else { fnprtdrv_driver_unzip -file ($dfile_dir_local + $dfile_filename_driver) -path ($dfile_dir_extract) if (fnprtdrv_driver_runinstall -file ($dfile_dir_extract + $dfile_filename_installer) -eq $RC_ERROR) { return $RC_ERROR } } } #---------------------------------------------- # Function: fnprtdrv_driver_wget_checksum # Returns: #---------------------------------------------- function fnprtdrv_driver_wget_checksum ([string]$remotefile, [string]$outfile) { Write-Host "Downloading checksum file"$dfile_filename_sha wget $remotefile -OutFile $outfile $result = $? if ($result -eq $false) { Write-Host "Error downloading checksum file" -BackgroundColor Red } return $result } #---------------------------------------------- # Function: fnprtdrv_driver_checksum # Returns: $RC_SUCCESS - verified # $RC_ERROR - failed #---------------------------------------------- function fnprtdrv_driver_checksum ([string]$pathsha,[string]$pathfile) { $hash_checksumin = Get-Content ($pathsha) $hash_checksumcalc = Get-FileHash -Path $pathfile -Algorithm SHA1 if (($hash_checksumin | Where-Object {$_ -like "sha1:*"}).Replace("sha1: ","") -eq $hash_checksumcalc.Hash ) { Write-Host "Checksum verified for"(Split-Path $pathfile -leaf) -BackgroundColor DarkGreen return $RC_SUCCESS } else { Write-Host "Checksum failed for"(Split-Path $pathfile -leaf) -BackgroundColor Red return $RC_ERROR } } #---------------------------------------------- # Function: fnprtdrv_driver_wget_driver # Returns: #---------------------------------------------- function fnprtdrv_driver_wget_driver ([string]$remotefile, [string]$outfile) { Write-Host "Downloading driver file"(Split-Path $outfile -leaf) wget $remotefile -OutFile $outfile $result = $? if ($result -eq $false) { Write-Host "Error downloading driver file" -BackgroundColor Red } return $result } #---------------------------------------------- # Function: fnprtdrv_driver_unzip # Returns: #---------------------------------------------- function fnprtdrv_driver_unzip ([string]$file,[string]$path) { Add-Type -assembly "system.io.compression.filesystem" Write-Host "Extracting content of zip file" [io.compression.zipfile]::ExtractToDirectory($file, $path) } #---------------------------------------------- # Function: fnprtdrv_driver_runinstall # Returns: $RC_SUCCESS, $RC_ERROR #---------------------------------------------- function fnprtdrv_driver_runinstall ([string]$file) { Write-Host "Starting driver installer" Start-Process $file -Verb RunAs -Wait if ($? -eq $false) { Write-Host "Error running driver installer" -BackgroundColor Red return $RC_ERROR } else { return $RC_SUCCESS } } #---------------------------------------------- # Function: fnprtdrv_driver_install # Returns: $RC_SUCCESS, $RC_ERROR #---------------------------------------------- function fnprtdrv_driver_install ([string]$drvname) { Write-Host "Installing driver"$drvname Add-PrinterDriver -Name $drvname if ($? -eq $false) { Write-Host "Error installing driver" -BackgroundColor Red return $RC_ERROR } else { return $RC_SUCCESS } } #---------------------------------------------- $returncode = fnprtdrv_driver_check -drvname $drivername -drvvernew $driververnew -drvfileprefix $driverfileprefix -drvfileinstaller $driverfileinstaller -drvdodownload $driverdodownload if ($returncode -eq $RC_ERROR) { return $RC_ERROR } elseif ($returncode -eq $FNPRTDRV_DRIVER_CHECK_RC_INSTALLED) { return $RC_SUCCESS } elseif ($returncode -eq $FNPRTDRV_DRIVER_CHECK_RC_INSTALL) { $returncode = fnprtdrv_driver_install -drvname $drivername -eq $RC_ERROR } return $returncode