Powershell - 逐行导入-Csv

时间:2018-02-15 21:04:59

标签: powershell

我目前正在尝试使用导入CSV将数据导入网站,我导入的CSV如下:

Yes Test    Test    Test            test@test.com   test    test    test    test    test    Yes Yes Yes Yes Yes Yes Yes 1   
Yes test2   test2   test2           test2@test2.com test2   test2   test2   test2   test2   Yes Yes Yes Yes Yes Yes Yes 2

我面临的问题是,如果以第二列为例,它将同时采用“Test”和“Test2”并将其放入字段中,如下所示:

我正在努力寻找一种逐行的方式。

$Header = 'Active Contact', 'First Name', 'Last Name', 'Position', 'Office', 'Mobile', 'Email Address', 'Site Name', 'Town', 'County', 'Authorised to make changes to this list?', 'Change', 'Commercial', 'Planned / Emergency Maintenance Notification?', 'Major Incident Notification?', 'Priority 1 Notification?', 'View All Portal Tickets?', 'Excalation', 'Contact'

function Create-Contact { 
    $products = import-csv 'C:\Users\Test\Desktop\CACL.csv' -Header $Header | Select -Skip 8
    ForEach-Object {
        write-host "Working on Contact: $iterator "

        #fill out form fields
        ($ie.document.getElementById("106009280") |select -first 1).value = $products.'First Name'; #Forename
        ($ie.document.getElementById("106009237") |select -first 1).value = $products.'Last Name'; #Surname
        ($ie.document.getElementById("106009289") |select -first 1).value = $products.'Position'; #Job Title
        ($ie.document.getElementById("106009283") |select -first 1).value = $products.'Office'; #Telephone
        ($ie.document.getElementById("106009286") |select -first 1).value = $products.'Mobile'; #Mobile
        ($ie.document.getElementById("106009416") |select -first 1).value = $products.'Email Address'; #Email

        Start-Sleep -Seconds 5; # Remove For Prod

        ($ie.document.getElementById("updateBtn") |select-first 1).click #Submit the new contact

        Start-Sleep -Seconds 5; # Remove For Prod

        $iterator ++

    }
}
Create-Contact

1 个答案:

答案 0 :(得分:0)

$products是一个数组。当您访问数组中保存的对象的成员时,powershell将返回这些值的数组。所以

$products.'First Name'

返回2个字符串的数组(即“Test Test2”)

根据您的评论,在您的情况下,您可以通过索引访问$products数组的各个行:

$products[0].'First Name'

将返回“测试”。