Skip to content

Instantly share code, notes, and snippets.

@vScripter
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vScripter/b5b5252bad85d28f2f62 to your computer and use it in GitHub Desktop.
Save vScripter/b5b5252bad85d28f2f62 to your computer and use it in GitHub Desktop.
# No need for the code on the next line, just put the functions in this script; you run into scoping issues dot sourcing these functions, the way they are written
#. .\func1.ps1
#region functions
function Start-LogLyncAvailability {
[CmdletBinding(
SupportsShouldProcess = $false,
ConfirmImpact = "None",
DefaultParameterSetName = "")]
param (
[Parameter(
Position = 0,
ValueFromPipeLine = $false,
Mandatory = $true,
ValueFromPipelineByPropertyName = $false,
ValueFromRemainingArguments = $false,
HelpMessage = "The email addresses of the contacts to monitor",
ParameterSetName = ""
)]
[string[]]$contacts,
[Parameter(
Position = 1,
ValueFromPipeLine = $false,
Mandatory = $true,
ValueFromPipelineByPropertyName = $false,
ValueFromRemainingArguments = $false,
HelpMessage = "The file to write the log to",
ParameterSetName = ""
)]
[string]$LogFile,
[Parameter(
Position = 2,
ValueFromPipeLine = $false,
Mandatory = $false,
ValueFromPipelineByPropertyName = $false,
ValueFromRemainingArguments = $false,
#HelpMessage="The path to the Lync 2010 SDK dll files",
ParameterSetName = ""
)]
[string]$lyncSDKPath = "C:\Program Files (x86)\Microscoft Lync\SDK\Assemblies\Desktop"
)
# Use Write-Debug, Write-Verbose, Write-Error, and Write-Warning
# Use Write-Output to write objects to the pipeline
BEGIN {
write-debug "In the BEGIN script block."
# setting EA preference here to apply to the function scope since there is non-native powershell code that may not produce terminating errors
$ErrorActionPreference = 'Stop'
} # end BEGIN block
PROCESS {
if ((Get-EventSubscriber | Where-Object { $_.sourceidentifier -like "LogLyncAvailability-*" }).count -gt 0) {
Write-Warning -Message "Currently logging Lync availability. Please run Stop-LogLyncAvailability and then re-run this command."
return
} # end if
$lyncSDKPath = $(Get-ChildItem $lyncSDKPath | Select-Object -first 1).directory.FullName
Write-Verbose -Message 'Importing Lync Module'
try {
$lyncModule = "$lyncSDKPath\Microsoft.Lync.Model.dll"
Import-Module $lyncModule
} catch {
Write-Warning -Message "[Import '$lyncModule'][ERROR] Failed to import the Microsoft.Lync.Model.dll"
Write-Warning -Message "[Import '$lyncModule'][ERROR Message] $_"
return
} # end try/catch
Write-Verbose -Message "Calling '[Microsoft.Lync.Model.LyncClient]::GetClient()' "
try {
$client = [Microsoft.Lync.Model.LyncClient]::GetClient()
} catch {
Write-Warning -Message "[Calling GetClient() Method][ERROR]Error attaching to the Lync client. Probably it is not running and/or not installed."
Write-Warning -Message "[Calling GetClient() Method][ERROR Message] $_"
return
} # end try/catch
$self = $client.Self
Write-Verbose -Message 'Adding content to log file'
Add-Content $Logfile "$((get-date).DateTime) - beginning log of lync availability changes for $contacts."
write-verbose "$((get-date).DateTime) - beginning log of lync availability changes for $contacts."
write-verbose "Logging to $((Get-ChildItem $Logfile).fullname)"
write-verbose "Note that the logging will continue only as long as this powershell session exists."
write-verbose "The 'Stop-LogLyncAvailability' command can be used to stop the lync status logging."
Write-Verbose -Message "Setting 'LyncAvailabilityLogFile' variable"
try {
# creating the $LyncAvailabilityLogFile variable as a read-only variable in the global scope, as it needs to be available to the event action scriptblock
set-Variable -name LyncAvailabilityLogfile -Option readonly -Value "$((Get-ChildItem $logfile).fullname)" -force -scope global
} catch {
Write-Warning -Message "[Setting 'LyncAvailabilityLogFile' variable][ERROR] $_"
} # end try/catch
foreach ($email in $contacts) {
Write-Verbose "Working on '$email'"
try {
$contact = $client.ContactManager.GetContactByUri($email)
$out = Register-ObjectEvent -InputObject $contact `
-EventName "ContactInformationChanged" `
-SourceIdentifier "LogLyncAvailability-$email" `
-action {
$changedinfo = $event.sourceeventargs.changedcontactinformation
if ($changedinfo -contains "Availability") {
Add-Content $LyncAvailabilityLogfile "$((get-date).DateTime) - $(($sender.uri -split ':')[1]) is now $([Microsoft.Lync.Model.ContactAvailability]$sender.getcontactinformation("Availability"))"
} # end if
} # end -action
} catch {
Write-Warning -Message "[Register-Objectevent][$email][ERROR] $_"
} # end try/catch
} # END foreach
} # END process block
END {
write-debug "In the END script block."
} # end END block
} # end function Start-LogLyncAvailability
function Stop-LogLyncAvailability {
[cmdletbinding()]
param ()
PROCESS {
try {
Get-EventSubscriber -ErrorAction 'Stop' |
Where-Object { $_.sourceidentifier -like "LogLyncAvailability-*" } |
Unregister-Event -ErrorAction 'Stop'
} catch {
Write-Warning -Message "[Unregister Event][ERROR] $_"
} # end try/catch
} # end PROCESS block
} # end function Stop-LogLyncAvailability
#endregion functions
$a = Get-Date
$filename = ...
$path = .....
#while (1)
#{
Start-LogLyncAvailability -contacts .....-logfile $path
# Start-Sleep 10
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment