QuickTip: Remote Lync PowerShell Connections

Here’s another quick tip for connecting to a remote PowerShell for the Lync Server management shell.  If you log in to your workstation as an administrative user, and you have the Lync administration tools installed, you might not need this.  However, if you’re like me and you log in with domain user credentials and keep a separate administrative account, or perhaps you just connect to multiple different environments with different credentials, here’s what you need to know:

$UserCredential =GetCredential

$a =NewPSSession –connectionuri https://lync01.domain.com/ocspowershell -Credential $UserCredential

ImportPSSession $a

Run that in a PowerShell window on your local host, it should prompt you for credentials and allow you to connect to your Lync server to run commands such as enable-csuser.  Go ahead and stuff that in the beginning of a .ps1 script so you can run it from anywhere.

If you want to create a desktop shortcut that you can right-click Run with PowerShell and have it prompt for credentials but leave you at a window where you can simply execute commands, try the following code:

param ( $Show )
if ( !$Show )
{
PowerShell -NoExit -File $MyInvocation.MyCommand.Path 1
return
}

$UserCredential = Get-Credential
$a = New-PSSession -connectionuri https://lync01.domain.com/ocspowershell -Credential $UserCredential
Import-PSSession $a

To give credit where it’s due, the command to start the PowerShell window and keep it open is from Microsoft MVP Jeff Woutershttp://jeffwouters.nl/index.php/2012/12/keep-your-powershell-script-open-when-executed/ (yes, I asked Jeff for permission, he’s a very cool guy like that, I encourage you to follow his blog!)