Powershell using send-mailmessage - powershell

i have the below code:
$file = Get-ChildItem -Path C:\MonitorTest | where {$_.LastWriteTime -lt (Get-Date).AddHours(-4) }
$message = #{
to="sender#mail.com"
from="monitoring#mail.com"
subject="testing monitoring"
smtpserver="mail.com"
bodyashtml=$true
body="$file.Name"
}
Send-MailMessage #message
the thing i have is that on Body, using "" it prints the content of the variable $file.name in one single line. (New Text Document (2) - Copy.txt New Text Document (2).txt New Text Document (3) - Copy.txt)
if i only select $file.name and run it, it will print it in lines
New Text Document (2) - Copy.txt
New Text Document (2).txt
New Text Document (3) - Copy.txt
if i run the send-mailmessage #message, the email comes thru, but its on one line as if running only "$file.name"
these are the contents of $file:
Directory: C:\MonitorTest
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/13/2020 3:57 PM 0 New Text Document (2) - Copy.txt
-a---- 8/13/2020 3:57 PM 0 New Text Document (2).txt
-a---- 8/13/2020 3:57 PM 0 New Text Document (3) - Copy.txt
-a---- 8/13/2020 3:57 PM 0 New Text Document (4) - Copy.txt
-a---- 8/13/2020 3:57 PM 0 New Text Document (5) - Copy.txt
-a---- 8/13/2020 2:13 PM 0 New Text Document.txt
Can anyone help me figure this out please? im running bonkers trying to figure this out.
Thanks in advance!

You are sending as html, so you will need html line breaks <br>, which can be achieved as follows:
$filesHtml = (Get-ChildItem -Path C:\MonitorTest | where {$_.LastWriteTime -lt (Get-Date).AddHours(-4) } | Select-Object -ExpandProperty Name) -join "<br>" | Out-String
$message = #{
to="sender#mail.com"
from="monitoring#mail.com"
subject="testing monitoring"
smtpserver="mail.com"
bodyashtml=$true
body=$filesHtml
}
Send-MailMessage #message
Or you can send as text:
$files = Get-ChildItem -Path C:\MonitorTest | Where-Object {$_.LastWriteTime -lt (Get-Date).AddHours(-4) } | Select-Object -ExpandProperty Name | Out-String
$message = #{
to="sender#mail.com"
from="monitoring#mail.com"
subject="testing monitoring"
smtpserver="mail.com"
bodyashtml=$false
body=$files
}
Send-MailMessage #message

Related

Foreach loop not reading files in numeric order

I have below files which i am reading using a foreach loop.
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
C:\Script\LF_Batch_1.csv
C:\Script\LF_Batch_10.csv
C:\Script\LF_Batch_11.csv
C:\Script\LF_Batch_12.csv
C:\Script\LF_Batch_13.csv
C:\Script\LF_Batch_14.csv
C:\Script\LF_Batch_15.csv
C:\Script\LF_Batch_16.csv
C:\Script\LF_Batch_17.csv
C:\Script\LF_Batch_18.csv
C:\Script\LF_Batch_19.csv
C:\Script\LF_Batch_2.csv
C:\Script\LF_Batch_20.csv
C:\Script\LF_Batch_21.csv etc...upto LF_Batch_.96.csv
Problem is it is reading the files like above not 1,2,3...and so on.
Please need idea how to read in ordered way
Solved using below approach
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$GetGeneratedFiles = $GetGeneratedFiles | Sort-Object $ToNatural
Thanks #vonPryz for the reference.
Another way. It's funny how I just did another answer similar to this. A numeric sort on the names.
echo hi | set-content (1,2,10,20 | % tostring LF_Batch_0\.csv)
dir | sort {[void]($_ -match '\d+'); [int]$matches.0}
Directory: C:\Users\js\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/17/2020 9:14 AM 4 LF_Batch_1.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_2.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_10.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_20.csv

How to split string and rename files in PowerShell?

I want to bulk rename the files in my folder, and all of them have the format of FilenameYeara\b.pdf, for example, TestData2001a.pdf, File2015b.pdf. I want to rename all of them to something like [Yeara\b]Filename, such as [2001a]TestData. The problem is that I don't know how can I split my filename into two parts (actually three if we count the extension, .pdf part), such that I put that second part as the first part of the file name.
Get-ChildItem | Rename-Item {$_.name -replace ‘current’, ’old’ }
How can I achieve this?
This does the regex match "anything, four digits, one character, .pdf" and replaces it with those items in the new ordering.
PS D:\t> gci | ren -newname { $_ -replace '(.*)(\d{4})(.)\.pdf', '[$2$3]$1.pdf' }
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:54 0 File2015b.pdf
-a--- 13/05/2016 02:53 0 TestData2001a.pdf
becomes
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:53 0 [2001a]TestData.pdf
-a--- 13/05/2016 02:54 0 [2015b]File.pdf
(Maybe try it with -Whatif before running for real)
This should get you started
$Matches.Clear()
Get-Item | % {
$_.BaseName -match "(\D+)([0-9]{4}[ab])"
Rename-Item -Path $_.FullName -NewName "$($Matches[2])$($Matches[1])$($_.Extension)"
}

Finding a file that has highest number in the filename using powershell

Sorry guys..I am new to powershell. Would be great if someone help with the following scenario:
I have couple of files in a folder c:\test
sample.x.x.1
sample.x.x.2
sample.x.x.3
sample.x.x.4
sample.x.x.5
I want to find the name of the file which has the highest number in its name in the given folder. In the above example, 5 is the highest number and the script should return the output filename as sample.x.x.5
Thanks in advance!
Sorting file names with numbers is quite a problem, as there are two ways. The first one sets them to alphabetical order. That is, 0, 1, 11, 111, 2,... The second one uses natural order. That is, 0, 1, 2, 11, 111.... This is surprisingly tricky and about every third programmer is confused with this.
There's a good answer already, which I'll refer like so,
# Create files 1..5
for($i=1;$i -le 5; ++$i) { set-content sample.x.x.$i -Value $null }
# Tricksy! Create file .10 to confuse asciibetic/natural sorting
set-content sample.x.x.10 -Value $null
ls # Let's see the files
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.1
-a---- 2015-09-28 10:29 0 sample.x.x.10
-a---- 2015-09-28 10:29 0 sample.x.x.2
-a---- 2015-09-28 10:29 0 sample.x.x.3
-a---- 2015-09-28 10:29 0 sample.x.x.4
-a---- 2015-09-28 10:29 0 sample.x.x.5
# Define helper as per linked answer
$ToNatural = { [regex]::Replace($_, '\d+$', { $args[0].Value.PadLeft(20,"0") }) }
# Sort with helper and check the output is natural result
gci | sort $ToNatural -Descending | select -First 1
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.10
Alphabetical sorting.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") | sort
sample.x.x.1
sample.x.x.11
sample.x.x.5
Numerical sorting.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") |
sort -Property #{Expression={[Int32]($_ -split '\.' | select -Last 1)}}
sample.x.x.1
sample.x.x.5
sample.x.x.11
Largest number.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") |
sort -Property #{Expression={[Int32]($_ -split '\.' | select -Last 1)}} |
select -Last 1
sample.x.x.11

Don't want some part information in output

Hi Guys I have the following code which search a folder and return any string containing the value down:
filter MultiSelect-String( [string[]]$Patterns ) {
# Check the current item against all patterns.
foreach( $Pattern in $Patterns ) {
# If one of the patterns does not match, skip the item.
$matched = #($_ | Select-String -Pattern $Pattern)
if( -not $matched ) {
return
}
}
# If all patterns matched, pass the item through.
$_
}
Get-ChildItem -recurse | MultiSelect-String 'report','Product1'
So if the code gets the file it displays it like that:
Directory: C:\Users\sarvesh.nundram\Desktop\PMI\RPD_Extract_XML\SQL_tobemigrated2\GROUP1
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/21/2013 1:07 PM 133279 Acapulco
What if I don't want these info:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/21/2013 1:07 PM
this?
Get-ChildItem -recurse |
MultiSelect-String 'report','Product1' |
select fullname # or select -expa fullname

How to set LastWriteTime property of a file?

I would like to change the creation date of the files that I generate with this script :
$clientname = Read-Host "Enter the client name"
$path = Read-Host "Enter the complete path of .bak files"
$time = "01-00-00"
$space = " "
for ($i = 0; $i -lt 7;$i++)
{
$date = (Get-Date).AddDays(-1-$i).ToString('yyyy-MM-dd')
New-Item -ItemType file $path$clientname$space$date$space$time.bak
}
So it gives me theses files :
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 16/08/2013 16:55 0 client 2013-08-15 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-14 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-13 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-12 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-11 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-10 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-09 01-00-00.bak
I want to modify the LastWriteTime property of each files, I want it to be the same as the date in the file name.
Example for this file "client 2013-08-15 01-00-00.bak" the LastWriteTime will be "15/08/2013 01:00"
I'm stuck and I do not how can we do that
Thank you
Not tested, but try this in your loop after you call New-Item:
$file = Get-ChildItem $path$clientname$space$date$space$time.bak
$file.LastWriteTime = (Get-Date).AddDays(-1-$i)
If you want to see a full listing of things you can do with FileInfo objects, try calling $file | gm in your powershell console. You could also view the docs on MSDN.
for ($i=0; $i -lt 7;$i++)
{
$date = (Get-Date).AddDays(-1-$i)
$filedate = $date.ToString('yyyy-MM-dd')
$file = New-Item -ItemType File $path$clientname$space$filedate$space$time.bak
$file.LastWriteTime = $date
}