###
# Autor: Pavel Valach <paulos@sin.cvut.cz>
###
$old_ErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# ------------- Definice proměnných ----------------
$PSScriptRoot = Split-Path $MyInvocation . MyCommand . Path -Parent
$driver = @ {
'Url' = $null ;
'SHA256' = $null ;
'CabOutputPath' = $null ;
'CabExtractPath' = " $ PSScriptRoot\hp-driver-dir " ;
'InfFilePath' = " $ PSScriptRoot\hp-driver-dir\prnhp002.inf " ;
'Name' = 'HP Color LaserJet CP5220 Series PCL6' ;
}
$printer = @ {
'PortName' = '\\tiskarna.sin.cvut.cz\Sincoolka-ColorLaserJet' ;
'Name' = 'Sincoolka Color LaserJet' ;
}
# ------------- Funkce -------------
function Exit-Gracefully
{
Read-Host " Stiskněte Enter a já se ukončím "
exit
}
# ------------- Výběr ovladače na základě architektury ----------------
$OSarch = ( Get-CimInstance Win32_operatingsystem ) . OSArchitecture
if ( $OSarch -like " *64* " )
{
$driver . Url = 'http://download.windowsupdate.com/msdownload/update/driver/drvs/2011/07/4753_fc148f3df197a4c5cf20bd6a8b337b444037655f.cab'
$driver . SHA256 = '36E4ADBA837C391E70219F64C31E20FC64EF5A0C11BFDA8170540DBECC4EF42F'
$driver . CabOutputPath = " $ PSScriptRoot\hp-driver-amd64.cab "
}
else
{
$driver . Url = 'http://download.windowsupdate.com/msdownload/update/driver/drvs/2011/07/4754_10c77b0c78335f0fa4334ea932d5c0b4a653880a.cab'
$driver . SHA256 = '864EE4D1E3C4914BF37535D15952A7981186048604F7311D005A156F61061599'
$driver . CabOutputPath = " $ PSScriptRoot\hp-driver-x86.cab "
}
# ------------- Úvodní znělka ----------------
$caption = " Instalace tiskárny "
$message = " Tento skript samostatně nainstaluje síťovou kolejní tiskárnu HP LaserJet CP5225dn a ovladače k ní. Pokud nebyly ovladače přiloženy, stáhne je z Windows Update.
Začneme ? "
$yes = new-Object System . Management . Automation . Host . ChoiceDescription " &Ano " , " Započne stahování a instalaci. "
$no = new-Object System . Management . Automation . Host . ChoiceDescription " &Ne " , " Neudělá nic a ukončí skript. "
$choices = [System.Management.Automation.Host.ChoiceDescription[]] ( $yes , $no )
$answer = $host . ui . PromptForChoice ( $caption , $message , $choices , 0 )
if ( $answer -eq 1 )
{
Exit-Gracefully
}
if ( ( $result = Read-Host " Zadejte, jak chcete, aby se tiskárna jmenovala na vašem počítači.
Nebo stiskněte Enter a použije se výchozí jméno [ $ ( $printer . Name ) ] " ) -ne '')
{
$printer . Name = $result
}
# ------------- Test, zda je možné se připojit k tiskárně ---------------
Write-Information " Zkouším připojení k tiskárně... " -InformationAction Continue
cmdkey / add : tiskarna . sin . cvut . cz / user : printer / pass : Sinc00lkaTiskarna
net use " \\tiskarna.sin.cvut.cz "
if ( $LastExitCode -gt 0 )
{
Write-Warning " K tiskárně se nezdařilo připojit.
Prosím překontrolujte si , zda jste připojeni k síti klubu Sincoolka . Tisk funguje na kabelu i wi-fi .
Přes eduroam tisk nefunguje !
Nemohu pokračovat dále , končím . " -WarningAction Continue
Exit-Gracefully
}
# ------------- Test, zda je nutné soubor stáhnout znovu ----------------
$needsDriverDownload = $true
$cabFileExists = Test-Path $driver . CabOutputPath
if ( $cabFileExists -eq $true )
{
$hash = Get-FileHash -Algorithm SHA256 $driver . CabOutputPath
if ( $hash . Hash -eq $driver . SHA256 )
{
$needsDriverDownload = $false
}
}
if ( $needsDriverDownload )
{
Write-Warning -Message " Soubor s ovladačem neexistuje nebo je poškozen. Stahuji z Windows Update... "
Import-Module BitsTransfer
Start-BitsTransfer -Source $driver . Url -Destination $driver . CabOutputPath -TransferPolicy Always
}
Write-Information -MessageData " Extrahuji ovladač... " -InformationAction Continue
# Extract the CAB
New-Item $driver . CabExtractPath -ItemType Directory -Force
expand . exe $driver . CabOutputPath -F : * $driver . CabExtractPath
Write-Information -MessageData " Instaluji ovladač... " -InformationAction Continue
# Install drivers from the extracted file
pnputil . exe -a $driver . InfFilePath
# Add printer driver which we just installed
Add-PrinterDriver -Name $driver . Name
Write-Information -MessageData " Přidávám do systému tiskárnu $( $printer . Name ) ... " -InformationAction Continue
Add-PrinterPort -Name $printer . PortName -ErrorAction SilentlyContinue
# this line should be executed as normal user, otherwise only admin can manage the printer
Add-Printer -Name $printer . Name -DriverName $driver . Name -PortName $printer . PortName -ErrorAction Continue
Write-Information -MessageData " Provádím úklid... " -InformationAction Continue -ErrorAction Continue
Remove-Item $driver . CabExtractPath -Recurse
$ErrorActionPreference = $old_ErrorActionPreference
Write-Information " Hotovo! " -InformationAction Continue
Exit-Gracefully