Powershell and Outlook - powershell

I have a problem readiing emails from an Inbox using powershell - however I can red emails from another folder (Test) without a problem. The code I have is
$account = "aaaa.bbb#xxxx.net"
$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(“MAPI”)
$Account = $n.Folders | ? {$_.Name -eq $account}
$f = $Account.Folders | ? {$_.Name -match "Test"}
$f.Items | ForEach {
Do Stuff
}
How do I amend the code so it read the emails from the Inbox and not the folder Test. Another thing is that there's another email account attached and that to has an inbox. How do I make sure I'm looking at the Inbox for the email address specified?
Thanks in Advance
G

I'm doing something similar and I'm using the following (I haven't tested yet on multiple mailboxes yet but it works for my own email)
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -ComObject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$emailAddress = "..."
$recipient = $namespace.CreateRecipient($emailAddress)
$folder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInbox)

Related

Downloading .xlsx attachment from Outlook of Specific Date using Powershell

I have the below script. This $Tests shows the list of .xlsx attachment of specific date but is not able to download and throws an error. Please find the below script.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder = $inbox.Folders | Where-Object {$_.Name -eq “Test”}
$mail=$subfolder.Items |Select-Object -Property "ReceivedTime",#{name="Attachments";expression={$_.Attachments|%{$_.DisplayName}}} | Where-Object{$_.attachments -match ".xlsx" -and ($_.receivedtime -match "9/15/2020")} | Select-Object "attachments"
$Test = $mail.attachments
foreach ($out in $test) {$_.attachments|foreach {
Write-Host $_.filename
$Filename = $_.filename
If ($out.Contains("xlsx")) {
$_.saveasfile((Join-Path $FilePath "$out")) }}}
I am able to filter the .xlsx Attachments with Specific Date. But after this, I don't know how to save/download them.
Working with com objects can be rather frustrating in powershell. I recommend you get extremely familiar with Get-Member. You really have to interrogate each object. I've simplified your script as well as tested thoroughly. It will download each matching attachment (name) from each match email (received date)
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder.Items | Where-Object {$_.receivedtime -match "9/20/2020" -and $($_.attachments).filename -match '.xlsx'} | foreach {
$filename = $($_.attachments | where filename -match '.xlsx').filename
foreach($file in $filename)
{
Write-Host Downloading $file to $filepath -ForegroundColor green
$outpath = join-path $filepath $file
$($_.attachments).saveasfile($outpath)
}
}
You may use this for more of an "in-line" approach.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder.Items | Where-Object {$_.receivedtime -match "9/20/2020" -and $($_.attachments).filename -match '.xlsx'} | foreach {
foreach($attachment in $($_.attachments | where filename -match '.xlsx'))
{
Write-Host Downloading $attachment.filename to $filepath -ForegroundColor green
$attachment.SaveAsFile((join-path $FilePath $attachment.filename))
}
}

Using Powershell to Do Action Every Time a Specific Outlook Email is Received

I receive a lot of emails from people to copy a file from one file location to another. I would like my Powershell program to automate this process that whenever I receive an email with a specific subject line, my program completes the action above and sends an email to the sender that this has been completed. My copy and paste code, testing the path, and sending an email by themselves work. Running the code in its entirety doesn't give me an error but also doesn't do anything. I have left comments in the code below where I think the problems are (can't be sure though)
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$emails = $inbox.Items | Where-Object {$_.subject -match ‘COPY FILE’} #I think might be wrong
if ($_.subject -match 'COPY FILE') #I think might be wrong
{
Copy-Item -Path ($oldLocation + $fileName) -Destination $newLocation
if (Test-Path -Path ($newLocation + $fileName))
{
$mail = $outlook.CreateItem(0)
$mail.To = $sender
$mail.Subject = "Completed"
$mail.Body = "File has been successfully moved"
$mail.Send()
}
else
{
$mail = $outlook.CreateItem(0)
$mail.To = $sender
$mail.Subject = "Failed"
$mail.Body = "File does not exist in $newLocation"
$mail.Send()
}
}
Your If statement is referring to $_ but there's no surrounding loop or pipeline.
Just a suggestion but maybe:
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$emails = $inbox.Items | Where-Object {$_.subject -match ‘COPY FILE’} #I think might be wrong
$Emails |
ForEach-Object{
if ($_.subject -match 'COPY FILE') #I think might be wrong
{
Copy-Item -Path ($oldLocation + $fileName) -Destination $newLocation
if (Test-Path -Path ($newLocation + $fileName))
{
$mail = $outlook.CreateItem(0)
$mail.To = $sender
$mail.Subject = "Completed"
$mail.Body = "File has been successfully moved"
$mail.Send()
}
else
{
$mail = $outlook.CreateItem(0)
$mail.To = $sender
$mail.Subject = "Failed"
$mail.Body = "File does not exist in $newLocation"
$mail.Send()
}
}
}
Also; I don't know if they are defined elsewhere but I don't see any definition for $oldlocation, $filename, or $newlocation. I'd have thought you need to parse those out of the subject or body of the message.
$Sender, might need to be $_.Sender in this example.

how to access email from a shared outlook mailbox

I need to get a the latest email from a shared outlook mailbox.
The shared inbox is eg. "Server Backup" and the email that i want to get is located
inbox\backup report\
Here is some code that I can access my Inbox but can't do it for a subfolder
$Outlook = New-Object -ComObject Outlook.Application
$OutlookFolders = $Outlook.Session.Folders.Item(1).Folders
$OutlookInbox = $Outlook.session.GetDefaultFolder(6)
#read the latest email
$latestmail=$OutlookInbox.items | select -last 1
What you are after is to navigate a little further down.
$outlook = New-Object -Com Outlook.Application
$MAPI = $Outlook.GetNamespace("MAPI")
# Gets all mailboxes tied to the account
$Mailbox = $MAPI.Folders("SharedEmail#Company.com")
# Gets the Inbox folder
$Inbox = $mailbox.Folders("Inbox").Folders("backup report")
# Shows all emails from the Inbox
$contents = $Inbox.Items
$contents.Sort("ReceivedTime", $true)
$contents | select Subject, SenderName, CreationTime -First 1
EDIT: Since the above didn't work, try the below. It will search for the Inbox, then pipe that through to look for the Server Backup.
$Outlook = New-Object -ComObject Outlook.Application
$OutlookFolders = $Outlook.Session.Folders.Item(1).Folders
(($OutlookFolders | Where-Object {$_.FolderPath -like "*Inbox*"}).Folders | `
Where-Object {$_.FolderPath -like "*Server Backup*"}).Items | `
select Subject, SenderName -Last 1
$outlook = New-Object -comobject outlook.application
$namespace = $outlook.application.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder(6)
#goto the inbox\backup eport and select the latest email and place it in variable
#$newreport
$newreport = $namespace.Folders.Item("Server
Backup").Folders.Item('Inbox').Folders.item('backups').items | select -first 1
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
Add-Type -assembly "System.Runtime.Interopservices"
$outlook = new-object -com outlook.application;
$namespace = application.GetNameSpace("MAPI")
$i = 1
$Sender = "abc#123.com" # email id
$Sender1 = "cde#123.com" # email id
$myRecipient = $namespace.CreateRecipient("Email Id") # Email id of the shared mail box
$inbox = $Namespace.GetSharedDefaultFolder($myRecipient ,[Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
# Save email contect to a file in a location
$inbox.Items | ?{$_.SenderName -match $Sender -and $_.UnRead -eq "No" } | sort receivedtime -desc |
%{
$Filepath = "C:\temp\" + "email" + $filecount +".txt"
echo $_.body | Out-File -FilePath $Filepath #do stuff with body
$i = $i + 1
$filecount = " (" + $i + ")"
$_.Unread= $False #mark as read
}
# Save email attachment to a location
$inbox.Items | ?{$_.SenderName -match $Sender1 -and $_.UnRead -eq "No" } | sort receivedtime -desc |
%{
$Filepath = "C:\Temp\"
$_.attachments| ?{$_.saveasfile((Join-Path $filePath $_.FileName)) }
$_.Unread= $False #mark as read
}

powershell script to export all outlook emails from folders/subfolders to csv file

I need to export all emails from a shared Outlook inbox. The emails are sorted into sub-folders.
I've tried running a PowerShell function in the command window.
Function Get-OutlookInBox {
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Folder = $Namespace.GetDefaultFolder($olFolders::olFolderInBox)
$Folder.Items |
Select-Object -Property Subject, ReceivedTime, SenderName,
SenderEmailAddress |
Export-Csv -NoTypeInformation XXXX\Trial.csv
}
Does not generate a CSV file.

Save an open Excel sheet using Powershell

I am completely newbie to Powershell. Need your help in saving an opened excel sheet using Powershell.
Script goes something like this
$xlPasteValues = -4163
$xlCellTypeLastCell = 11
$xl = new-object -comobject excel.application
$xl.Visible = $True
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$i = 1
$collection = Get-ChildItem C:\Test\* -include *.csv # Change the location of your CSV files here.
$length = 4
foreach ($item in $collection) {
$wb1 = $xl.Workbooks.Open("$item")
$array = $item.ToString()
$delim = "\"
$SheetName = $array.split($delim)
$s = $SheetName[2]
$sn = $s.split(".")
$nsn = $sn[0]
$ws1 = $wb1.worksheets | where {$_.name -eq $nsn}
Write-Host $item $nsn
$used = $ws1.usedRange
$used.Select()
$used.copy()
$wb.Activate()
$ws = $wb.Sheets.Add()
$ws2 = $wb.worksheets | where {$_.name -eq "sheet$i"}
[void]$ws2.Range("A1").PasteSpecial(-4163)
$ws2.name = $nsn
$i++
$wb1.Close()
}
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat =[Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $true
Your question was rather vague so I'm assuming that you want to know how to open and save an Excel document through Powershell.
Open your Excel Document using New-Object
$a = New-Object -COM "Excel.Application"
$a.Visible = $true
$b = $a.Workbooks.Open("C:\PATH\TO\YOUR\EXCEL\sheet.xlsx")
Save and close your document
$b.Save()
$b.Close()
Check out my PowerShell Excel Module on Github. You can also grab it from the PowerShell Gallery.
Then try:
$xlFileName="c:\temp\test.xlsx"
dir *.csv |
ForEach {
$sheetName=$_.Name.Split('.')[0]
Import-Csv $_.FullName |
Export-Excel $xlFileName -WorkSheetname $sheetName
}
Invoke-Item $xlFileName