×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Copmare Console Output send to an Email?!

Copmare Console Output send to an Email?!

Copmare Console Output send to an Email?!

(OP)
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 --> Powershell

$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 --> Powershell

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> 

RE: Copmare Console Output send to an Email?!

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...

RE: Copmare Console Output send to an Email?!

(OP)
@Rick998 Many Thanks!!

Yes it helps.

Only the output in the email text is not optimal.

CODE --> Powershell

$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 --> Email

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 

RE: Copmare Console Output send to an Email?!

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...

RE: Copmare Console Output send to an Email?!

(OP)
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.

RE: Copmare Console Output send to an Email?!

Show us what you have tried...

RE: Copmare Console Output send to an Email?!

(OP)
Hello

i would like to design the output like this

CODE --> Powershell

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

RE: Copmare Console Output send to an Email?!

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...

RE: Copmare Console Output send to an Email?!

(OP)
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 --> Powershell

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

tried, but unfortunately without success.
The "ZZ" was an Test winky smile

RE: Copmare Console Output send to an Email?!

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...

RE: Copmare Console Output send to an Email?!

(OP)
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 winky smile

RE: Copmare Console Output send to an Email?!

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

RE: Copmare Console Output send to an Email?!

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close