If Statement part deux

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Mark Prior Mark Prior 3 months, 2 weeks ago.

  • Author
    Posts
  • #59874
    Profile photo of Mark Prior
    Mark Prior
    Participant

    In A previous post with some help i managed to come up with an if statement that would search through a table and match a machines OU and then write that to another csv file, hunky dory so far

    
    $tbl = import-csv  "\\global\europe\newcastle\Transfer\Test\offices.csv" -Delimiter ","
    	
    	
    	$ComputerName = $env:COMPUTERNAME
    	
    	
    	$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
    	$searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))"
    	[System.DirectoryServices.SearchResult]$result = $searcher.FindOne()
    	if (!$?)
    	{
    		return
    	}
    	$dn = $result.Properties["distinguishedName"]
    	$ouResult = $dn.Substring($ComputerName.Length + 4)
    	if ($ValueOnly)
    	{
    		$ouResult
    	}
    	
    	$Serial = gwmi win32_bios | select Serialnumber -ExpandProperty Serialnumber
    	
    	
    	
    	foreach ($row in $tbl)
    	{
    		
    		if ($ouResult -like $row.pattern)
    		{
    			
    			$serial | Export-Csv $row.destination -NoTypeInformation -append
    				break					
    		}
    		
    		
    	}
    
    

    Now my issue is what if something falls outside of the $tbl variable? i would like to append it once to another file

    
    else ($ouResult -notlike $row.pattern)
    		{
    			
    			$serial | Export-Csv c:\tmp\misc.csv -NoTypeInformation -append
    				break					
    		}
    
    

    however this will match & write however many items that is in the csv that does not meet the criteria and i end up with the data x amount of times written to the same file. (also happens if it does find a match in the first part of the foreach for any rows it does not match)

    example of csv

    Pattern, Destination
    *OU=\#London Campus*, \\global\europe\Transfer\Newcastle\Test\London Campus.csv
    *OU=Adelaide*, \\global\europe\Transfer\Newcastle\Test\Adelaide.csv
    *OU=Amsterdam*, \\global\europe\Transfer\Newcastle\Test\Amsterdam.csv
    *OU=Auckland*, \\global\europe\Transfer\Newcastle\Test\Auckland.csv

    appreciate any help guys

    Thanks

  • #59880
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Adding some quick logic, as above maybe a bit confusing

    
    	foreach ($row in $tbl)
    	{
    		
    		if ($ouResult -like $row.pattern)
    		{
    			
    			$serial | Export-Csv $row.destination -NoTypeInformation -append
    				break					
    		}
    

    Append a file with only this result

    Else

    			
    		else ($ouResult -notlike $row.pattern)
    		{
    			
    			$serial | Export-Csv c:\tmp\test.csv -NoTypeInformation -append
    				break					
    		}
    
    

    Append named file only containing 1 result

  • #59884
    Profile photo of Mark Prior
    Mark Prior
    Participant

    got it

    
    foreach ($row in $tbl)
    	{
    		
    		if ($ouResult -like $row.pattern)
    		{
    			
    			$serial | Export-Csv $row.destination -NoTypeInformation -append
    			$match = "Yes"
    				break					
    		}
    		
    		
    	}
    	
    	if($match -notlike "yes")
    	{
    		
    		$serial | Export-Csv "\\global\europe\Transfer\Newcastle\Test\misc.csv" -NoTypeInformation -append
    		break
    	}
    
  • #59905
    Profile photo of Ron
    Ron
    Participant

    Even though it may not be necessary in one particular instance like this, its always good practice to initialize variables like $match. Just add a $match = "no" before the foreach loop. Sooner or later, an uninitialized variable will bite you. I'd also go with a boolean variable to make it cleaner.

    $match = $false

    $match = $true

    if ($match) {...}

  • #59908
    Profile photo of Mark Prior
    Mark Prior
    Participant

    thanks Ron, will take suggestion on board

You must be logged in to reply to this topic.