Access $Credentials from login Authentication -or import Microsoft.Exchange PSSession


#1

I’m trying to import a PSSession for Microsoft.Exchange. I need to pass the $Credentials from the New-UDAthenticationMethod.

I’ve been unable to access them from the page just via $Credentials variable.
Here’s the authentication setup:

    param([PSCredential]$Credentials)
    Function Test-Credential {
        [OutputType([Bool])]
        
        Param (
            [Parameter(
                Mandatory = $true,
                ValueFromPipeLine = $true,
                ValueFromPipelineByPropertyName = $true
            )]
            [Alias(
                'PSCredential'
            )]
            [ValidateNotNull()]
            [System.Management.Automation.PSCredential]
            [System.Management.Automation.Credential()]
            $Credential,
    
            [Parameter()]
            [String]
            $Domain = $Credential.GetNetworkCredential().Domain
        )
    
        Begin {
            [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") |
                Out-Null
    
            $principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext(
                [System.DirectoryServices.AccountManagement.ContextType]::Domain, $Domain
            )
        }
    
        Process {
            foreach ($item in $Credential) {
                $networkCredential = $Credential.GetNetworkCredential()
                
                Write-Output -InputObject $(
                    $principalContext.ValidateCredentials(
                        $networkCredential.UserName, $networkCredential.Password
                    )
                )
            }
        }
        End {
            $principalContext.Dispose()
        }
    }
    $adgroupname = 'FileServer_Admin'
    if ((Test-Credential -Credential $Credentials)) {
        if(Get-ADGroupMember -Identity $adgroupname | Where-Object{$_.Name -eq $Credentials.UserName}){
            New-UDAuthenticationResult -Success -UserName $Credentials.UserName
$Session:Creds = $Credentials
        } else { New-UDAuthenticationResult -ErrorMessage "Only members of $adgroupname have access."}
    }
    New-UDAuthenticationResult -ErrorMessage "Invalid Credentials, please try again."
} #end FormLogin

#2

I’ve also headed down the path of defining my PS exchange import as a function:

function Connect-Exchange 
{
    $Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Session:Creds.UserName, $Session:Creds.Password
    $ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://MyExchange.internal.org/PowerShell/ -Credential $Creds
    Import-PSSession $ExSession
}

And then New-UDEndpointInitialization:

$InitExchange = New-UDEndpointInitialization -Function Connect-Exchange -Debug
$MyDashboard= New-UDDashboard -Title "New User Provisioning" -EndpointInitialization $InitExchange .....

Still no luck getting the Exchange commands to load.


#3

Stupid question… You are actually calling the function somewhere, right?

Thanks,
Tim Curwick


#4

We have the support with the enterprise license. Via email, Adam was able to determine my

$Session:Creds = $Credentials 

wasn’t being saved to the session variable array.
2.4.0 has a fix inline for it.

I’ll be posting this entire mess up to the marketplace as a free sample in a few more days.