使用Powershell

时间:2019-05-28 11:38:12

标签: powershell

第一个导出的.csv文件(Report1第一天)

id                                      principalName       licenseDisplayName    

4b3556f1-df58-6c3c-848b-022fc6a8668d    abc@my.com         Basic   
5b3556f1-df58-6c3c-848b-022fc6a8668d    xyz@my.com         Stackholder   
6b3556f1-ff58-6c3c-848b-022fc6a8668d    pqr@my.com         Admin   

另一天导出.csv文件(Report2第二天)     idPrincipalName licenseDisplayName

4b3556f1-df58-6c3c-848b-022fc6a8668d    abc@my.com         Stackholder   
5b3556f1-df58-6c3c-848b-022fc6a8668d    xyz@my.com         Stackholder   
8b3556f1-ff58-6c3c-848b-022fc6a8668d    yz@my.com          Basic   

方案中,有使用PowerShell的每日导出报告。在上面的报告1和报告2中显示了记录。所以,我想要两个.csv文件之间的区别。

在Report2中,第一条记录从Basic更改为Stackholder,而第三条记录丢失了6b3556f1-ff58-6c3c-848b-022fc6a8668d,并添加了新记录8b3556f1-ff58-6c3c-848b-022fc6a8668d

我想要以下输出(两个.csv文件的区别)

id                                     principalName       licenseDisplayName       Status    
4b3556f1-df58-6c3c-848b-022fc6a8668d    abc@my.com         Stackholder     Updated from Basic to Stackholder
6b3556f1-ff58-6c3c-848b-022fc6a8668d    pqr@my.com         Admin             Deleted
8b3556f1-ff58-6c3c-848b-022fc6a8668d    yz@my.com          Basic               New

我的代码:

$reference = Import-Csv -Path C:\Documents\Daily_Report\Daily_Export_jsonold.csv
$lookup = $reference | Group-Object -AsHashTable -AsString -Property Server
$results = Import-Csv -Path C:\Documents\Daily_Report\Daily_Export_jsonnew.csv | foreach {
    $server = $_.id
    Write-Verbose "Looking for $server"
    if ($lookup.value.ContainsKey($server))
    {
        $oldState = ($lookup[$server]).Access_Level
    }

    if ($_.State -ne $oldState)
    {
        [PSCustomObject]@{
            id = $server
            Username = $_.Username
            OldState = $oldState
            NewState = $_.Access_Level
        }
    }
}

但是我得到以下输出

id                                   Username                  OldState NewState   
--                                   --------                  -------- --------   
4b3556f1-df58-6c3c-848b-022fc6a8668d abc@my.com                Unknown  Stakeholder
5b3556f1-df58-6c3c-848b-022fc6a8668d xyz@my.com                Unknown  Stakeholder 

我希望它像下面这样:

id                                   Username             OldState     NewState   
--                                   --------             --------     --------   
4b3556f1-df58-6c3c-848b-022fc6a8668d abc@my.com           Basic        Stakeholder
5b3556f1-df58-6c3c-848b-022fc6a8668d xyz@my.com           Stakeholder  Stakeholder 

1 个答案:

答案 0 :(得分:0)

我建议让cmdlet Compare-Object完成工作的核心。

$Reference = Import-Csv '.\Daily_Export_jsonold.csv'
$Difference= Import-Csv '.\Daily_Export_jsonnew.csv'

Compare-Object -Ref $Reference -Diff $Difference `
               -Property id,principalName,licenseDisplayName

示例输出:

id                                   principalName licenseDisplayName SideIndicator
--                                   ------------- ------------------ -------------
4b3556f1-df58-6c3c-848b-022fc6a8668d abc@my.com    Stackholder        =>
8b3556f1-ff58-6c3c-848b-022fc6a8668d yz@my.com     Basic              =>
4b3556f1-df58-6c3c-848b-022fc6a8668d abc@my.com    Basic              <=
6b3556f1-ff58-6c3c-848b-022fc6a8668d pqr@my.com    Admin              <=

这与您想要的结果非常接近,按属性id进行分组可以得出:

Count Name                      Group
----- ----                      -----
    2 4b3556f1-df58-6c3c-848... {@{id=4b3556f1-df58-6c3c-848b-022fc6a8668d; principalName=abc@my.com; licenseDisplayName=Stackho...
    1 8b3556f1-ff58-6c3c-848... {@{id=8b3556f1-ff58-6c3c-848b-022fc6a8668d; principalName=yz@my.com; licenseDisplayName=Basic; S...
    1 6b3556f1-ff58-6c3c-848... {@{id=6b3556f1-ff58-6c3c-848b-022fc6a8668d; principalName=pqr@my.com; licenseDisplayName=Admin; ...

这里:

  • 多一个数字表示变化
  • 计数为1且SideIndicator '<='表示“已删除”
  • 计数为1且SideIndicator '=>'表示“新建”

所以这个脚本:

## Q:\Test\2019\05\28\SO_56341395.ps1
$Reference = Import-Csv '.\Daily_Export_jsonOld.csv'
$Difference= Import-Csv '.\Daily_Export_jsonNew.csv'

$Result = Compare-Object -Ref $Reference -Diff $Difference `
               -Property id,principalName,licenseDisplayName |
    Group-Object id | ForEach-Object {
        if ($_.Count -gt 1){
            $Status = 'Updated from {0}' -f $_.Group[1].licenseDisplayName
        } elseif ($_.Group[0].SideIndicator -eq '<='){
            $Status = 'Deleted'
        } else {
            $Status = 'New'
        }
        [PSCustomObject]@{
            id                 = $_.Group[0].id
            principalName      = $_.Group[0].principalName
            licenseDisplayName = $_.Group[0].licenseDisplayName
            Status             = $Status
        }
    }

$Result | Sort-Object id
$Result | Sort-Object id| Export-Csv '.\Daily_Export_jsonDif.csv' -NoTypeInformation

提供此输出:

> Q:\Test\2019\05\28\SO_56341395.ps1

id                                   principalName licenseDisplayName Status
--                                   ------------- ------------------ ------
4b3556f1-df58-6c3c-848b-022fc6a8668d abc@my.com    Stackholder        Updated from Basic
6b3556f1-ff58-6c3c-848b-022fc6a8668d pqr@my.com    Admin              Deleted
8b3556f1-ff58-6c3c-848b-022fc6a8668d yz@my.com     Basic              New