Add sensitivity label to email via powershell - powershell

I've got a need to send email within PowerShell and have all the components except the sensitivity label. For example:
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = 'someone#co.com'
$Mail.Subject = 'Special subject'
$SigString=Get-Content "C:\Users\someone\AppData\Roaming\Microsoft\Signatures\sig.htm"
$Mail.HTMLBody = 'blah blah blah.'
$Mail.HTMLBody += $SigString
$Mail.Send()
Unfortunately when this runs I'm prompted for the sensitivity label. I thought "Sensitivity = " might work and it does allow values 0 thru 3 but that doesn't help.
Is this possible to do?

I also had this problem and I could not find any way to do this "outside" of an Office program as this seems to be a built-in enterprise function allowing only a given set of sensitivity labels set by your comany.
If Python can be an alternative, you this answer can automate sensitivity label setting. Alternatively, having a pre-stored template that can be copied and then edited would also work with PS.

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

Marking Emails as Read

I might be asking this in the wrong place so I apologize. I'm running a PS1 file to look in a specific folder, mark the emails as read, save the attachments and send me an email. For whatever reason, marking the emails as read, doesn't always work.
Since everything else works perfectly, I'll leave you with just what is not working
#Open Outlook and find emails from today
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNamespace('mapi')
$mb = $ns.Stores['some#email.com'].GetRootFolder()
$inbox = $mb.Folders['Reports']
$inbox.Items | ForEach-Object {$_.UnRead = $false}
If I run the script in ISE one line at a time, it works.
The problem arises when I select all and run in ISE, or as a PS1
Thanks!

Reading a text file line by line

I'm new to power shell and can't seem to get this script correct.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$users1\$users.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$users"
$Shortcut.Save()
Every time it goes to "users.txt" it tries to read it as a complete file. Instead i would like for it to read as followed:
User1
User2
User3
I just can't seem to get this to work.
RE-EDIT:
What I’m trying to accomplish:
We went from one storage server to a new storage server.
ASRV1 to FSSRV
I want to create a shortcut from asrv1 to fssrv. Along with the upgrade we are also changing domains and changing everyone windows user names.
For example my user name was fMunoz and it was changed to fMunoz00.
I want to pull from a text file > users.txt with all the old usernames, and create shortcuts to the new user names storage file those users names are in a txt file called users1.txt.
If I understand your request, you want to create shortcuts in each home directory of the people in Users1.txt
This means you will need to complete multiple ForEach loops to cycle through both txt files.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
# For every line in $users1, Do something
Foreach($User in $users1) {
# For every line in $users, Do something
Foreach($Name in $users) {
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$User\$Name.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$Name"
$Shortcut.Save()
}
}
I want to thank everyone for helping me.
I came up with a different solution for my problem. The way i fixed this issue was by creating an excel sheet w/ 2 columns.
OLD USERS | NEW USERS
I then created a mail merge using microsoft word. Then i continued with the following code in powershell to start my loop:
$WshShell = New-Object -comObject WScript.Shell
Once my loop was started with i then utilized mail merge to take <> from the excel sheet and replaced it with the insert it into my code. Also took <> and did the same.
$Shortcut = $WshShell.CreateShortcut("\Fssrv\homeshares$\«New_Users»\«Old_Users»_Z Drive.lnk")
$Shortcut.TargetPath = "\asrv1\users\«Old_Users»"
$Shortcut.Save()
I hope this can help anyone else trying to accomplish something similar.
Yet again, Thanks & Good Luck...

Edit email using powershell and retain the HTML format

Hi I am trying to edit the HTML email template using powershell which has some pictures and colours.
I need to edit and replace some content in email and retain the html body. When i try the below script it's converting to text format
$file= 'FILE PATH '
$outlook= New-Object -ComObject outlook.application
$msg= $outlook.createitemfromtemplate($file)
$msg.body= $msg.body -replace "December\d*", "TEST$a"
$msg.saveas($file)
You need to use HTMLBody and not body. Because you are telling it to use plane text in the email rather than HTML. Hope this helps.
$file= 'C:\Temp\template.msg'
$outlook= New-Object -ComObject outlook.application
$msg= $outlook.createitemfromtemplate($file)
$msg.HTMLbody = $msg.HTMLbody.Replace("color2", "color")
$msg.saveas($file)
#$file | ConvertTo-Html #if needed
Test scenario:
Before
Code was ran:
Code
After:
After
I was having the same issue than I used $mail.Save() after replacing de part I want on html body than everything worked.
Without saving before sending I got an text message rather than html.

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.