2016-09-12 92 views
0

我被賦予了從VBScript中將舊腳本重寫爲PowerShell的任務。這個腳本基本上只是讀取一行文本文件並安裝與該行相對應的打印機,然後再次刪除它,以便只安裝驅動程序。用於Windows Server 2008 R2的添加打印機,刪除打印機等的替代方案

該腳本每天在我們的虛擬Citrix終端服務器上執行,這樣我們就可以獨立於當前發佈的映像更新驅動程序。

下面是最終腳本如下:

# Variables 
Param([string]$FileName) 
$DriverList = Get-Content $FileName 
$ComputerName = hostname 
$LogFile = "C:\Windows\Logs\PrinterCreation.log" 
$SeperatorL = "═══════════════════════════════════════════════════════════════════════════════════════════════════════" 
$SeperatorS = "═══════════════════════════════════════════" 
$StartDate = Get-Date -Format "dd.MMM.yyyy HH:mm:ss" 

# Log Header 
"$SeperatorL" > $LogFile 
" ServerName: $ComputerName" >> $LogFile 
" DriverList: $FileName" >> $LogFile 
" StartTime: $StartDate" >> $LogFile 
"$SeperatorL" >> $LogFile 
"" >> $LogFile 
"Beginning driver instalation process:" >> $LogFile 

# Process the "$DriverList" by installing each printer on the list and deleting the connection afterwards 
foreach ($line in $DriverList) { 
    "$SeperatorL" >> $LogFile 
    " Print driver Installation: $line" >> $LogFile 

    # Installing the printer 
    try { 
     Add-Printer -ConnectionName $line -ErrorAction Stop 
     Start-Sleep -s 10 

     # Deleting the Printer 
     try { 
      Remove-Printer -Name $line -ErrorAction Stop 
      " Printer installation successfull." >> $LogFile 
     } catch { 
      " INSTALATION NOT COMPLETE:  Printer was installed but connection could not be removed!" >> $LogFile 
     } 
    } catch [Microsoft.Management.Infrastructure.CimException] { 
     " INSTALATION FAILED:   Printer driver cannot be found or does not exist!" >> $LogFile 
    } finally { 
     Start-Sleep -s 5 
    } 
} 

# Log Footnote 
$EndDate = Get-Date -Format "HH:mm:ss" 
"$SeperatorL" >> $LogFile 
"" >> $LogFile 
"Instalation process completed:" >> $LogFile 
"$SeperatorS" >> $LogFile 
" End Time: $EndDate" >> $LogFile 
"$SeperatorS" >> $LogFile 

它被稱爲像這樣:.\scriptfilename.ps1 ".\Driverlist.txt"

的驅動程序列表中只包含這樣的行:"\\spbdn140\KM_Universal_PCL"

短的問題

當寫這個腳本時,我沒有意識到打印機管理模塊只能從Win 8和Server 2012向上工作。我們的終端服務器都運行Server 2008.

有沒有什麼辦法可以實現我想用Server 2008上的PowerShell v3-4中的可用信息(驅動程序列表)來做什麼?

此外,最終的結果應該是僅使用所提供的信息將驅動程序安裝在終端服務器上。

回答