0

I've been struggling to get this script working via Intune for several days. The below is a script that checks to see if a printer is installed, if not, the driver is downloaded from our blob repository, extracted, added to the driver store, then the printer is added.

The script works perfectly when running locally, however, when I push it out via Intune to our Windows 10 devices, no printer is added, and Intune tells me that the script failed to run on the targeted devices, but I'm unsure why.

The script is failing at the line starting with $pnpOutput, everything up until that line is completed. After that line, add-printerport is successful (printer port is added to the computer), but nothing else is because all of the other commands require the driver to be installed, which it isn't. Checking C:\Windows\INF on the target computer confirms that the driver was not added to the driver store.

I figure the problem has something to do with the way I am calling pnputil.exe with powershell. I've tried phrasing the command a few ways with no result. I'm fairly new to powershell, but know enough to mash a couple of scripts together and do some basic troubleshooting.

I tried adding some error logging, but wasn't successful. I tried to out-file the $pnpoutput line but the resulting file was empty. I tried adding a try / catch around the whole last block, but my log file wasn't even created (not even locally, even when I broke the script on purpose to cause an error).

# 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"

}

Does anyone have any ideas? I'm up for some trial and error, so if you know how to make the logging work correctly, I'm happy to set that up, push it via Intune and report back with the error message. Powershell isn't my first language (yet), so I may need answers dumbed-down a little if possible :)

Thank you!

0

I have completely revised my answer after looking into other scripts that do similar to what you want to achieve taking intune into consideration.

Please try the below:

# 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"

}
  • Thanks for the response! I'll give it a shot and report back. I'm not sure that's the issue, however. I say that because the driver is supposed to get installed one line above ($pnpOutput = pnputil -a "$unzippeddestination\KOAXWJ__.INF" | Select-String "Published name"), but it never even installs when pushed through Intune. The 2 lines below that line just help us figure out the name of the driver once it is installed so that we can pass it to add-printerdriver. If you see a flaw in my logic, please point it out, I'm here to learn! – YouGotServered Oct 2 '18 at 17:33
  • Apologies I had only picked up on the intune element of your question afterwards. Whilst I don't have experience with intune I do with driver installation. How are you testing that pnputil is working? Where you are declaring a variable it may not allow output to file may be worth stripping the variable out and piping the output of pnputil directly either with out-file/ appending the line with "> somedestination\pnputil.txt" – CraftyB Oct 2 '18 at 18:34
  • No worries! Just to confirm, I pushed the revised script with your first suggestion, and no dice. – YouGotServered Oct 2 '18 at 19:15
  • I'm testing pnputil by running the script locally and then verifying that the OEM##.inf is present in Windows and that I can add-printerdriver using the $driverinfvariable successfully. Unfortunately, when the script is run via Intune on a device, the INF never shows in C:\Windows\INF, however the extracted contents are in the IT folder, and add-printerport successfully added the port. From that evidence, I can assume everything in the script worked, with the exception of pnputil.exe. – YouGotServered Oct 2 '18 at 19:22
  • I'll try pnputil -a "$unzippeddestination\KOAXWJ__.INF" | Select-String "Published name" | out-file c:\IT\Printerlog.txt to see if I can get some data from that. I'll report back asap. – YouGotServered Oct 2 '18 at 19:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.