PSSession Issue

I have a Powershell GUI program I am writing but having issues with PSSessions for some reason. When the program opens, the function getCredentials fires off.

function getCredentials
	{
		$global:rrCredential = Get-Credential -Credential "domain.local\"
		$global:rrSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.domain.local/PowerShell/ -Authentication Kerberos -Credential $rrCredential -ErrorAction SilentlyContinue
	}

Then later off when we go to create the new user in AD and import the PSSsession with the following but get errors

function createUser
{ 
.... lots of other code....

Import-PSSession $rrSession -AllowClobber -DisableNameChecking 
}

Below is the errors I get

Import-PSSession : Cannot bind parameter 'Session'. Cannot convert the "" value of type "System.String" to type "System.Management.Automation.Runspaces.PSSession".
At Z:\End User Support\Scripts\guiToolkit\eusgToolKitbeta.ps1:557 char:24
+             Import-PSSession $rrSession -AllowClobber -Di ...
+                              ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Import-PSSession], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportPSSessionCommand

This error I don’t believe is a result of the one above but just copying just in case…

Couldn't find the Enterprise Organization container.
At Z:\End User Support\Scripts\guiToolkit\eusgToolKitbeta.ps1:1282 char:6
+     if (!(Get-Recipient $emailAddress -ResultSize Unlimited -ErrorAct ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], OrgContainerNotFoundException
    + FullyQualifiedErrorId : 38824D84

Ah, global variables.

My guess is that your variable is going out of scope somehow. Or getting overwritten. This is one of the many fun reasons that global variables are a dangerous idea. Especially in a GUI - it’s really impossible, from this end, to track what might be happening.

Try setting a breakpoint on that variable and running it. See if you can see where it’s being touched.