how to process columns from a csv-file

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Richard Diphoorn Richard Diphoorn 6 months, 4 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #36056
    Profile photo of Roman Constantin
    Roman Constantin
    Participant

    Hello,

    i am a beginner in programming and specially powershell. Therefore I hope to learn a lot and hopefully to get help from you experts, too. I have a csv lists with 2 columns (one with some server-dns records, and one with ip addresses that the servers should have). In our company often hapens that the dns names will be deleted or overwritten in. For this reason I would like to creat a powershell script to check that (every day via task scheduler).
    I would like to import the csv-file and select the first columns (called Hosts which contains the dns names of the servers) and check if the dns records are still available. At the moment I don´t know how to select the forst column only and send it to foreach command. Here is my script:

    $servers = get-content "C:\temp\test1.csv"

    foreach ($Server in $Servers)
    {
    $Addresses = $null
    try {
    $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch {
    $Addresses = "Hostname cannot be resolved."
    }
    foreach($Address in $addresses) {
    write-host $Server, $Address
    }
    }

    Any Ideas?
    P.S: the second column with IP Addresses I will need as next step to ping them to find out if my machine is on or off.

    Thank you so much

    #36058
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Use Import-CSV.

    ForEach ($Server in (Import-CSV test1.csv)) {
    $Server.Hosts
    }

    $Server will represent the current row, and $Server.Hosts will represent a column titled "Hosts" in the CSV.

    FWIW, "Learn PowerShell in a Month of Lunches" covers this, and is a great way to learn about all the other difficult stuff in PowerShell.

    #36064
    Profile photo of Roman Constantin
    Roman Constantin
    Participant

    Hi Don,

    thank you for your answer. I also will follow your recomendation.. I changed the script as bellow:

    $servers = Import-Csv "C:\temp\test1.csv" -Delimiter ','
    ForEach ($Server in $Servers){
    $Server.Hosts
    }
    {
    $Addresses = $null
    try {
    $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch {
    $Addresses = "Hostname cannot be resolved."
    }
    foreach($Address in $addresses) {
    write-host $Server, $Address
    }
    }

    and this came back_________________________________________

    $Addresses = $null
    try {
    $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch {
    $Addresses = "Hostname cannot be resolved."
    }
    foreach($Address in $addresses) {
    write-host $Server, $Address
    }

    Any idea why?

    Thanks a lot!

    #36065
    Profile photo of Don Jones
    Don Jones
    Keymaster

    OK, so, sorry – that's my fault. I wasn't trying to provide you with a complete solution, I was illustrating the logic you need to use.

    $servers = Import-Csv "C:\temp\test1.csv" -Delimiter ','
    ForEach ($Server in $Servers){
    $Server.Hosts
    }
    {
    $Addresses = $null
    try {
    $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch {
    $Addresses = "Hostname cannot be resolved."
    }
    foreach($Address in $addresses) {
    write-host $Server, $Address
    }
    }
    

    $server.hosts will contain your host name. As-is, this script is just going to output the host name. You need to do something else with it. I'm not entirely sure what you're trying to achieve, and I'm not going to be able to write a complete script for you, but for example:

    $hostname = $server.host
    

    Would get the host name into the $hostname variable.

    The ForEach Loop:

    ForEach ($Server in $Servers){
    $Server.Hosts
    }
    

    Is what's running through all the rows in the CSV. I'm guessing the majority of your script logic needs to fall within that ForEach loop. The reason you're getting the output you are is because:

    {
    $Addresses = $null
    try {
    $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch {
    $Addresses = "Hostname cannot be resolved."
    }
    foreach($Address in $addresses) {
    write-host $Server, $Address
    }
    }
    

    See how the outermost braces are just enclosing everything? PowerShell isn't executing any of that, it's just defining it as a script block.

    #36122
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Hi Roman, welcome to the forum!

    First, I'd highly recommend to follow Don's advice, to read the book "Learn PowerShell in a Month of Lunches". It was one of my main learning resources.

    Secondly, as you are beginning to learn PowerShell, try to stick first with native PowerShell Cmdlets. Don't try to solve things with 'talking' directory to .NET.

    What I'm interpreting from your script, is that you want to take in hostnames, and then try to see if they can be resolved in DNS.
    To start simple, I would just use this code to achieve this (you will need at least PowerShell v3 for this):

    Import-Csv hosts.csv | ForEach-Object -Process { 
    
        Try {
            Resolve-DnsName -Name $_.Hostname -ErrorAction Stop | Select-Object -Property Name,IPAddress
        }
    
        Catch {
            Write-Warning -Message $_
        }
    }
    

    But I highly would urge you to discover what kind of Cmdlets are available to you, before diving into .NET.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.