Jump to content

Recommended Posts

Posted

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

Posted

Err which bit isn't it working?

 

As in from how I read that, you're sending an email not SMS to the server.

 

It's then comparing the email address, and looking up the mobile number from AD

 

In regards to $Phone = $item.From.address it's just taking the from-address of (what to me seems more like an email, unless you have somethign that accepts SMS?) and uses that as the lookup variable.

 

Steve

Posted

Thanks for your reply.

 

The latter part is not working. Users smart phones are able to send an sms to the email address and the carrier then converts it to email and it ends up in the Reset email box. This part is fine. The part that is not working is when we run the script against the unread emails in the Reset email box. We end up getting this result;

Phone number +4471234567 not found which suggests to me it is not looking in the right place for the users mobile number. But where is the right place?

 

Hope this is a bit more clear.

 

Regards.

Posted (edited)

I though at much as that is where I have placed the number on our test account. However we are still getting the number not found issue. I have tried the number in +4472345678 4472345678 and 071234567 formats with no avail.

 

Do you have any suggestions as it seems I have implemented everything as it should be?

 

I would really like to get this in place so any help would be great.

Edited by 2011ComputerMan

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...