【发布时间】:2016-09-12 13:27:32
【问题描述】:
我被分配了从 VBScript 将旧脚本重写为 PowerShell 的任务。该脚本基本上只是读取文本文件的一行并安装该行对应的打印机,然后再次将其删除,这样就只安装了驱动程序。
这个脚本每天都会在我们的虚拟 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 中的可用信息(驱动程序列表)来实现我想要做的事情?
同样,最终结果应该只是使用提供的信息将驱动程序安装在终端服务器上。
【问题讨论】:
标签: sql-server-2008 powershell printing windows-server-2008