Hello,
I am working on a project in my workplace to try and cut down the amount of password resets we do. The theory is that a user sends a message to
[email protected] and then it sends them a reset link back and then they can log in. To do this I have a script that will run on the Server against the reset mailbox. The script now runs without any errors in the Shell just the system never knows the mobile number in question.
Here is the script
#Add Quest Active Directory Management snapinAdd-PSSnapin Quest.ActiveRoles.ADManagement
#Configuration Block
$SmtpServer = "smtp.domain.com"
$ResetEmail = "
[email protected]"
$Username = "reset"
$Password = "Password"
$Domain = "Domain"
$MailServer = "https://server.domain.com/ews/exchange.asmx"
#Download for file is here: http://www.microsoft.com/en-us/download/details.aspx?id=35371
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll")
function Create-RandomString()
{
$aChars = @()
$aChars = "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "C", "b", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9", "_", ";"
$intUpperLimit = Get-Random -minimum 8 -maximum 10
$x = 0
$strString = ""
while ($x -lt $intUpperLimit)
{
$a = Get-Random -minimum 0 -maximum $aChars.getupperbound(0)
$strString += $aChars[$a]
$x += 1
}
return $strString
}
$email = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
$email.Credentials = New-Object Net.NetworkCredential($Username, $Password, $Domain)
$uri=[system.URI] $MailServer
$email.Url = $uri
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($email,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
if ($inbox.UnreadCount -gt 0)
{
$PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
# Set search criteria - unread only
$SearchForUnRead = New-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead, $false)
$items = $inbox.FindItems($SearchForUnRead,10) #return only 10 unread mail items
foreach ($item in $items.Items)
{
# load the property set to allow us to view the body
$item.load($PropertySet)
if($item.Body.text -Like "*")
{
$Phone = $item.From.address
$Phone = $item.From.address.substring(0, $Phone.IndexOf("@"))
$user = get-qaduser -MobilePhone $Phone
If ($user -ne $null)
{
$PW = Create-RandomString
if ($PW.length -gt 6)
{
Set-QADUser -identity $user.samaccountname -UserPassword (ConvertTo-SecureString -AsPlainText $PW -Force)
Unlock-QADUser -identity $user.samaccountname
$PasswordAge = (Get-QADUser $user |select-object PasswordLastSet)
if ($PasswordAge.PasswordLastSet -ge (Get-Date).AddMinutes(-1)){
$Body = "Password reset for " + $user.SamAccountName + "-" + $user.DistinguishedName
send-mailmessage -to $ResetEmail -from $ResetEmail -subject "Password Reset" -body $Body -SmtpServer $SmtpServer
send-mailmessage -to $item.From.address -from $ResetEmail -subject " " -body "Your password is now $PW" -SmtpServer $SmtpServer
}
}
}
else
{
send-mailmessage -to $ResetEmail -from $ResetEmail -subject "Invalid Phone number" -body "Phone number $Phone not found" -SmtpServer $SmtpServer
send-mailmessage -to $item.From.address -from $ResetEmail -subject " " -body "Your phone number was not found." -SmtpServer $SmtpServer
}
}
$item.Isread = $true
$item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
}
}
If anybody has any advice on this then it woudl be much appreciated. I am convinced the number is in the wrong place just I am unsure where line number 51 and 52 - $Phone = $item.From.address is pointing too.
Regards,
Adam