用于添加 CSv 的 Powershell 脚本

Powershell script to add CSv

如何从 CSv 获取数据到 PowerShell?

所以,基于此...

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.

您的操作应该在该循环内,因为就目前而言,除了列出 CSV 文件中的内容外,该循环不会为您做任何事情。

使用您显示的布局,即 space 分隔文件,而不是默认的逗号分隔 CSV。因此,您必须将 space 指定为分隔符,否则您将无法按列获得预期的结果。

# 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
}