none
ignore null value from import-CSV RRS feed

  • Question

  • While running a script to add DNS resource records, I import CSV containing some $Null values.  The records are added successfully but I get warnings for null values. How can I update the script to not return warnings for null values?

    CSV format:

    Zonename Recordname Ipv4Address HostnameAlias Mailexchange Preference  
    abcd.net test5 10.10.10.10        
    abcd.net www   raghav.local6      
    abcd.net mail5     mail.abcd.net 10  

    Script:

    Function Add-Resourcerecords
    {
        [CmdletBinding()]
        param(
             [parameter(Mandatory=$True,HelpMessage="Filepath of text file containing server names")]
             [ValidatePattern("([a-zA-Z]:(\\w+)*\\[a-zA-Z0_9]+)?.csv")]
             [string]$CSVFilepath

        )

        $Errors = @()

        Import-Csv $CSVFilepath | %{

            If (($_.Zonename -ne $null) -and ($_.recordname -ne $null) -and ($_.ipv4address -ne $null)) {
                #Adds A record
                Try   {Add-DnsServerResourceRecord -ZoneName $_.Zonename -Name $_.recordname -A -IPv4Address $_.ipv4address -EA Stop 
                       Write-Verbose "$($_.recordname) with $($_.ipv4address) added successfully on $($_.Zonename)" -Verbose    
                }#Try
                Catch {$date = Get-Date
                       $Errors+= "$date - $_"
                       Write-Warning "$_"
                       #Write-EventLog -logname Application -Source $using:eventSource -EventId 9899 -EntryType Error -Message "$_" -Category 0 -RawData 10,20
                }#catch
            }#If

            If (($_.Zonename -ne $null) -and ($_.recordname -ne $null) -and ($_.HostNameAlias -ne $null)) {

                #Adds CNAME
                Try   {Add-DnsServerResourceRecord -ZoneName $_.Zonename -Name $_.recordname -CName -HostNameAlias $_.HostNameAlias -EA Stop -warningaction silentlycontinue
                       Write-Verbose "$($_.recordname) alias to $($_.HostNameAlias) added successfully on $($_.Zonename)" -Verbose
                }#Try
                Catch {$date = Get-Date
                       $Errors+= "$date - $_"
                       Write-Warning "$_"
                       #Write-EventLog -logname Application -Source $using:eventSource -EventId 9899 -EntryType Error -Message "$_" -Category 0 -RawData 10,20
                }#catch
            }#If

            If (($_.Zonename -ne $null) -and ($_.recordname -ne $null) -and ($_.MailExchange -ne $null) -and ($_.Preference -ne $null)) {
                #Adds MX record
                Try   {Add-DnsServerResourceRecord -ZoneName $_.Zonename -Name $_.recordname -MX -MailExchange $_.MailExchange -Preference $_.Preference -EA Stop -warningaction silentlycontinue
                       Write-Verbose "$($_.recordname) added successfully on $($_.Zonename) pointinng to $($_.MailExchange)" -Verbose
                }#Try
                Catch {$date = Get-Date
                       $Errors+= "$date - $_"
                       Write-Warning "$_"
                       #Write-EventLog -logname Application -Source $using:eventSource -EventId 9899 -EntryType Error -Message "$_" -Category 0 -RawData 10,20
                }#catch
            }#If

        }#%

    }Add-Resourcerecords

    Output:

    VERBOSE: test5 with 10.10.10.10 added successfully on abcd.net
    WARNING: Cannot validate argument on parameter 'HostNameAlias'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    WARNING: Cannot validate argument on parameter 'MailExchange'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    WARNING: Cannot process argument transformation on parameter 'IPv4Address'. Cannot convert value "" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
    VERBOSE: www alias to raghav.local6 added successfully on abcd.net
    WARNING: Cannot validate argument on parameter 'MailExchange'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    WARNING: Cannot process argument transformation on parameter 'IPv4Address'. Cannot convert value "" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
    WARNING: Cannot validate argument on parameter 'HostNameAlias'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    VERBOSE: mail5 added successfully on abcd.net pointinng to mail.abcd.net



    99upgrade



    • Edited by 99upgrade Sunday, April 5, 2015 6:01 PM edited csv format
    Sunday, April 5, 2015 5:56 PM

Answers

  • You cannot add to $errors s it is managed by system.

    Here is how to correctly check for empty values.  Also you should dump the whole error so you can see what is actually causing the error.

    If($_.Zonename.Trim() -and $_.recordname.Trim() -and $_.ipv4address.Trim()) {
        Try {
            Add-DnsServerResourceRecord -ZoneName $_.Zonename -Name $_.recordname -A -IPv4Address $_.ipv4address -EA Stop
            Write-Verbose "$($_.recordname) with $($_.ipv4address) added successfully on $($_.Zonename)" -Verbose
        }
        Catch {
            $_
        }
    }else{
    Write-Host 'One or more arguments are null or empty' -fore red
    }


    \_(ツ)_/


    • Edited by jrv Sunday, April 5, 2015 8:31 PM
    • Marked as answer by 99upgrade Monday, April 6, 2015 6:51 AM
    Sunday, April 5, 2015 6:22 PM