使用Powershell操作CSV

时间:2016-12-29 17:07:55

标签: powershell csv

目前我正在编写一个脚本来自动化IE中的进程以添加计算机名称及其MAC,以便我们对其进行成像。该页面有两个字段,一个用于MAC,一个用于计算机名称,然后是添加新按钮。我不得不提出一个非常草率的解决方案,通过在提交后退出com对象来避免页面弹出。

我对Powershell没有多少经验,也没有使用过CSV的经验,所以我在使用这项工作时遇到了一些麻烦。我的目标是让脚本从一行读取两个条目,填写正确的字段,然后提交,然后移动到下一行并重复。

现在它的作用是填写两个字段中未定义的字段,然后提交并重复。

编辑:我已经稍微编辑了我的代码,所以它确认了什么是试图阅读。This is what the results look like.我相信@WalterMitty正在解决$ ie.document.getElementsByName行出错的问题,我刚试过$ ie.document.getElementById但没有填写任何字段。看起来读取CSV没有问题,但输入正确读入字段的信息确实存在问题。

这是CSV的样子。

 NewComputerName,NewMACAddress
 ComputerName1,111122223333
 ComputerName2,112233446677
 ComputerName3,AAAABBBBCCCC
 ComputerName4,AABBCCDDEEFF

这就是我的代码目前的样子。

cls

$URL = ""
$iterator = 1;
$csv = Get-Content C:\example1.csv

foreach($row in $csv)
{


#starts IE
$ie = new-object -ComObject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($URL)

while($ie.Busy -eq $true) { start-sleep -Milliseconds 100 }

($ie.document.getElementsByName("mc_id") |select -first 1).value = $_.NewComputerName;
($ie.document.getElementsByName("mac_id") |select -first 1).value = $_.NewMACAddress;
$ie.document.forms | Select -First 1| % { $_.submit() };

$ie.quit()
$iterator++

write-host "$iterator new ID(s) added"
write-host $row.NewComputerName - $row.NewMACAddress
}

3 个答案:

答案 0 :(得分:1)

$URL = ""
$iterator = 1

# use Import-Csv for CSV files
$csv = Import-Csv "C:\example1.csv" -Delimiter ","

foreach($row in $csv) {
    Write-Host "$iterator new ID(s) added"

    #starts IE
    $ie = New-Object -ComObject "InternetExplorer.Application"
    $ie.Visible = $true
    $ie.Navigate($URL)

    while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 100 }

    # $_ is not defined in foreach blocks, you have to use $row here
    ($ie.Document.getElementsByName("mc_id") | Select-Object -First 1).Value = $row.NewComputerName
    ($ie.Document.getElementsByName("mac_id") | Select-Object -First 1).Value = $row.NewMACAddress

    $ie.Document.Forms | Select-Object -First 1 | ForEach-Object { $_.submit() }

    $ie.Quit()
    $iterator++
}

答案 1 :(得分:1)

I'm having similar issues but I just found this may help you guys - I don't have 50 reputation to comment sorry :/....

I messed around with the -Path of the Import-CSV command but I just couldn’t make it work. Apparently this has nothing to do with the path of the CSV file. The Warlock posted this on his blog:

Long story short, the error came from having trailing blank columns in my CSV. Import-Csv uses the first row in the CSV as names for the columns (unless you specify otherwise) and when you have blank columns (or at least multiple blank columns) it causes this error as it doesn’t have a valid name for them.

Instead of changing the file, I changed my import command to include the headers as per Dale's comment and it worked perfectly:

$data = import-csv "C:\Sharepoint.csv" -header("Department","AD Group","Members","Notes")

The Warlock and Dale saved me lots of time, please stop by the Warlock’s blog并给予他们非常感谢

答案 2 :(得分:1)

考虑组合使用Import-Csv和Invoke-WebRequest,例如像这样:

import-csv .\example.csv | %{ iwr http://someurl.local -body @{mc_id=$_.NewComputerName; mac_id=$_.NewMACAddress} -Method POST }

它将读取csv文件,迭代记录并使用每条记录中的值创建application / x-www-form-urlencoded POST请求。

当您使用iwr(Invoke-WebRequest)并将哈希表作为“正文”传递时,它将表现为它正在提交的表单。 POST方法将表单值提交为application / x-www-form-urlencode。如果没有POST方法,它会将表单提交为GET,即传递url中的值。

如果您需要身份验证,会话支持等,请阅读Invoke-WebRequest文档。

使用IE来自动化Web请求很脆弱且容易出错。