gpt4 book ai didi

powershell - 如何在Power Shell中比较两个文本文件并将差异发送到电子邮件

转载 作者:行者123 更新时间:2023-12-02 23:54:02 25 4
gpt4 key购买 nike

我有2个文本文件,其中包含SQL函数名称列表。我希望能够比较$ A和$ B中的列表。然后,我想要$ B中而不是$ A中的SQL函数名称的列表,即列表$ C作为电子邮件地址的附件发送。

这是我的代码:

   $A = "c:\ReferenceFunctions.txt"
$B = "c:\GeneratedFunctions.txt"
$C = "c:\FileWithDifferences.txt"


$fromaddress = "noreply@xyz"
$toaddress = "123@hhh"
$bccaddress = "3333@nnn.com"
$login = "yourlogin"
$password = "password" | Convertto-SecureString -AsPlainText -Force
$smtpserver = "gggg.com"
$smtp = new-object Net.Mail.SmtpClient($smtpserver, 800)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($login,
$password);
$body = "see attached "
$Subject = "checking differences"
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
#$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject

$message.body = $body
$message.Priority = [System.Net.Mail.MailPriority]::High

Compare-object (Get-Content $A) (Get-Content $B) | Out-File $C

#Attach the eported file with differences
$attachment = $C
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)

IF (Compare-Object -ReferenceObject (Get-Content $A) -DifferenceObject (Get-Content $B)){


$smtp.Send($message);
}

变量$ A具有以下信息:
fn_StatementEndOfMonth                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
fn_Performance_Sql
fn_DailyReturns

变量$ B具有以下信息:
fn_Performance_Sql                                                                                                                                                                                     
fn_DailyReturns
fn_SQLPerfomance_Monitor

$ C然后应包含
fn_SQLPerfomance_Monitor

此代码的问题在于,它正在文件$ A和$ B中发送带有所有SQL函数名称的附件,而不是发送差异。我似乎无法弄清楚哪里出了问题。有谁能帮忙吗?一直试图弄清楚整天,但没有运气。

最佳答案

看来您实际上想做一个与比较对象完全相反的事情。要获得您在$ C中所需的确切信息(我不确定为什么'fn_StatementEndOfMonth'也不应存在),您可以执行以下操作:

$a = 'fn_StatementEndOfMonth
fn_Performance_Sql
fn_DailyReturns' -split '[\r\n]'

$b = 'fn_Performance_Sql
fn_DailyReturns
fn_SQLPerfomance_Monitor' -split '[\r\n]'

$c = $b | % { if($_ -notin $a){$_} }

$c

fn_SQLPerfomance_Monitor

关于powershell - 如何在Power Shell中比较两个文本文件并将差异发送到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50510142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com