Jump to content

Recommended Posts

Posted

Hello all,

 

I am currently finalising a script to allow self service password resets via text message (based on the script here )

 

the final thing I want to sort is that phone numbers may appear as +447123456789 or as 07123456789 when they come in. I can strip off the unneccessary + but I want to convert the 447 to 07.

However I only want to convert it if its the first characters - obviously if there is a 447 elsewhere in the number I need to preserve it e.g. 447123447789 should become 07123447789 NOT 0712307789!

 

any suggestions / code snippits?

 

Cheers

Posted

Assuming you've got the phone number in a variable then something like this should get you started:

 

 
If ($phone -match "+44") {
$phone.replace("+44","0")
}

 

I'd double check how to use replace, posting from phone and can't test myself. Should only replace instances of +44 with 0.

  • Thanks 1
Posted

The script is linked in my initial post, but here is the current version:

 

#Configuration Block
$SmtpServer = "mail.mydomain.local"
$ResetEmail = "[email protected]"
$Username = "passwordreset"
$Password = "Password"
$Domain = "mydomain.local"
$MailServer = "https://mail.mydomain.local/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"
 $bChars = "2", "3", "4", "5", "6", "7", "8", "9"
 $intUpperLimit = Get-Random -minimum 6 -maximum 8

 $x = 0
 $strString = ""
 while ($x -lt $intUpperLimit)
 {
    $a = Get-Random -minimum 0 -maximum $aChars.getupperbound(0)
    $strString += $aChars[$a]
    $x += 1
 }
    $b = Get-Random -minimum 0 -maximum $bChars.getupperbound(0)
    $strString += $bChars[$b]

    $b = Get-Random -minimum 0 -maximum $bChars.getupperbound(0)
    $strString += $bChars[$b]
    
 return $strString
}
$email = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
$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)
{
Add-PSSnapin Quest.ActiveRoles.ADManagement
$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.subject
  $Phone = $phone = $item.subject -replace '\D+(\d+)] ','$1'
  $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 "$phone" -body "$Phone 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 "$phone" -body "Your phone number was not found." -SmtpServer $SmtpServer
  }
 }
 $item.Isread = $true
 $item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite) 
}
}

  • 3 weeks later...
Posted (edited)

Don't know if you found a solution to this but since nobody mentioned it.. use ^

 

In regex the caret resembles 'The beginning of a string' (also NOT, depending on how it's used)

 

$phone = $phone -replace ('^44', '0') will replace only the starting 44 if a user enters 447123456789 instead of +447123456789 (Annoyingly, $Phone.Replace('^....) doesn't work.

$phone = $phone -replace ('^\+44', '0') will also only replace the starting +44 with 0 (though why it would crop up anywhere else in the number is beyond me..)

Edited by Garacesh
Posted

I got it working using a varient of what halbaradkenafin posted:

 

$Phone = $item.subject

$phone = $phone -replace("\+44","0")

$Phone = $phone -replace '\D+(\d+)] ','$1'

 

replacing the +44 before stripping out the rest of the text in the string.

 

But thanks for the pointer.

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...