I have a question about dot sourcing in PowerShell workflows. I am currently using a workflow to enable users in Lync Server 2013. I’ve created three functions that get the line uri, voice policy, and dial plan based on the users office attribute in Active Directory. Right now, my script looks like this:
workflow Setup-Lync { Param ( [Parameter(Mandatory=$true)] [string]$UserPrincipalName ) $Credential = Get-AutomationPSCredential -Name "DomainAdmin" $LyncConnectionUri = Get-AutomationVariable -Name "LyncConnectionUri" $ExchangeConnectionUri = Get-AutomationVariable -Name "ExchangeConnectionUri" inlineScript { # Create session to Lync and Exchange $LyncSession = New-PSSession -ConnectionUri $Using:LyncConnectionUri -Credential $Using:Credential $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Using:ExchangeConnectionUri -Credential $Using:Credential # Load only the necessary cmdlets $LyncCmdlets = Import-PSSession $LyncSession -CommandName Enable-CsUser, Get-CsUser, Set-CsUser, Get-CsVoicePolicy, Grant-CsVoicePolicy, Get-CsDialPlan, Grant-CsDialPlan $ExchangeCmdlets = Import-PSSession $ExchangeSession -CommandName Get-User, Set-User, Enable-UMMailbox # Load custom functions . '\\pathto.ps1' # Get users Guid and Office attributes $User = Get-User -Identity $Using:UserPrincipalName | Select-Object DisplayName, Guid, Office # Enable user in Lync Enable-CsUser -Identity $Using:UserPrincipalName -RegistrarPool "registrarpool" -SipAddressType UserPrincipalName $CsUser = Get-CsUser -Identity $Using:UserPrincipalName # Enable enterprice voice Set-CsUser -Identity $CsUser.Identity -EnterpriseVoiceEnabled $True # Set line uri $LineURI = Get-LineURI -Office $User.Office Set-CsUser -Identity $CsUser.Identity -LineURI $LineURI # Set voice policy $VoicePolicy = Get-VoicePolicy -Office $User.Office Grant-CsVoicePolicy $CsUser.Identity -PolicyName $VoicePolicy.Identity # Set dial plan $DialPlan = Get-DialPlan -Office $User.Office if($DialPlan -ne $Null) { Grant-CsDialPlan $CsUser.Identity -PolicyName $DialPlan.Identity } $Phone = $LineURI.TrimStart("tel:") $Extension = $LineURI.Substring($LineURI.Length - 4,4) # Set phone in Active Directory Set-User -Identity $Using:UserPrincipalName -Phone $Phone # Enable unified messaging Enable-UMMailbox -Identity $Using:UserPrincipalName -UMMailboxPolicy "ummailboxpolicy" -SIPResourceIdentifier $Using:UserPrincipalName -Extensions $Extension # Remove session(s) Remove-PSSession $LyncSession, $ExchangeSession } }
This seems to work great but I am not 100% sure this is the best method. Any suggestions?