Lync Management Shell provides an interface for administrators to easily manage Lync server. In different circumstances, even simplest job becomes an unavoidable hurdle. For example, creating Lync simple URL (e.g. https://lync.mydomain/SipDomain/Meet ) using management shell is quite simple by executing the following commands:
$urlEntry = New-CsSimpleUrlEntry -Url "https://lync.MyDomain/SipDomain/Meet"
$simpleUrl = New-CsSimpleUrl -Component "Meet" -Domain "SIPDomain" -SimpleUrl $urlEntry -ActiveUrl "https://lync.MyDomain/SipDomain/Meet"
Set-CsSimpleUrlConfiguration -Identity Global -SimpleUrl @{Add=$simpleUrl}
Enable-CSComputer
Above commands once executed will create simple URL but this easy task becomes quite difficult if you had to do it remotely (for any purpose like an automation solution for Lync management). Inside a vb.net code above shell cmdlets were called. But upon execution below error came: Assignment statements are not allowed in restricted language mode or a Data section.
Few solutions to bypass above error were tried like setting execution policy to unrestricted , using admin credentials for remote session etc. , but all in vain.
<
p style=”text-align: justify;”>However, another approach was used to get this task done by saving these cmdlets in a ps1 file and executing the file. Following is the code snippet that does this:
Try
Proc.StartInfo.FileName = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Dim ScriptFile As String = “C:\Script.ps1”
Dim PSCommand As String = "Import-Module Lync" & vbCrLf
PSCommand &= Command
File.AppendAllText(ScriptFile, PSCommand)
Proc.StartInfo.Domain = DomainNETBIOS
Proc.StartInfo.UserName = ADUserName
Proc.StartInfo.LoadUserProfile = True
Dim Pass As New System.Security.SecureString
For Each character As Char In ADPassword
Pass.AppendChar(character)
Next
Proc.StartInfo.Password = Pass
Proc.StartInfo.UseShellExecute = False
Proc.StartInfo.RedirectStandardError = True
Proc.StartInfo.RedirectStandardOutput = True
Proc.StartInfo.Arguments = " -ExecutionPolicy Unrestricted -Command &'" & ScriptFile & "'"
Proc.Start()
Proc.WaitForExit()
If Not Proc.StandardError Is Nothing Then
Dim ErrorMsg As String = Proc.StandardError.ReadToEnd
If ErrorMsg <> "" Then
Throw New Exception(ErrorMsg)
End If
End If
Proc = Nothing
File.Delete(ScriptFile)
Catch ex As Exception
Throw New Exception("Error processing PS command: " & ex.Message)
End Try
Often you will find that new approach to a problem is better than focusing all efforts in one direction