Send emails with Powershell mailto with formated text - powershell

My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
I appreciate any insights on this.
My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
Additional information:
Code:
$outPut = 'Start-Process'
$outPut+= '"mailto:'+$userMail+"?Subject=Password Reset"+"&Body=Hi, your password is $Password"
$outPut+= '";'
$mailFile = "Path" + $user.SAM + ".ps1"
$outPut | Out-File $mailFile
So, this takes the information this way and stored it in a ps1 file, then executed, opening a new Gmail tab with proper data.
I need that some words has format, bold for the password or hyper-link for guideness link...
Regards!

You haven't provided any indication of what you are doing. But the way to send emails via PowerShell is with the Send-MailMessage cmdlet. If you are using Send-MailMessage and formatting the body of the message with HTML, you just need to make sure you are using the -BodyAsHtml argument.
Here's an example:
$html = "<body><h1>Heading</h1><p>paragraph.</p></body>"
Send-MailMessage -To "bob#fake.com" -From "me#fake.com" -Subject "Test" -Body $html -BodyAsHtml

Related

HTML email body truncation while sending email from outlook using PowerShell

I have one HTML file which i want to send from PowerShell using outlook. I have used below code however it is truncating the email body after 603 chars, hence only upper half of the html page is going as body in outllook email.
$body = Get-Content -Path .\\Output.html #this have around 1500 chars.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "xyz#abc.com"
$Mail.Subject = "Daily Dump Status"
$body.ToString()
$Mail.HTMLBody = $body # while coping the data only 603 char are going into body.
$Mail.Send()
[System.Runtime.Interopservices.Marshal\]::ReleaseComObject($Outlook) | Out-Null
I have also tried options like Out-String while coping but didnt work
There is a limit of data you could set for a property. In the Outlook object model string properties are limited in size depending on the information store type.
To bridge the gap you need to use a low-level API on which Outlook is based on - Extended MAPI which allows opening properties with stream for writing huge amount of data. The IMAPIProp::OpenProperty method provides access to a property through a particular interface. OpenProperty is an alternative to the IMAPIProp::GetProps and IMAPIProp::SetProps methods. When either GetProps or SetProps fails because the property is too large or too complex, call OpenProperty.
You may also take a look at the famous and popular wrapper around Extended MAPI - the Redemption library.
After spending hours, finally got the root cause of this issue. The html page which i was trying to sent as email body was the output of another powershell command using ConvertTo-Html. And the output of PowerShell command had "NUL" in it.
$Mail.HTMLBody = $body
Hence above command was coping the data till first "NUL". I identified this by opening the html page in notepad++. Issue got resolved after replacing NUL with " "

Collecting user input with pop up window in Powershell

so I am trying to prepare a simple robocopy script. the script will be used by almost 500 users, so I am trying to keep it as simple and as user-friendly as possible.
to collect the information such as source and destination, i wanted to have a pop up window asking users to enter the information. I checked on the other forums and here as well, so far i found several alternatives, unfortunately none of them "does the trick"
Option 1 (my favorite except PowerShell hangs when I use it):
Add-Type -AssemblyName Microsoft.VisualBasic
$title = 'Your Current File Shares (Source)'
$msg = 'Please enter the EEX file share you want to copy from ( please make sure the format is \\server\share\...) :'
$source = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
Add-Type -AssemblyName Microsoft.VisualBasic
$title = 'Your new drive (Destination)'
$msg = 'Please enter to where you want to copy your files (please make sure you choose the full destination) :'
$destination = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
Robocopy $source $Destination /log:N:\logfile.txt
Option 2 (similar to option just a different way to call VB it seems): replying the first line Add-Type -AssemblyName Microsoft.VisualBasic with [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') , doesn't make it any better .
with both 2 options above PowerShell hangs after the 3rd run. SOmetimes after the first run it hangs. Odd thing is it runs succesfully and runs the robocopy command , but then after it's done it stops responding after a few minutes. Not sure if VB is looping in the background and has to be stopped?
Option 3:
$source = Read-Host 'Enter Your current file share:' -AsSecureString
$destination = Read-Host 'Enter Your new file share:' -AsSecureString
This is simple enough, however you cannot control anything in the box, which is something we can live with, but the main issue is the secure string, so it doesn't allow the user to see what he or she is typing, which would lead to a lot of human errors.
Option 4:
function copy_files {
param (
[string]$Copy_from,
[string]$Copy_to
)
[pscustomobject]#{
copy_from = $Copy_from
copy_to = $copy_to
}
}
$result = Invoke-Expression (Show-Command Copy_files -PassThru )
$result
has a form that is really not desirable , with the "copy" in the middle on the bottom (on the bottom of the form one sees ok / copy / cancel ) which would confuse users causing to hit copy and wait for something to happen (as the purpose is to transfer files…)
another negative thing is that it is very limited in terms what text and title you can use (or at least what I can use as I tried to have spaces but it wouldn't recognize it no matter if put them in quotations or double quotations). But such cosmetic con i can live with.
I cannot add any additional modules so showui for example is unfortunately not an option.
Any ideas on how this can be done?
Thanks in advance
There are pre-built scripts for this sort of thing that you can use as-is or tweak as needed.
The AutoCopier - PowerShell File Copy Utility w/GUI
A PowerShell GUI utility to copy files to computers based on a supplied text file of Hostnames/IP addresses. Please read below for more details and feedback would be appreciated!
Download: AutoCopier.ps1

Who to email based on attachment file

Without using a ton of ElseIf statements is it possible to select the the recipient of an email based on the file that will be attached to the email while iterating over all files in a folder?
I have started building this without the foreach get-ChildItem running over the folder where I create the email object, assign specific recipients, and choose a specific file out of the folder, but this is quite tedious and repetitive. I feel like there has to be a way to use an array of arrays or something where based on the file that the loop is on it pulls through the recipients and maybe a custom subject line.
There's tons of powershell email code out there so I won't repost that. Just not sure how to even attack this.
One option would be to have an accompanying CSV file, with rows for the filename, the recipient and the subject name etc. You could then import the CSV file using Import-Csv.
For example:
$emailList = Import-csv c:\users.csv
foreach ($line in $emailList) {
Write-Host "Sending message to $($line.emailAddress)"
Send-MailMessage -To $line.emailAddress `
-Subject $line.subject `
-Attachments $line.attachmentPath `
-Body $line.bodyText
}
and if you wanted to define the contents of the CSV in the PowerShell script, rather than using an external file, you could do something like:
$emailList = "emailAddress,subject,attachmentPath,bodyText
joe#bloggs.com,Attachment for Joe,c:\attachmentsToSend\joe.pdf,Attached is your file
Mary#bloggs.com,Mary's attachment,c:\attachmentsToSend\mary.pdf,Hello, Mary. Attached is your file
" | ConvertFrom-Csv
Put that above the foreach loop in the script

How can I use Powershell to extract mail headers from .msg file?

I'm trying to write a script that reads the mail headers from a directory full of .msg files so I can later parse them via regex. I tried $MSG = Get-Content .\message.msg, which could work, but it's a pretty dirty output. Has anyone tried this? I can't seem to find a working example online.
You have a few options depending on your environment. If you are on a computer with Outlook installed you can easily do this with an Outlook com object. The problem is that the headers are not exposed by default so you have to dig for them.
$ol = New-Object -ComObject Outlook.Application
$msg = $ol.CreateItemFromTemplate("SOME\PATH\TO\A\MSG\FILE.msg")
$headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
$headers
At this point you have a text block with all of the header information in it. If you want a specific header you will need to write a regex to extract it.
You could also write a class that reads the raw content based on the specification. Or read in the raw content with powershell and write a regex to attempt to extract it.

How to extract data from excel and send through mail automatically using powershell

Actually, I am very new to powershell and I dont have much knowledge about it. My requirement is:
I am working in MNC where we have to do shifts for 24/7. In our roster we have 3 shifts A,B & C. And all of threes having different timings in a day.
Once the roster has been created we need to send the emails to team about there shifts, and informing team about who all are there is A,B & C shifts. For that we need to do mannual work to get the data from Excel and send the mail to everyone.
But here i want to do it automated, like i want to prepare a script which extract the data from the roster excel sheet and send the mail automatically with all the members in the group on certain/specfic time.
Like we have 3 shifts so I want the mail to be sent three times in a day whenever new shift starts with all member details.
I request if anybody may help me in resolving my issue. I would be very greatful to him.
Regards
AH
It really sounds like you need to schedule a task to run the script 3 times a day as needed.
As far as getting the data out of Excel and into PowerShell I highly recommend utilizing a function from the MS Script Depot, Import-Xls (see http://gallery.technet.microsoft.com/scriptcenter/17bcabe7-322a-43d3-9a27-f3f96618c74b). The script is harmless, it seriously just loads the Excel doc you specify, asks Windows for an available temporary file name and then does a Save As to that name as a CSV file. Then it imports that CSV into PowerShell for you, and closes down Excel and performs a garbage collection routine.
The alternative is to do it all from the Excel ComObject and that's just ugly and more difficult than is needed. So, assuming you have a spreadsheet with headers Name, Email, and Shift (using A, B, or C for shift) what you probably want is something along the lines of:
Function Import-Xls {PASTED IN FROM SCRIPT REFERENCED ABOVE}
Switch((get-date).Hour){
{($_ -ge 8) -and ($_ -lt 16)}{$Shift = "A"}
{$_ -ge 16}{$Shift = "B"}
{$_ -lt 8}{$Shift = "C"}}
$Schedule = Import-Xls "C:\Users\Public\Desktop\Schedule.xlsx"
$CurrentWorkers = $Schedule|?{$_.Shift -eq $Shift}|Select Name
$Schedule|?{$_.Shift -eq $Shift}|%{send-mailmessage -to "$($_.Name) <$($_.email)>" -from "Management <GenMgr#example.com>" -subject "Shift listing for $(get-date -format M/d/yy) shift $Shift" -body $CurrentWorkers -smtpserver smtp.ourcompany.com}
So if that is run between 8AM and 4PM it assumes it is for Shift A (4PM to Midnight for B, and Midnight to 8AM for C), loads the Excel file, figures out who is on that shift, and sends them all an email who's body is simply a list of the people on that shift. More formatting or sending the mail through Outlook or something would be more work, but doable. This does also assume you have a SMTP server available to use. It at least covers how to get the data into PowerShell and how to make it automate picking a shift and select who to send mail to.