PowerShell:使用 if 条件导入 CSV 数据

huangapple go评论66阅读模式
英文:

Powershell : import-csv with if condition

问题

我已经有一个如下的CSV文件:

  • 服务器 操作系统 补丁ID
  • Comp1 Win12 KB452
  • Comp1 Win12 KB453
  • svrname3 Win8 KB134

我编写了下面的脚本来检查操作系统,如果匹配,然后应该检查服务器是否具有相同的补丁ID。

$file = Import-Csv .\Desktop\hotfix.csv
if ($Win12 = $file | where {$_.os -eq "Win12"} | select Source, HotFixID)
{
Get-HotFix -ComputerName ($Win12.Source) -Id ($Win12.HotFixID)
}
else
{
$Win8 = $file | where {$_.os -eq "Win8"} | select Source, HotFixID
Get-HotFix -ComputerName ($Win8.Source) -Id ($Win8.HotFixID)
}

问题在于输出... 我的CSV中有2台Win12服务器,但我得到了4个输出,其中2个是重复的... 我能理解这里有一个嵌套循环在运行,但无法纠正它。请告诉我如何解决这个问题。

英文:

I have a csv as below :

  • Server OS HotFixID
  • Comp1 Win12 KB452
  • Comp1 Win12 KB453
  • svrname3 Win8 KB134

I have written below script for checking OS and if its matched then it should check if the server is having the same HotfixID or not.

$file = Import-Csv .\Desktop\hotfix.csv
if($Win12 = $file | where {$_.os -eq "Win12"} | select Source, HotFixID)
{
Get-HotFix -ComputerName ($Win12.Source) -Id ($Win12.HotFixID)
}
else
{
$Win8 = $file | where {$_.os -eq "Win8"} | select Source, HotFixID
Get-HotFix -ComputerName ($Win8.Source) -Id ($Win8.HotFixID)
}

> problem is with output.. I have 2 Win12 server in csv, but I am getting 4 output 2 as duplicate.. I am able to understand that here one nested loop is running but unable to rectify it. Please!! let me know how can we fix this issue.

答案1

得分: 1

Assuming I understand your task correctly, you should iterate over all elements in your CSV file and check each item individually.

$CSVList = Import-Csv .\Desktop\hotfix.csv
foreach ($CSVItem in $CSVList) {
    if ($CSVItem.os -eq 'Win12' -or $CSVItem.os -eq 'Win8') {
        if (Test-Connection -ComputerName $CSVItem.Source) {
            Get-HotFix -ComputerName $CSVItem.Source -Id $CSVItem.HotFixID
        }
    }
}

(Note: This is the provided code without translation.)

英文:

Assumed I understood your task correct you should iterate over all elements in your CSV file and check each item indivually.

$CSVList = Import-Csv .\Desktop\hotfix.csv
foreach ($CSVItem in $CSVList) {
    if ($CSVItem.os -eq 'Win12' -or $CSVItem.os -eq 'Win8') {
        if (Test-Connection -ComputerName $CSVItem.Source) {
            Get-HotFix -ComputerName $CSVItem.Source -Id $CSVItem.HotFixID
        }
    }
}

答案2

得分: 1

你在Win12下有多个条目,所以$Win12是一个PSCustomObject数组。当你使用($Win12.Source)时,它将输出所有来源的数组(Comp1,Comp1)。Get-Hotfix接受这个数组,并与每个来源测试$Win12.HotFixID(这也是一个数组)。
这就是你在CSV中看到每个项目出现次数的原因。为了避免这种情况,单独处理CSV中的每一行。如果你想保持你的结构,你的代码会是这样的:

if($Win12 = $file | where {$_.os -eq "Win12"} | select Source, HotFixID)
{
    $Win12 | foreach {
        Get-HotFix -ComputerName ($_.Source) -Id ($_.HotFixID)
    }
}
else
{
    $Win8 = $file | where {$_.os -eq "Win8"} | select Source, HotFixID
    $Win8 | foreach {
        Get-HotFix -ComputerName ($_.Source) -Id ($_.HotFixID)
    }
}

然而,Olaf的代码更简洁,但实现了相同的功能。

英文:

You have multiple entries with Win12 so $Win12 is an array of PSCustomObject. When you use ($Win12.Source) it will output an array of all sources. (Comp1,Comp1). Get-Hotfix accepts this array and tests $Win12.HotFixID (which is an array, too) with each of the sources. Thats the reason you get each Item the Number of times they apear in the CSV. To avoid this, process each line in the CSV on their own. If you want to keep your structure, your code would look like this:

if($Win12 = $file | where {$_.os -eq "Win12"} | select Source, HotFixID)
{
    $Win12 | foreach {
        Get-HotFix -ComputerName ($_.Source) -Id ($_.HotFixID)
    }
}
else
{
    $Win8 = $file | where {$_.os -eq "Win8"} | select Source, HotFixID
    $Win8 | foreach {
        Get-HotFix -ComputerName ($_.Source) -Id ($_.HotFixID)
    }

}

However the Code of Olaf is cleaner and does the same.

huangapple
  • 本文由 发表于 2020年1月3日 21:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定