比较powershell中的2个CSV文件,将差异导出到另一个文件。

比较powershell中的2个CSV文件,将差异导出到另一个文件。,powershell,scripting,dns,export-to-csv,reverse-dns,Powershell,Scripting,Dns,Export To Csv,Reverse Dns,我已经写下了这个剧本,我可以发誓我让它在今天早上给了我一个失败的机会,但现在我什么都没有得到,也不知道为什么 基本上,我需要比较$SourceAddress和$OutputAddress并将文件之间的差异导出到另一个CSV。任何关于如何获取输出文件的帮助都将不胜感激。或者,除了“Compare-Object”cmdlet之外,还有一种更好的比较方法。谢谢 ########################################################### # # Douglas

我已经写下了这个剧本,我可以发誓我让它在今天早上给了我一个失败的机会,但现在我什么都没有得到,也不知道为什么

基本上,我需要比较
$SourceAddress
$OutputAddress
并将文件之间的差异导出到另一个CSV。任何关于如何获取输出文件的帮助都将不胜感激。或者,除了“Compare-Object”cmdlet之外,还有一种更好的比较方法。谢谢

###########################################################
#
# Douglas Francis
# DNS Audit Script
# V 1.2
# Date: 05/06/2014
#
# Pulls IP address from a text file. Parses the IP's in the text file for their DNS entry and exports to a CSV file. 
# For IP address' that do not resolve get parsed to a new file.
#
#
###########################################################

#This creates an ouput folder called Script_Results to put the results in.

    $CheckFolder = Test-Path -PathType Container Script_Results
    if($CheckFolder -eq $false)
    {
        New-Item 'Script_Results' -Type Directory
    }



#This gets the IP address' from the file

    $IPADDR = Get-Content "C:\Users\douglasfrancis\Desktop\IP_test.txt"



#This logic runs through each line in the text file to get the IP's perform a reverse DNS search and pulls the IP and hostname to be outputted and sorted later. It also ignores the errors from failed results

    $SortIP =@()
    ForEach ($IPADDR in $IPADDR)
    {
        $ErrorActionPreference = "SilentlyContinue"
        $SortIP += [System.Net.DNS]::GetHostbyAddress($IPADDR)  | Add-Member -Name IP -Value $IPADDR -MemberType NoteProperty -PassThru | Select Hostname, IP 
    }



#Running through the data from $SortIP sorts by hostname and exports to CSV

    $SortIP | Sort -Property Hostname | Export-Csv     "C:\Users\douglasfrancis\Desktop\Script_Results\ReverseLookup.csv" -NoTypeInformation



#Here we start to work some voodoo magic. Instead of attempting to parse the errors from the failed DNS results we're going to compare the orginal IP address txt file to the output file of the DNS
#By comparing those two files we can determine what files failed to resolve a DNS and output that to a file for review.

    #Here we're reimporting the exported CSV file of the DNS results. Pulling the IP address column from it and then exporting it again with only the IP info.
    Import-csv "C:\Users\douglasfrancis\Desktop\Script_Results\ReverseLookup.csv" | Select IP | Export-csv -Path "C:\Users\douglasfrancis\Desktop\Script_Results\OutputAddress.csv" -NoTypeInformation

    #Now for some futher voodoo data manipulation. Going to take the source IP address file import it back in as a CSV file with a IP column header.
    # After that we will then reexport it back again as a CSV file so a comparision can be done between the two files.
    Import-csv "C:\Users\douglasfrancis\Desktop\IP_Test.txt" -Header "IP" | Export-csv -Path "C:\Users\douglasfrancis\Desktop\Script_Results\InputAddress.csv" -NoTypeInformation

    #Alright just a bit more voodoo for some more data manipulation. Going to take those parsed data files from above and assign them to a var for further magic
    $OutputAddress = Import-csv "C:\Users\douglasfrancis\Desktop\Script_Results\OutputAddress.csv" -NoTypeInformation
    $SourceAddress = Import-csv "C:\Users\douglasfrancis\Desktop\Script_Results\InputAddress.csv" -NoTypeInformation


#Finally here we make use of all the voodo magic and do a comparison of data so we can find out what IP's failed to resolve via DNS and export that to the failed file.

    Compare-Object $OutputAddress $SourceAddress | Select InputObject | Export-csv "C:\Users\douglasfrancis\Desktop\Script_Results\ReverseLookup_failed.csv" -NoTypeInformation



#Alrighty, one final thing to do some housecleaning and we're outta here!

    Remove-Item "C:\Users\douglasfrancis\Desktop\Script_Results\InputAddress.csv"
    Remove-Item "C:\Users\douglasfrancis\Desktop\Script_Results\OutputAddress.csv"

这是否有助于解决错误处理问题

$(ForEach ($IPADDR in $IPADDR)
    {
        Try {
             [System.Net.DNS]::GetHostbyAddress($IPADDR)  | 
              Select Hostname,@{label='IP';expression={$IPADDR}} 
            }

        Catch {
               Add-Content -Value "$IPADDR failed lookup" -Path "C:\Users\douglasfrancis\Desktop\Script_Results\ReverseLookup_failed.csv" 
              }

    }) | Sort -Property Hostname |
         Export-Csv "C:\Users\douglasfrancis\Desktop\Script_Results\ReverseLookup.csv" -NoTypeInformation

Voodoo并不是错误处理的好替代品。@mjolinor我以前曾试图让ps使用反向DNS故障导致的错误,并将故障源输出到文本文件中。不过,我得到的最好结果是powershell导出错误本身,而不是导致错误的行。因此,我们制作了两个csv文件,并对它们进行了比较。让我们看看是否无法解决您的错误处理问题,然后。是的,这正是我所需要的。这是一个更好的解决方案,我想做的是。现在我知道了为什么我之前尝试的try/catch我试图用try括号来运行catch。谢谢。很高兴我能帮忙。我们本可以让CSV比较的东西发挥作用,但我认为这将是今天学到的更有价值的东西:)。同意。我相信这在将来会更有用。