Sending an email using PowerShell

PowerShell is proving to be a wonderful tool for Microsoft System Admins. The ability to send emails in a PowerShell script can be quite useful, you can use this simple little function to send an email from PowerShell.

You could potentially use this function to send you an email when an automatic PowerShell task runs and fails or if you want to notify yourself of something during the script.

The results of my test are below the PowerShell code.
Function SendMail{
#Set your outgoing SMTP Server, this will work as long as your server does not need authentication
$MailServer = "mail.bigpond.com" #Change this to our SMTP server
#Creating a Mail object
$MailObject = new-object Net.Mail.MailMessage
#Creating SMTP server object
$SMTP = new-object Net.Mail.SMTPClient($MailServer)
#Actual email text
$MailObject.From = "[email protected]"
$MailObject.ReplyTo = "[email protected]"
$MailObject.To.Add("[email protected]")
$MailObject.subject = "This is the test email"
$MailObject.body = "Hello," + "`r`n" + `
"Powershell is sending you an email"
#Send the email
$SMTP.Send($MailObject)
}

send email powershell script

Email received from my test
Email received from my test sending with PowerShell, it got filtered into spam but it worked!!
Read More →
Replies: 0 / Share:

How to set Office 365 password to never expire

First you need to make sure you have the Azure AD Module installed on the computer.
If you do not have the the Azure AD Module installed you need to:
Open PowerShell as administrator
Run the command
(get-item C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll).VersionInfo.FileVersion

Then you need to run the following commands in powershell:
$msolcred = get-credential
connect-msolservice -credential $msolcred
Get-MSOLUser | Set-MsolUser -PasswordNeverExpires $true

You can check if this has worked by running the following command in powershell
Get-MSOLUser | Select UserPrincipalName, PasswordNeverExpires

Read More →
Replies: 0 / Share:

Resolve VSS errors without rebooting your server

Usually when you have a VSS error you will need to reboot your server or open the services.msc console and restart the service that is associated with it. I know I have had to do this a number of times with Exchange services on Small Business Servers to resolve backup issues.

This is a task that can be automated with this PowerShell script.

What the script does is it checks the status of each VSS Writer(which can be manually done in the command prompt by using the command “VSSAdmin list writers“) and then restart the service that is associated with it.

You can also set this to run as a scheduled task maybe in the middle of the night or once a backup has finished.

$ServiceArray = @{
'ASR Writer' = 'VSS';
'Bits Writer' = 'BITS';
'Certificate Authority' = 'EventSystem';
'COM+ REGDB Writer' = 'VSS';
'DFS Replication service writer' = 'DFSR';
'Dhcp Jet Writer' = 'DHCPServer';
'FRS Writer' = 'NtFrs'
'IIS Config Writer' = 'AppHostSvc';
'IIS Metabase Writer' = 'IISADMIN';
'Microsoft Exchange Writer' = 'MSExchangeIS';
'Microsoft Hyper-V VSS Writer' = 'vmms';
'MS Search Service Writer' = 'EventSystem';
'NPS VSS Writer' = 'EventSystem';
'NTDS' = 'EventSystem';
'OSearch VSS Writer' = 'OSearch';
'OSearch14 VSS Writer' = 'OSearch14';
'Registry Writer' = 'VSS';
'Shadow Copy Optimization Writer' = 'VSS';
'Sharepoint Services Writer' = 'SPWriter';
'SPSearch VSS Writer' = 'SPSearch';
'SPSearch4 VSS Writer' = 'SPSearch4';
'SqlServerWriter' = 'SQLWriter';
'System Writer' = 'CryptSvc';
'WMI Writer' = 'Winmgmt';
'TermServLicensing' = 'TermServLicensing';
}
vssadmin list writers | Select-String -Context 0,4 'writer name:' | ? {$_.Context.PostContext[3].Trim() -ne "last error: no error"} | Select Line | %
{$_.Line.tostring().Split("'")[1]}| ForEach-Object {Restart-Service $ServiceArray.Item($_) -Force}

Read More →
Replies: 4 / Share:

How to recover folder from Public Folder on Office 365/Exchange 2013

A lot of the documentation for recovering folders from exchange says that you can do it straight from outlook by selecting the recover deleted items icon Outlook.

In theory this is fine but in practice your exchange users do not have permission to do this and they will get an error saying:

Outlook was unable to recover some or all of the items in this folder. Make sure you have the required permissions to recover items in this folder and try again. If the problem persists contact your administrator.

Outlook was unable to recover some or all of the items in this folder. Make sure you have the required permissions to recover items in this folder and try again. If the problem persists contact your administrator.

Your public folder that has been deleted will still be held by exchange because exchange has a retention policy so when a folder is deleted it is still held in the exchange retention folder.

Recover Public Folders using Powershell

To restore the public folder you will need to open an exchange PowerShell if you are using Office 365 follow the steps in our post how to connect to office 365 using powers shell to connect to Office 365 using PowerShell.

Then you need to list all of the public folders and output to CSV by running the command(you will need to set the output of Export-Csv to the folderpath of the file you want to output):

Get-PublicFolder -Identity "\NON_IPM_SUBTREE" -Recurse | Select Identity | Export-Csv c:\outfolder\out.csv

Then open up the csv file and find the folders you would like to restore then go back to powershell and you will need to run the following command replacing the *VALUE* the the value from the CSV file.

Set-PublicFolder -Identity "*VALUE*" -Path "\" -Verbose
Eg:
Set-PublicFolder -Identity "\NON_IPM_SUBTREE\DUMPSTER_ROOT\DUMPSTER_EXTEND\RESERVED_1\RESERVED_1\a33b5631-a4a8-432f- 9349-80aa14eadbf8\2015\General Correspondence" -Path "\" -Verbose

You might also like this article about how to recover a public folder database which will allow you to recover the public folders after the retention period on an on premis installation of Exchange Server.

Read More →
Replies: 2 / Share:

How to connect to Office 365 Exchange using Powershell

I use these commands to connect to Exchange Online in Office365.

To do this you need to open powershell and run these command:

$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange-ConnectionUri https://ps.outlook.com/powershell -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session

After the first line it will pop up and ask you for credentials for Office 365. You will need to use your administrator email and password.

 

get credential for exchange online 365
get credential for exchange online 365
Read More →
Replies: 0 / Share: