Author Posts

August 10, 2017 at 1:47 pm

Here is the structure of my dupelog.csv:

and the relevant portion of my script (there is a lot of variable output just to doublecheck it is correct)

$dupeLog = "\\server\temp\PrintServerMigration\failures.csv"
$files = Get-ChildItem -Path \\server\temp\PrintServerMigration\*.csv | Where-Object { $_.LastWriteTime -ge (Get-Date).Date -and $_.Name -notlike "failures.csv" } | Select-Object -ExpandProperty fullname

foreach ($file in $files)
	Write-Host "Processing file $file"
	$data = Import-Csv -Path $file | Where-Object { $_.returncode_errormessage -eq '3019' } | Get-Unique
	if ($data -ne $null)
		$computerName = $data.computername
		$userName = $data.username
		$printerName = $data.printername
		$printerName = $printerName.TrimStart("\\printserver\")
		Write-Host "Found 3019 Error on $computerName - checking log to prevent duplicate notifications"
		$dupeCheck = Import-Csv -Path $dupeLog | Where-Object { $_.computername -eq "$computerName" -and $_.username -eq "$userName" -and $_.printername -eq "$printerName" }
		#$printerDriver = Invoke-Command printserver -ScriptBlock { Get-printer $using:printerName | Select-Object -ExpandProperty DriverName }
		if ($dupeCheck -eq $null)
			$printerDriver = Invoke-Command printserver -ScriptBlock { Get-printer $using:printerName | Select-Object -ExpandProperty DriverName }
			#Send-MailMessage -SmtpServer mailserver -From -To -Subject "Failed driver install for $printerName for $userName on $computerName ($printerDriver)"
			Write-Host "No previous errors logged for this. Emailing HelpSTAR and adding to the log so no duplicate emails get sent"

This is the particular line that isn't returning anything. I've tried all sorts of different things in the where-object, but nothing ever gets returned.

$dupeCheck = Import-Csv -Path $dupeLog | Where-Object { $_.computername -eq "$computerName" -and $_.username -eq "$userName" -and $_.printername -eq "$printerName" }

August 10, 2017 at 1:51 pm

Oy, Write-Host. Sigh. The struggle is real. I might humbly suggest you look into Write-Verbose and Write-Debug as more sustainable ways to do what you're doing ;).

So, I'd obviously question what was in $computerName, $userName, and $printerName when you run this. For example, based on your sample data:

$dupeCheck = Import-Csv -Path $dupeLog | Where-Object { $_.computername -eq "RUN5V3N502B" -and $_.username -eq "kvanmeeteren" -and $_.printername -eq "RUNM7V-RAMS1" }

Does that return anything?

August 10, 2017 at 1:56 pm

Bad habits are hard to kick. It's just for testing my output at the moment, and will be gone once I get around this weird issue.

EDIT: I copied the actual values like you suggested and IT WORKS. So, why when I use the variables does it not? They come back exactly as intended when I blat them out one by one.

I swear the bulk of my issues come down to multiple condition checks when scripting. That is where the real struggle for me is!

August 10, 2017 at 2:37 pm

This is the reason. So after I send the email, I write to the log so it gets recorded as an already notified entry.

I was using $env:computername, instead of $computername, so it was never seeing it as a duplicate entry from the system I was executing the script from. Duh.

August 12, 2017 at 12:52 am

I see my post was too late, but here's other ways to debug a script when you run into something like this in the future:

In addition to Write-Debug, you might try debugging your script by setting up a PS breakpoint for the $ComputerName, $UserName, & $PrinterName variables to follow their values as the script runs. When you hit a breakpoint, it will tell you which variable it broke for, and whether it was a read or a write operation. For each time it breaks, you can manually dump out the variable. You might even put a Write-Debug statement before it breaks on reading the variables for the importing of the importing of the failures.csv file to say something like "About to import the failures.csv.". That way, you can see what the value of each variable is when the import of the failures.csv is being done.

Set-PSBreakpoint -Script -Variable ComputerName,UserName,PrinterName -Mode ReadWrite

August 12, 2017 at 3:49 am

Corrected the Set-PSBreakPoint command for my earlier post.