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

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

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.

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

Thanks,
Tim Curwick

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.

I’d really like to use Universal Dashboard to query some mailbox data from our Exchange servers, but I haven’t found a way yet to create and import a PSSession to an Exchange server.

Has anyone been able to create a PSSession connection to an Exchange server and have that session accessible in all of the runspaces that the endpoints run in?

@jdotto02 -
Get some credentials either via login or username and password inputs on a page.

In my experience the imported session is only available in the endpoint that you place this code in. I believe that’s the nature of the endpoints, unless you could push it into some global variable set.

$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Session:Creds.UserName, $Session:Creds.Password
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServer.Your.Org/PowerShell/ -Credential $Creds
Import-PSSession $ExSession

This is working for me, and I’m using it to provision new active directory accounts and exchange mailboxes on our network.

Kirk.

I’m using this exact same Forms Login process and would like to be able to access the Username throughout my dashboards for logging purposes. But just declaring the session variable within the login page doesn’t seem to render the variable usable within any other pages on my dashboard. Here’s the end of my login page

...
    if ((Test-Credential -Credential $Credentials)) {
        New-UDAuthenticationResult -Success -UserName $Credentials.UserName -Role $role
        $Session:Creds = $Credentials.Username
    }
    New-UDAuthenticationResult -ErrorMessage "Invalid Credentials, please try again."
}

New-UDLoginPage -AuthenticationMethod $FormLogin 

I’m not saving the password as I only need the Username for logging purposes.

As a test I’ve just built a “homepage” to say welcome user.

New-UDPage -Name "HomePage" -AuthorizedRole "All" -Icon user -Content{
New-UDCard @Colors -Title "Welcome, $Session:Creds" -Text "Test."
}

But I’ve yet to get the variable to pass to this page. They are seperate ps1 files being loaded by the dashboard. Could that be the issue I’m running into? If so, any ideas how I can store and use the current user variable within my Dashboard with this login process?