2016-09-12 90 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中的可用信息(驱动程序列表)来做什么?

此外,最终的结果应该是仅使用所提供的信息将驱动程序安装在终端服务器上。

回答