Hi,
I'm in the process of working on a script to perform password generating and notifications. I've hit a couple of hurdles. I have a function called "Send_Email" which takes 2 parameters, if I run this and only supply a single parameter it works fine. As soon as I try 2 parameters, it doesn't work at all - the objection invocation call in the function fails with "New-Object : Cannot find an overload for "EmailMessage" and the argument count: "2". I'm not sure what I'm doing wrong here - maybe an incorrect parameter declaration?
Also, how can I get a new line to appear in my email messages? I've seen some examples of using </br>, but wouldn't that simply display "</br>" in plain text email (if turned on)?
Thanks in advance
Function GeneratePassword()
{
#generates random password of length 8 with 2 non alpha numeric characters
$Assembly = Add-Type -AssemblyName System.Web
$password = [System.Web.Security.Membership]::GeneratePassword(8,2)
return $password
}
function Send_Email ($svc, $passw)
{
#sends out password email to end user
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($svc)
$message.Subject = 'Your password has been reset'
$message.Body = 'Your new password is ' + $passw + "'r'n'r'n You must reset your password on first login"
$message.ToRecipients.Add('destination@fqdn’)
$message.SendAndSaveCopy()
}
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.UseDefaultCredentials=$true
$uri=[system.URI] "https://exchange.fqdn/ews/exchange.asmx"
$service.Url = $uri
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
#get unread emails
$sf = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead,$false)
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(3)
$findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$sf,$view)
""
foreach ($item in $findResults.Items)
{
#load method is needed as otherwise the From Address is not exposed in findresults
$EmailMessage=[Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($service, $item.Id )
$EmailMessage.Load()
"Last Mail From : " + $EmailMessage.Sender.Name
"Last Mail From Address : " + $EmailMessage.Sender.Address
"Subjet : " + $EmailMessage.Subject
"Sent : " + $EmailMessage.DateTimeSent
#Move email to deleted items once processed - can also HardDelete (non recoverable) and SoftDelete (dumpster)
#$EmailMessage.Delete("MoveToDeletedItems")
}
$password1 =GeneratePassword
Send_Email($service, $password1 )
"Number or Unread Messages : " + $inbox.UnreadCount