Powershell中的VLOOKUP脚本

时间:2017-10-30 08:16:52

标签: excel powershell

我正在编写一个脚本,它将从具有特定操作系统的AD中获取计算机列表,然后将此工作表的“名称”列与另一个具有向AV代理报告的计算机列表的工作簿进行比较。有点像使用VLOOKUP函数检查不匹配,然后基于相同的数据构建数据透视表和图表。然而;我有点坚持“名称”的比较,因为它给出了一个带有指标的空白表。我想要比较表,没有指标。可以在Powershell中实现吗?对此有任何帮助真的很棒。守则如下: -

import-module ac*

Get-ADComputer -filter { OperatingSystemVersion -Like '*6.1*' -and Enabled -eq "true"} -SearchBase 'OU=Computers,OU=IM,dc=miraje,dc=intr' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\ExportPC3.csv" -Encoding UTF8

$file1 = import-csv -Path "C:\Temp\ExportPC3.csv"
$file2 = import-csv -Path "C:\Temp\AV_Machines.csv"


$result = Compare-Object $file1 $file2 -property Name -IncludeEqual | Export-Csv -NoType "C:\Temp\ExportPC2.csv" -Encoding UTF8

2 个答案:

答案 0 :(得分:0)

$AV_Machines = ConvertFrom-CSV @"
Host Name,OS,Version,Device,Type
AMSSVIATRS31,Win,Windows Server 2008 R2,Server
AMSSVIFLS32,Win,Windows Server 2008 R2,Server
AMSSVIPRTFLS31,Win,Windows Server 2008 R2,Server
ANTSRVATRS31,Win,Windows Server 2008 R2,Server
"@

$PC3 = ConvertFrom-CSV @"
PC Name,PC status,OS
BFK0852,Enabled,6.1 (7601)
BLVDLMBHYV2,Enabled,5.1 (2600)
BLVPACKAGING,Enabled,6.1 (7601)
BLVSAMSUNG2,Enabled,6.1 (7601)
BRG314F68G,Enabled,6.1 (7601)
"@

使用Compare-Object

PS C:\> Compare-Object $PC3 $AV_Machines -Property "Host Name", "PC Name", "OS"
, "Version", "Device", "Type", "PC status" | FT

Host Name      PC Name      OS         Version                Device Type PC status SideIndicator
---------      -------      --         -------                ------ ---- --------- -------------
AMSSVIATRS31                Win        Windows Server 2008 R2 Server                =>
AMSSVIFLS32                 Win        Windows Server 2008 R2 Server                =>
AMSSVIPRTFLS31              Win        Windows Server 2008 R2 Server                =>
ANTSRVATRS31                Win        Windows Server 2008 R2 Server                =>
               BFK0852      6.1 (7601)                                    Enabled   <=
               BLVDLMBHYV2  5.1 (2600)                                    Enabled   <=
               BLVPACKAGING 6.1 (7601)                                    Enabled   <=
               BLVSAMSUNG2  6.1 (7601)                                    Enabled   <=
               BRG314F68G   6.1 (7601)                                    Enabled   <=

请注意,我不确定这是否符合您的需要,因为这还包括"Host Name" -eq "PC Name"的记录,但给定的示例没有任何通用名称,但仍不清楚您对输出的期望是什么

使用Join-Object

PS C:\> $PC3 | FullJoin $AV_Machines {$Left."Host Name" -ne $Right."PC Name"} |
 ft

Version                Type PC Name      OS         Device Host Name      PC status
-------                ---- -------      --         ------ ---------      ---------
                            BFK0852      6.1 (7601)                       Enabled
                            BLVDLMBHYV2  5.1 (2600)                       Enabled
                            BLVPACKAGING 6.1 (7601)                       Enabled
                            BLVSAMSUNG2  6.1 (7601)                       Enabled
                            BRG314F68G   6.1 (7601)                       Enabled
Windows Server 2008 R2                   Win        Server AMSSVIATRS31
Windows Server 2008 R2                   Win        Server AMSSVIFLS32
Windows Server 2008 R2                   Win        Server AMSSVIPRTFLS31
Windows Server 2008 R2                   Win        Server ANTSRVATRS31

答案 1 :(得分:0)

我有一个与此类似的问题,无法在google上找到任何东西,因此这是我的解决方案,以防它帮助其他人搜索同一事物:

$(this).val()

$ VPNRepSum只是我拉入系统阵列的报告(显然,这就是您要查找的内容),FYI的类型如下:

$Addresses = Import-CSV -Path "C:\myfiles\mylookupdata.csv" 
#csv contains SAMAccountName,emailLower,


foreach ($entry in $VPNRepSum) {
        if (($Addresses.SAMAccountNameLower).contains($entry.username)) {
            write-output "Match found..."
            $tempemail = $Addresses | Where-Object {$_.SAMAccountName -eq $entry.username } | select-object -Expand emailLower
            $unique = $tempemail | get-unique
            Add-Member -InputObject $entry -MemberType NoteProperty -Name Email -Value $unique -Force
            } else {
            write-output "No match found."
            }
        }

此外,请记住Get-Unique区分大小写,而且我不知道有任何更改方法。我通过确保所有内容都是小写来解决了这个问题。