Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copmare Console Output send to an Email?!

Status
Not open for further replies.

sugram

Technical User
Feb 5, 2021
26
DE
Hello

I have this powershell that checked the directories for me.
Everything works fine.

Of course I get 3 emails through the forech.

My question:
is it possible to only receive one email with the content of the console?
Or an email with an overview of which files are different on the systems?


The Powershell script:

Code:
$ref_dir = '\\PC1\Share\123\xyz'
$filefilter = '*.pvx'
$shares = "\\PC2\Share\123\xyz","\\PC3\Share\123\xyz","\\PC4\Share\123\xyz"
$ref = ls "$ref_dir\*" -File -Filter $filefilter -exclude '*Cali*','*Light*','*ORG*','*org*','*save*','*EA*','*TEST*'
foreach ($share in $shares){
    $diff = compare @($ref) @(ls "$share\*" -File -Filter $filefilter -exclude '*Cali*','*Light*','*ORG*','*org*','*save*','*EA*','*TEST*') -Property Name,LastWriteTime 
    if ($diff){
        $diff | ft -AutoSize
        Send-MailMessage –To "Email@xyz.com" `
                   –Subject ("ACHTUNG PVX DATEI UNTERSCHIEDLICH!")`
                   –Body ("Die Datei $($diff.Name) $($diff.LastWriteTime) ist auf $share unterschiedlich")`
                   –SmtpServer "smtp.com" `
                   –From "noreplay@domain.com" `
                   -encoding ([System.Text.Encoding]::UTF8)
    }
}

i mean this Output

Code:
PS C:\Windows\System32\WindowsPowerShell\v1.0> U:\Test\Powershell\Check-PVX.ps1
\\PC2\Share\123\xyz hat unterschiedliche Dateien

Name                                     LastWriteTime       SideIndicator
----                                     -------------       -------------
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.2021 08:22:06 =>           
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.1900 00:00:01 <=           


\\PC3\Share\123\xyz hat unterschiedliche Dateien

Name                                     LastWriteTime       SideIndicator
----                                     -------------       -------------
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.2021 08:22:06 =>           
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.1900 00:00:01 <=           


\\PC4\Share\123\xyz hat unterschiedliche Dateien

Name                                     LastWriteTime       SideIndicator
----                                     -------------       -------------
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.2021 08:22:06 =>           
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx 09.03.1900 00:00:01 <=           



PS C:\Windows\System32\WindowsPowerShell\v1.0>
 
How about taking the Send-MailMessage portion out of the loop and use -append to the clipboard instead then send the contents of the appended results to Send-MailMessage afterwards?

Use Set-Clipboard before the loop to set its contents to null, Set-Clipboard -Append within the loop then use Get-Clipboard to populate the email contents.

(See this MS doc for more info.)

Hope this helps...
 
@Rick998 Many Thanks!!

Yes it helps.

Only the output in the email text is not optimal.

Code:
$ref_dir = '\\PC1\Share\123\xyz'
$filefilter = '*.pvx'
$shares = "\\PC2\Share\123\xyz","\\PC3\Share\123\xyz","\\PC4\Share\123\xyz"
$ref = ls "$ref_dir\*" -File -Filter $filefilter -exclude '*Cali*','*Light*','*ORG*','*org*','*save*','*EA*','*TEST*'
set-Clipboard -Value $null
foreach ($share in $shares){
    $diff = compare @($ref) @(ls "$share\*" -File -Filter $filefilter -exclude '*Cali*','*Light*','*ORG*','*org*','*save*','*EA*','*TEST*') -Property Name,LastWriteTime 
    if ($diff){
        $diff | ft -AutoSize
        Set-Clipboard -Value $share -Append
        Set-Clipboard -Value $diff -Append
        Set-Clipboard -Value "<br />" -Append
    }
} 
$Ausgabe = Get-Clipboard

Send-MailMessage –To "Email@xyz.com" `
                   –Subject ("ACHTUNG PVX DATEI UNTERSCHIEDLICH!")`
                   –BodyAsHtml ("Die Datei <br /> $Ausgabe <br /> ist unterschiedlich")`
                   –SmtpServer "smtp.com" `
                   –From "noreplay@domain.com" `
                   -encoding ([System.Text.Encoding]::UTF8)

Email Message

Code:
Die Datei 
\\PC2\Share\123\xyz @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; SideIndicator==>} @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01; SideIndicator=<=} 
\\PC3\Share\123\xyz @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; SideIndicator==>} @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01; SideIndicator=<=} 
\\PC4\Share\123\xyz @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; SideIndicator==>} @{Name=Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01; SideIndicator=<=} 

ist unterschiedlich
 
It looks like you need to add a carriage return plus newline (`r`n) between your variable and the subsequent instruction to -append it.

(Apologies if I've got it wrong. Trying to view on a tiny phone screen is rubbish.)

Hope this helps...
 
Good Idea

I just don't know in which line or where I would have to insert the ('r'n.)
My attempts so far have all failed.
 
Hello

i would like to design the output like this

Code:
Die Datei 
\\PC2\Share\123\xyz 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01
\\PC3\Share\123\xyz 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01;  
\\PC4\Share\123\xyz 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/2021 08:22:06; 
Lx_St1.1_EVGV1_Defekte_Deckel_hinten.pvx; LastWriteTime=03/09/1900 00:00:01;

ist unterschiedlich

so i would have to change the output of the compares, but i haven't found a solution for that yet
 
You've already got the basic data in the clipboard. Rather than spend time amending the pipeline to get what you want, why don't you just do two replacements within the clipboard data:

1) Replace:

Code:
; SideIndicator==>} @{Name=

... with a carriage return plus newline (`r`n). This will seperate the files into their own lines. Do this first.

2) Replace:

Code:
 @{Name=

... with a carriage return plus newline (`r`n). (Note: There's a space in fromt of the @ character.) This will seperate the device (for example, \\PC2\Share\123\xyz) from the subsequent files.

Finally, use $Ausgabe = Get-Clipboard to get the now amended clipboard data.

Hope this helps...
 
Hello

Many Thanks

Ok, but i don't know how can i change the Clipboard information.
I have to find out more about that first.

i have this

Code:
$Ausgabe = Get-Clipboard
$Ausgabe = $Ausgabe -replace "; SideIndicator==>} @{Name=", "ZZ"

tried, but unfortunately without success.
The "ZZ" was an Test ;-)
 
You don't need the -replace operator, just the Replace method, for example:

Code:
$Ausgabe = "\\PC2\Share\123\xyz @{Name=".Replace("\\PC2\Share\123\xyz @{Name=", "\\PC2\Share\123\xyz`r`nZZ")

(You probably only need `n [i.e. backtick+n], not `r`n...)

The -replace operator is more useful for regular expression replacements but is more complex to implement. Yours appear to be simple string replacements so the Replace method is much easier to use.

Hope this helps...

 
Many Thanks.

I now have to find the correct "format" to replace so that I can get the output I want.
Is still a bit difficult for me at the moment ;-)
 
Glad you got it sorted.

I still find PowerShell to be difficult... so it's a lot of trial and an even greater amount of error. [smile]
 
I don't get it sorted yet, at least not as I would like.
Currently I have the problem that I only get one change displayed.
Only from one PC and not from all of them as before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top