I'm passing selected Outlook.MailMessage properties from a VBA script to PowerShell:
Public Sub InvokeMessageParser()
For Each Item In Application.ActiveExplorer.Selection
If TypeName(Item) = "MailItem" Then
Dim MailItem As Outlook.MailItem: Set MailItem = Item
// arrange
Dim cmd As String: cmd = "powershell -NoExit -NoProfile -File C:\Users\[user]\Desktop\Invoke-MessageParser.ps1 -Subject """ & MailItem.Subject & """ -Verbose"
// act
i = Shell(cmd, vbNormalFocus)
End If
Next
End Sub
Invoke-MessageParser.ps1:
param (
[string]$Subject
)
Write-Host "Subject: $Subject"
Which prints the expected value to the console.
I would prefer to pass the entire message, but haven't been able to get the syntax correct:
// arrange
Dim cmd As String: cmd = "powershell -NoExit -NoProfile -File C:\Users\[user]\Desktop\Invoke-MessageParser.ps1 -Message " & MailItem
Invoke-MessageParser.ps1:
param (
[object]$Message
)
Write-Verbose "Subject: $($Message.Subject)"
Which results in Subject: being printed to the console.
Is this possible?
$Messageis aSystem.String.