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

Welcome Forums General PowerShell Q&A 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

3 years, 2 months ago.

  • Author
  • #36945

    Topics: 27
    Replies: 51
    Points: 0
    Rank: Member

    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?



  • #36957

    Topics: 1
    Replies: 296
    Points: 103
    Helping Hand
    Rank: 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

    Topics: 6
    Replies: 236
    Points: 0
    Rank: Member

    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.

The topic ‘Unable to create a array from inside a if statement and export to csv’ is closed to new replies.