Search code examples
powershellscriptingpowershell-2.0powershell-3.0

Powershell script to add CSv


How to fetch data from CSv to PowerShell?


Solution

  • So, based on this...

    No, that's the same CSv, actually, Test.csv hold the information IP, hostname, and zone. I want the script to look into hostname, IP, and Zone column and add line by line in my firewall.

    Your actions should be inside that loop, because as it stands now, that loop is not doing anything for your cause other than listing what is in the CSV file.

    With that layout you show, that is space delimited file, not the default comma separate CSV. So, you have to specify that space as a delimiter or you won't get what you'd expect, column-wise.

    # Assign and display the dataset from the data source
    ($Csv = Import-Csv -Path 'D:\Scripts\test.csv' -Delimiter ' ')
    
    # Results
    
    NEW_HOSTNAME NEW_IP_ADDRESS ZONE
    ------------ -------------- ----
    WEBSERVER_01 1.1.1.1        DMZ 
    SQLSERVER_01 5.5.5.5        SQL
    
    ForEach($lol in $Csv)
    {
        # Your do something code goes here for each item to be processed
        $lol.NEW_HOSTNAME
    }