Can't get Where-Object to work on Imported CSV

This topic contains 5 replies, has 3 voices, and was last updated by  Kevyn 2 months, 1 week ago.

  • Author
    Posts
  • #77101

    Karson Van Meeteren
    Participant

    Here is the structure of my dupelog.csv:
    COMPUTERNAME,USERNAME,PRINTERNAME
    RUN5V3N502B,kvanmeeteren,RUNM7V-RAMS1
    RUN5V3N502B,kvanmeeteren,RUNM7V-RAMS1
    RUN5V3N502B,kvanmeeteren,RUNM7V-RAMS1

    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
    		$computerName
    		pause
    		$userName = $data.username
    		$userName
    		pause
    		$printerName = $data.printername
    		$printerName
    		pause
    		$printerName = $printerName.TrimStart("\\printserver\")
    		$printerName
    		pause
    		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" }
    		$dupeCheck
    		pause
    		#$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 printmigration@donotreply.com -To recipient@gmail.com -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" }

  • #77104

    Don Jones
    Keymaster

    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?

    • #77110

      Karson Van Meeteren
      Participant

      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!

  • #77122

    Karson Van Meeteren
    Participant

    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.

  • #77254

    Kevyn
    Participant

    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

  • #77256

    Kevyn
    Participant

    Corrected the Set-PSBreakPoint command for my earlier post.

You must be logged in to reply to this topic.