Advertisement
Guest User

Set Lync Status from Email

a guest
Jan 27th, 2015
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #VARIABLES AND STUFF!
  2. #Enter the from address to verify against
  3. $fromAddress = "blah@gmail.com"
  4. #Enter your work address - to be used for sending reply notifications and for EWS auto discover
  5. $yourAddress = "blah.blah@work.com"
  6. #Enter the address you want the reply notification/confirmation to be sent to (probably the same as the from address)
  7. $replyAddress = "blah@gmail.com"
  8. #Mail server for sending the reply notficiation/confirmation e-mail
  9. $smtpServer = "smtp.work.com"
  10.  
  11. #Function to set the status
  12. Function SetLyncStatus {
  13.     #Set up input parameters
  14.     Param(
  15.         [string]$statusText,
  16.         [string]$lyncStatus
  17.         )
  18.     #Set up the Lync client object
  19.     $lyncClient = [Microsoft.Lync.Model.LyncClient]::GetClient()
  20.     #Switch statement for different status possibilities, capture result in $statusCode
  21.     [int]$statusCode = Switch ($statusText) {
  22.         Available {"3500"}
  23.         Away {"15500"}
  24.         BRB {"12000"}
  25.         Busy {"6500"}
  26.         DND {"9500"}
  27.         }
  28.     #Set up object to hold the status update information
  29.     $contactInfo = New-Object 'System.Collections.Generic.Dictionary[Microsoft.Lync.Model.PublishableContactInformationType, object]'
  30.     #New Availablity setting
  31.     $contactInfo.Add([Microsoft.Lync.Model.PublishableContactInformationType]::Availability, $statusCode)
  32.     #New Status setting
  33.     $contactInfo.Add([Microsoft.Lync.Model.PublishableContactInformationType]::PersonalNote, $lyncStatus)
  34.     #Setup the method to publish the information
  35.     $updateMethod = $lyncClient.Self.BeginPublishContactInformation($contactInfo, $null, $null)
  36.     #Publish it!
  37.     $lyncClient.Self.EndPublishContactInformation($updateMethod)
  38.     }
  39.  
  40. #Function to retrieve the current status
  41. Function GetLyncStatus {
  42.     #Set up the Lync client object
  43.     $lyncClient = [Microsoft.Lync.Model.LyncClient]::GetClient()
  44.     #Object containing this user's contact information
  45.     $ownContact = $lyncClient.ContactManager.GetContactByUri($lyncClient.Uri)
  46.     #Retrieve the availability code, and cast to that type so that we don't have to transform the integer back to plain text
  47.     $setAvailability = [Microsoft.Lync.Model.ContactAvailability]$ownContact.GetContactInformation("Availability")
  48.     #Do the same but this time retrieve the status
  49.     $setStatus = $ownContact.GetContactInformation("PersonalNote")
  50.     #Send the confirmation e-mail
  51.     Send-MailMessage -To $replyAddress -From $yourAddress -SmtpServer $smtpServer -Subject "Your Lync status is $setAvailability" -Body "Your Lync status is $setAvailability with a note of $setStatus"
  52.     }
  53.  
  54. #Load the assemblies and modules needed
  55. Import-Module "C:\Program Files\Microsoft Office\Office15\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Controls.Dll"
  56. Import-Module "C:\Program Files\Microsoft Office\Office15\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.Dll"
  57. [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll")
  58. #Create the EWS object for connectivity
  59. $EWSObj = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
  60. #Populate e-mail address for auto discover to work
  61. $EWSObj.AutodiscoverUrl($yourAddress)
  62. #Create an object to bind to the inbox
  63. $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($EWSObj,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
  64.  
  65. #Start the loop for the actual doing
  66. Do {
  67.     #Get messages, returning most recent 500 messages that are unread. Seems excessive, but fast enough.
  68.     $updateMessages = $inbox.FindItems(500) | Where-Object {$_.IsRead -eq $False}
  69.     #Load the full content for the previously gathered messages
  70.     $updateMessages | ForEach {$_.Load()}
  71.     #Now let's filter down even further by checking the sender address
  72.     $updateMessages | ForEach {
  73.         If ($_.From.Address -eq $fromAddress) {
  74.             #If sender address is good, mark the message as read
  75.             $_.IsRead = $true
  76.             $_.Update("NeverOverwrite")
  77.             #If statement to check for GetStatus message subject
  78.             If ($_.Subject -eq "GetStatus") {
  79.                 GetLyncStatus
  80.                 }
  81.             #If statement to check for SetStatus message subject
  82.             If ($_.Subject -like "SetStatus*") {
  83.                 #Grab the new status and availability from the subject
  84.                 $splitSubject = $_.Subject.Split("/")
  85.                 $newLyncAvailability = $splitSubject[1]
  86.                 $newLyncStatus = $splitSubject[2]
  87.                 #Set the status
  88.                 SetLyncStatus -statusText $newLyncAvailability -lyncStatus $newLyncStatus
  89.                 #Wait just a few seconds for the status to fully update
  90.                 Start-Sleep -Seconds 7
  91.                 #Run GetLyncStatus to send an e-mail back to confirm
  92.                 GetLyncStatus
  93.                 }
  94.             }
  95.         }
  96.     #Start a sleep of 60 seconds, and let it kick off again
  97.     Start-Sleep -Seconds 60
  98.     } Until ($i -gt 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement