Working with Powershell Modules and Remote Sessions

load_unload_configurationsI had a request yesterday to see which users in an Active Directory group had a Lync conferencing policy applied. Easy enough request but the hitch was that the group was in a different domain than where the Lync servers are hosted. What I learned during this exercise was how to load and unload PowerShell modules automatically and how to establish a remote PowerShell session.

On my workstation’s PowerShell configuration, the Lync modules automatically load. However, when I set up the remote PowerShell session to a Lync server, I got an error back basically saying that there was a conflict between local commands and remote commands. For example, if I type Get-CSUser, will that run against the local instance of Get-CSUser or the remote instance? I didn’t want to figure out the details of the AllowClobber parameter. So the first thing I needed to do is determine if the local Lync module is loaded and then unload it. This is done with the following code:

if(Get-Module -name "Lync")
{
Remove-Module "Lync"
}

I simply run the Get-Module cmdlet and pass it the name of the module. If the module can be “gotten” then it evaluates to true. If it is true, I then remove that module. If you don’t know the name of the modules you have loaded, you can run  Get-Module -ListAvailable.

The next thing I needed to do was to make sure the ActiveDirectory module was loaded as I was going to use the Get-ADGroupMembers <Group DN> -recursive cmdlet to enumerate all of the users in the AD group. To install the ActiveDirectory module on a Windows PC, follow the steps here. Very similar to above, the simple and straightforward thing to do is to see if the module is NOT loaded. If it is not loaded run the Import-Module command to load it.

if(-not(Get-Module -name ActiveDirectory))
{
Import-Module ActiveDirectory
}

There is a more complete way to import this module which I didn’t use in my script but I feel I should point out. My if statement above assumes that the ActiveDirectory module is actually installed and can be imported. Ideally you would check to see if the module is installed before loading the module.

$name=&quot;ActiveDirectory&quot;
if(-not(Get-Module -name $name))
{
if(Get-Module -ListAvailable | Where-Object { $_.name -eq $name })
{
#module available so import it
Import-Module -Name $name
}
else
{
#module not available
$false
}
} # end if not module
else
{
#Module already loaded
$true
}

Setting up the remote PowerShell session isn’t too complicated. I went with a basic check on the first pass and simply verified if any remote session already exists. If any remote session exists I assume it is for Lync.

$RemoteUsername = &quot;naflinchbot&quot;
$RemoteConnectionURI =  “https://lyncfe01.na.flinchbot.com/OcsPowershell”

if (-not(Get-PsSession))
{
$cred = Get-Credential $RemoteUsername
$session = New-PSSession -ConnectionURI $RemoteConnectionURI -Credential $cred
Import-PsSession $session
}

Ideally you would go more advanced and would run a check to make sure the remote PowerShell Session specifically for Lync doesn’t already exist and that it is in a usable (i.e. “opened”) state. Check this bad boy out:

$RemoteUsername = &quot;naflinchbot&quot;
$RemoteConnectionURI =  “https://lyncfe01.na.flinchbot.com/OcsPowershell”
$PSSessionState = Get-PsSession

If (($PSSessionState.Runspace.ConnectionInfo.ConnectionUri.AbsolutePath -ne &quot;/OcsPowershell&quot;) -and ($PSSessionState.Runspace.RunspaceStateInfo -ne &quot;opened&quot;))
{
$cred = Get-Credential $RemoteUsername
$session = New-PSSession -ConnectionURI $RemoteConnectionURI -Credential $cred
Import-PsSession $session
}

By grabbing the PSSession information and putting it into a variable, I can then check that variable for certain values. I can check to make sure that I am actually connected to a Lync remote PowerShell session by looking for the AbsoluteURI of “OcsPowershell”. If that does NOT exist AND if the session is NOT in an opened stated, I then create the session.

Well there you have it. How to check if modules are loaded and available and how to make sure a session already exists and how to create one.

2 comments

    • Mattias on 2013/08/09 at 08:50
    • Reply

    Hi there!

    I normally run both a Lync and a Exchange PSSession. I made two functions for this. And i needed to do a check if its already started so i dont run multiple sessions against the same server.

    I got the ideas for the solution from here. But i couldn’t get it to work with
    ($PSSessionState.Runspace.RunspaceStateInfo -ne “opened”).

    It would only give a True or False result? Maybe a PS version issue?
    Im running PS version 3.0.

    Im sure i could have done it differently. But I changed it to look for ($PSSessionState.State -eq “opened”) instead. And to look for “-eq AbsolutePath” for Lync and “-eq OriginalString” for Exchange.

    • Mattias on 2013/08/09 at 08:50
    • Reply

    Hi there!

    I normally run both a Lync and a Exchange PSSession. I made two functions for this. And i needed to do a check if its already started so i dont run multiple sessions against the same server.

    I got the ideas for the solution from here. But i couldn’t get it to work with
    ($PSSessionState.Runspace.RunspaceStateInfo -ne “opened”).

    It would only give a True or False result? Maybe a PS version issue?
    Im running PS version 3.0.

    Im sure i could have done it differently. But I changed it to look for ($PSSessionState.State -eq “opened”) instead. And to look for “-eq AbsolutePath” for Lync and “-eq OriginalString” for Exchange.

Leave a Reply to Mattias Cancel reply

Your email address will not be published.