如何从具有两列的CSV中存储两个变量?

时间:2014-06-25 22:04:29

标签: powershell csv

每个CSV行中的每个PC和用户都有一个连接,是从库存软件生成的。

CSV文件:

"pc1";"user1"
"pc2";"user2"
"pc3";"user3"

新手代码:

$csv = IMPORT-CSV ~\desktop\report.csv

$computersCsv = get-magicallyColumn1
$usersCsv = get-magicallyColumn2

$i = 0

Foreach ($computersCsv in $usersCsv) {

    W7path = \\computersCsv[$i]\c$\users\usersCsv[$i]\desktop

        if(Test-Path W7){

            "not matters - the size is $size"

            if($size -gt "some bytes"){

            Add-Content ~\desktop\NewReport.csv "THAT MATTERS ComputersCsv[$i] userCsv[$i] $size"
            }
        }

        else{
            "Error! - bad connection"
        }

    $i++
}

请求帮助/想法。

1 个答案:

答案 0 :(得分:3)

您不需要单独收集csv行,PowerShell可以很好地管理它。不太确定你想要实现的目标,但这里有一些帮助:

$csv = import-csv test.csv -Delimiter ";" -Header PCName, User
echo $csv #debug purposes

foreach($item in $csv)
{
    echo "this is the first pc name"
    echo $item.PCName
    $pc = $item.PCName
    echo "This is the first user name"
    echo $item.User
    $user = $item.User

    $W7path = \\$PCname\c$\users\$User\desktop

    if(Test-Path $W7path){

        "not matters - the size is $size"

        if($size -gt "some bytes"){

        Add-Content ~\desktop\NewReport.csv "THAT MATTERS $item.PCname $item.User $size"
        }
    }

    else{
       echo "Error! - bad connection"
    }

}

希望这有帮助