【问题标题】:How to export two variables into same CSV as joined via PowerShell?如何将两个变量导出到通过 PowerShell 连接的相同 CSV?
【发布时间】:2019-10-10 20:11:07
【问题描述】:

我有一个使用 poshwsus 模块的 PowerShell 脚本,如下所示:

$FileOutput = "C:\WSUSReport\WSUSReport.csv"
$ProcessLog = "C:\WSUSReport\QueryLog2.txt"
$WSUSServers = "C:\WSUSReport\Computers.txt"
$WSUSPort = "8530"

import-module poshwsus 

ForEach ($Server in Get-Content $WSUSServers)
{
    & connect-poshwsusserver $Server -port $WSUSPort | out-file $ProcessLog -append
    $r1 = & Get-PoshWSUSClient | select @{name="Computer";expression={$_.FullDomainName}},@{name="LastUpdated";expression={if ([datetime]$_.LastReportedStatusTime -gt [datetime]"1/1/0001 12:00:00 AM") {$_.LastReportedStatusTime} else {$_.LastSyncTime}}}
    $r2 = & Get-PoshWSUSUpdateSummaryPerClient -UpdateScope (new-poshwsusupdatescope) -ComputerScope (new-poshwsuscomputerscope) | Select Computer,NeededCount,DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount

}

我需要做的是导出 CSV 输出,包括带有列的结果(如“内部连接”):

Computer, NeededCount, DownloadedCount, NotApplicableCount, NotINstalledCount, InstalledCount, FailedCount, LastUpdated

我尝试在 foreach 中使用下面的行,但它没有按我预期的那样工作。

$r1 + $r2 | export-csv -NoTypeInformation -append $FileOutput

如果您能提供帮助或建议,我将不胜感激。

EDIT --> 我得到的输出:

ComputerName LastUpdate
X                A
Y                B
X
Y

所以没有错误,前两行来自 $r2,最后两行来自 $r1,它没有像我预期的那样加入表格。

谢谢!

【问题讨论】:

  • 您是否收到错误消息或意外输出?您是否能够共享该输出(如果需要,删除敏感信息)?
  • 意外输出。让我编辑问题。
  • 如果您不想重新发明轮子并遇到与在 PowerShell 中加入对象相关的每个陷阱(例如:Not all properties displayed)和(性能)问题,您可能需要查看此@987654322 @cmdlet。我猜你的语法很简单:$r1 | Join $r2 -On Computer

标签: powershell powershell-5.0 poshwsus


【解决方案1】:

我在这篇文章中找到了我的指导:Inner Join in PowerShell (without SQL)

像下面这样相应地修改了我的查询,就像一个魅力。

$FileOutput = "C:\WSUSReport\WSUSReport.csv"
$ProcessLog = "C:\WSUSReport\QueryLog.txt"
$WSUSServers = "C:\WSUSReport\Computers.txt"
$WSUSPort = "8530"

import-module poshwsus 

function Join-Records($tab1, $tab2){
    $prop1 = $tab1 | select -First 1 | % {$_.PSObject.Properties.Name} #properties from t1
    $prop2 = $tab2 | select -First 1 | % {$_.PSObject.Properties.Name} #properties from t2
    $join = $prop1 | ? {$prop2 -Contains $_}
    $unique1 = $prop1 | ?{ $join -notcontains $_}
    $unique2 = $prop2 | ?{ $join -notcontains $_}


    if ($join) {
        $tab1 | % {
            $t1 = $_
            $tab2 | % {
                $t2 = $_
                foreach ($prop in $join) {
                    if (!$t1.$prop.Equals($t2.$prop)) { return; }
                }
                $result = @{}                
                $join | % { $result.Add($_,$t1.$_) }
                $unique1 | % { $result.Add($_,$t1.$_) }
                $unique2 | % { $result.Add($_,$t2.$_) }
                [PSCustomObject]$result
            }
        }
    }
}

ForEach ($Server in Get-Content $WSUSServers)
{
    & connect-poshwsusserver $Server -port $WSUSPort | out-file $ProcessLog -append
    $r1 = & Get-PoshWSUSClient | select @{name="Computer";expression={$_.FullDomainName}},@{name="LastUpdated";expression={if ([datetime]$_.LastReportedStatusTime -gt [datetime]"1/1/0001 12:00:00 AM") {$_.LastReportedStatusTime} else {$_.LastSyncTime}}}
    $r2 = & Get-PoshWSUSUpdateSummaryPerClient -UpdateScope (new-poshwsusupdatescope) -ComputerScope (new-poshwsuscomputerscope) | Select Computer,NeededCount,DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount
    Join-Records $r1 $r2 | Select Computer,NeededCount,DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount, LastUpdated | export-csv -NoTypeInformation -append $FileOutput

}

【讨论】:

    【解决方案2】:

    我认为这可以变得更简单。由于Select-Object-Property 参数接受值数组,因此您可以创建要显示的属性数组。可以通过比较两个对象的属性并输出这些属性的唯一列表来构造数组。

    $selectProperties = $r1.psobject.properties.name | Compare-Object $r2.psobject.properties.name -IncludeEqual -PassThru
    $r1,$r2 | Select-Object -Property $selectProperties
    

    Compare-Object 默认只输出参考对象和差异对象之间的差异。添加-IncludeEqual 开关会显示不同且相等的比较。添加-PassThru 参数会输出比较的实际对象,而不是默认的 PSCustomObject 输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2023-01-20
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多