Unable to create a array from inside a if statement and export to csv

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 8 months, 2 weeks ago.

  • Author
    Posts
  • #36945
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    I am having a little difficulity trying to do something.

    1) I am running a command that checks the DNS Conditional Forwarders, ANDexports the results to a CSV.
    2) create a variable and import the contents of the csv
    3) have a if statement that if the variable is $null to create/add data to the csv file so it is not null.

    Here is what I came up with:

                gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4" |
    					            foreach {
    					              foreach ($server in $_.masterservers) {
    						            new-object -type psobject -prop @{Name=$_.Name;MasterServer=$server}
    					              }
    					            }  | Export-Csv "C:\temp\DNSZones.csv"
    
                Start-Sleep -s 10
    								
    			$DNSZonesCSVContent = import-csv "c:\temp\DNSZones.csv"
    			
    			If ($DNSZonesCSVContent -like "$null") {
                                                        
                                                        $CSVContent = @() 
    
    													$row = New-Object System.Object
    													$row | Add-Member -MemberType NoteProperty -Name "Name" -Value "NA"
    													$row | Add-Member -MemberType NoteProperty -Name "MasterServer" -Value "NA" 
    
    													$CSVContent += $row
                                                        
                                                        $CSVContent | Export-CSV -Path "C:\temp\DNSZones.csv"
                                                        
                                                    }
    
    

    I can see that a empty csv is being created by the original command, and when I import it into the variable it doesn't show anything (as expected). I also know that if I run the portion of the script that creates the array and exports it into the existing CSV it works fine when run all by itself (not in the if statement). I have tried using -eq, -like, -notlike, etc.. against $null or * and still nothing happens.

    What am I doing wrong?

    Thanks,

    Ed

  • #36957
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    Try it without the quote marks around $null.

    What I think is happening is the "$null" actually represents an empty string and an empty string is not the same as a null object.

    $string = "$null"
    Write-Output ($string -eq $null)
    
  • #36958
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Just curious ... what do you see when you enter

    import-csv "c:\temp\DNSZones.csv"

    Because I'm not sure what you're expecting since a blank line will not import. And a line with just a delimiter will show a blank line for the object, but it is not null. It is a collection of empty strings. And -Like is always the wrong operator use with $null.

    If ($DNSZonesCSVContent.Name -eq "" -and $DNSZonesCSVContent.Server -eq "") 
    {
        $row = [PSCustomObject]@{Name = "NA"; MasterServer = "NA"}
    }

    Although this doesn't really seem like useful information to me. You're going to end up with a bunch of NA's at the bottom of the list.

You must be logged in to reply to this topic.