Forum Discussion
Powershell command to send emails/attachments to shared drive
Hi PJTharpe,
To automatically save fax attachments from a shared mailbox to a shared network location using PowerShell, you can use the following steps:
- Create a new PowerShell script.
- Add the following code to the script:
# Get the shared mailbox email address.
$mailboxAddress = "removed for privacy reasons"
# Get the shared network location.
$networkLocation = "\\server\faxes"
# Create a new MailMessage object.
$message = new-object Net.Mail.MailMessage
# Subscribe to the MessageReceived event.
$message.MessageReceived += {
# Get the attachments from the email message.
$attachments = $message.Attachments
# Save each attachment to the shared network location.
foreach ($attachment in $attachments) {
$filePath = $networkLocation + "\" + $attachment.FileName
$attachment.SaveAsFile($filePath)
}
}
# Start listening for new email messages.
$message.StartListening()
- Save the script as a .ps1 file.
- Create a scheduled task to run the script on a regular basis. For example, you could run the script every minute.
Once the script is running, it will automatically save all fax attachments from the shared mailbox to the shared network location.
Here is an example of how to create a scheduled task to run the script every minute:
- Open the Task Scheduler.
- Click on "Create Task".
- Give the task a name and description.
- Select "Run whether user is logged on or not".
- Select "Windows Vista, Windows 7, Windows 8, Windows 10, or Windows 11".
- Click on the "Triggers" tab.
- Click on "New".
- Select "On a schedule".
- Select "Daily".
- Select "Every 1 minute".
- Click on the "Actions" tab.
- Click on "New".
- Select "Start a program".
- In the "Program/script" box, browse to the PowerShell script file that you created.
- Click on "OK".
- Click on the "OK" button to create the scheduled task.
The scheduled task will now run the PowerShell script every minute, and the script will automatically save all fax attachments from the shared mailbox to the shared network location.
there are a few things to keep in mind:
- You need to make sure that the PowerShell script has the necessary permissions to access the shared mailbox and the shared network location.
- You need to make sure that the scheduled task is running with the correct permissions.
- You need to make sure that the shared network location has enough disk space to store the fax attachments.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
LeonPavesic Awesome! Thanks so much! Will this require a workstation or server with Outlook installed?
- LeonPavesicSep 21, 2023Silver Contributor
Hi PJTharpe,
Thanks for your Update.
You do not need a workstation or server with Outlook installed to run the PowerShell script I provided. The script does not use Outlook in any way.The script uses the System.Net.Mail namespace to connect to the shared mailbox and to download the fax attachments. The System.Net.Mail namespace is built into PowerShell, so you do not need to install any additional software to run the script.
You can run the script on any computer that has PowerShell installed. This includes workstations, servers, and even virtual machines.
Once the script is running, it will automatically save all fax attachments from the shared mailbox to the shared network location. You do not need to have Outlook installed on the computer where the script is running.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- PJTharpeSep 25, 2023Copper ContributorI received an error upon running this script:
New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is
denied. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\Users\bct\Desktop\fax-script.ps1:7 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
Import-PSSession : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try
running the command again.
At C:\Users\bct\Desktop\fax-script.ps1:8 char:18
+ Import-PSSession $Session -AllowClobber
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportPSSessionCommand
New-InboxRule : The term 'New-InboxRule' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\bct\Desktop\fax-script.ps1:11 char:1
+ New-InboxRule -Name "Forward to Shared Location" -Mailbox "bct@mvfpi. ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-InboxRule:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-PSSession : Cannot validate argument on parameter 'Id'. The argument is null. Provide a valid value for the argument, and then try running
the command again.
At C:\Users\bct\Desktop\fax-script.ps1:14 char:18
+ Remove-PSSession $Session
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-PSSession], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.RemovePSSessionCommand- LeonPavesicSep 26, 2023Silver Contributor
Hi PJTharpe,
thanks for the update.The error message indicates that you are having trouble connecting to your Exchange Online account. There are a few things you can try:
- Make sure that you are using the correct credentials.
- Make sure that you are using the correct Exchange Online server URL.
- Make sure that you have the necessary permissions to connect to Exchange Online.
Here are some additional troubleshooting tips:
- Try running the script in an elevated PowerShell console.
- Try disabling any antivirus or firewall software that you are using.
- Try running the script from a different network.
Once you are able to connect to Exchange Online, you should be able to run the script without any errors.
Here are some additional tips for running the script:
- Make sure that the shared network location exists and that you have write permissions to the shared folder.
- Make sure that the PowerShell script has the necessary permissions to access the shared mailbox and the shared network location.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic