无法让 pnputil.exe 在通过 MS Intune 推送的 PowerShell 脚本中运行

无法让 pnputil.exe 在通过 MS Intune 推送的 PowerShell 脚本中运行

几天来我一直在努力通过 Intune 让这个脚本运行。下面的脚本用于检查是否安装了打印机,如果没有,则从我们的 blob 存储库下载驱动程序,提取,添加到驱动程序存储区,然后添加打印机。

该脚本在本地运行时运行良好,但是,当我通过 Intune 将其推送到我们的 Windows 10 设备时,没有添加打印机,并且 Intune 告诉我该脚本无法在目标设备上运行,但我不确定原因。

脚本在以 开头的行处失败$pnpOutput,直到该行完成为止的所有内容。在该行之后,add-printerport成功(打印机端口已添加到计算机),但没有其他操作,因为所有其他命令都需要安装驱动程序,但事实并非如此。检查C:\Windows\INF目标计算机可确认驱动程序未添加到驱动程序存储中。

我认为问题与我使用 powershell 调用的方式有关pnputil.exe。我尝试了几种方式来表达命令,但没有结果。我对 powershell 还不熟悉,但知道足够多的东西来组合几个脚本并进行一些基本的故障排除。

我尝试添加一些错误日志,但没有成功。我尝试添加out-file$pnpoutput行,但生成的文件为空。我尝试在整个最后一个块周围添加一个 try / catch,但我的日志文件甚至没有创建(甚至在本地也没有创建,即使我故意破坏脚本以导致错误)。

# This script installs the US Bizhub C368 printer
# This checks to see if the printer has already been added
$CheckPrinter = Get-printer | where {$_.Name -like "US Bizhub C368"}
If ($CheckPrinter -eq $null) {

# Make IT folder for driver download
$ITFolder = "C:\IT"
New-Item -Path $ITFolder -ItemType Directory

# Download the driver from Azure Blob repository
$source = "OurAzureBlobURL.com"
$zipdestination = "$ITFolder\USBizhubC368Driver.zip"
Invoke-WebRequest $source -OutFile $zipdestination

# Extract the zip archive and delete the zip
$unzippeddestination = "$ITFolder\USBizhubC368Driver"
Expand-Archive -Path $zipdestination -DestinationPath $unzippeddestination
Remove-Item -Path $zipdestination

$pnpOutput = pnputil -a "$unzippeddestination\KOAXWJ__.INF" | Select-String "Published name"
$null = $pnpOutput -match "Published name :\s*(?<name>.*\.inf)"
$driverINF = Get-ChildItem -Path C:\Windows\INF\$($matches.Name)
Add-PrinterDriver -Name "KONICA MINOLTA C368SeriesPCL" -InfPath $driverINF.FullName
Add-PrinterPort -Name "US Bizhub C368" -PrinterHostAddress "192.168.121.20"
Add-Printer -Name "US Bizhub C368" -DriverName "KONICA MINOLTA C368SeriesPCL" -PortName "US Bizhub C368"

}

有人有什么想法吗?我愿意尝试一些尝试,所以如果你知道如何让日志记录正常工作,我很乐意设置它,通过 Intune 推送它并报告错误消息。Powershell 不是我的第一语言(目前),所以如果可能的话,我可能需要稍微简化一下答案 :)

谢谢你!

答案1

在研究了其他与您想要实现的目标类似的脚本后,我完全修改了我的答案。

请尝试以下操作:

# This script installs the US Bizhub C368 printer
# This checks to see if the printer has already been added
$CheckPrinter = Get-printer | where {$_.Name -like "US Bizhub C368"}
If ($CheckPrinter -eq $null) {

# Make IT folder for driver download
$ITFolder = "C:\IT"
New-Item -Path $ITFolder -ItemType Directory

# Download the driver from Azure Blob repository
$source = "OurAzureBlobURL.com"
$zipdestination = "$ITFolder\USBizhubC368Driver.zip"
Invoke-WebRequest $source -OutFile $zipdestination

# Extract the zip archive and delete the zip
$unzippeddestination = "$ITFolder\USBizhubC368Driver"
Expand-Archive -Path $zipdestination -DestinationPath $unzippeddestination
Remove-Item -Path $zipdestination

if($env:PROCESSOR_ARCHITECTURE -eq "x86"){
    Start-Process "$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -WorkingDirectory $ITFolder -ArgumentList "pnputil /add-driver *.inf /subdirs /install | Out-File -FilePath (Join-Path $ITFolder '\Install-Drivers.txt')" -NoNewWindow -Wait
}
elseif($env:PROCESSOR_ARCHITECTURE -eq "AMD64"){
    Start-Process "powershell.exe" -WorkingDirectory $ITFolder -ArgumentList "pnputil /add-driver *.inf /subdirs /install | Out-File -FilePath (Join-Path $ITFolder '\Install-Drivers.txt')" -NoNewWindow -Wait
}

[String]$pnpOutput = Get-Content "$ITFolder\Install-Drivers.txt" | Select-String "Published Name"
$pnpOutput -match "Published name:\s*(?<name>.*\.inf)"
$driverINF = Get-ChildItem -Path C:\Windows\INF\$($matches.Name)
Add-PrinterDriver -Name "KONICA MINOLTA C368SeriesPCL" -InfPath $driverINF.FullName
Add-PrinterPort -Name "US Bizhub C368" -PrinterHostAddress "192.168.121.20"
Add-Printer -Name "US Bizhub C368" -DriverName "KONICA MINOLTA C368SeriesPCL" -PortName "US Bizhub C368"

}

相关内容