Search code examples
winformspowershellpowershell-2.0exchange-serverpowershell-3.0

Store credentials on button


To make it easier for new employees to connect to Exchange via powershell im writting a quick little tools using powershell:winforms.

The tool will just contain a couple of buttons, text field, en informative text so users can manage the Exchange envoirement from one UI.

#1  Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)

#3  Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect' 
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)

As you can see the first 'Set Credentials' button allows for users to enter credentials via the default get-credentials.

The Second button 'Connect to Exchange' starts a new PSSession using the $UserCredentials to login.

The issue i'm facing is that when pressing the connect button i get an error that says the -credential value is Null.

Querying the $UserCredential also gives an empty line.

It seems that altrough the credentials get entered they dont get stored because they are called trough an button function.

I tried looking it up but most of the anwsers are quite vague.

Ful code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Exchange_Manager = New-Object Windows.Forms.Form

#Session Initation 1
#1  Set Credentials Button
    $Set_Credential = New-Object System.Windows.Forms.Button
    $Set_Credential.Top = "5"
    $Set_Credential.Left = "5"
    $Set_Credential.Anchor = "Left,Top"
    $Set_Credential.Text = 'Set Credentials'
    $Set_Credential.add_Click({
         $UserCredential = Get-Credential
    })
    $Exchange_Manager.Controls.Add($Set_Credential)
#2  Current Credentials

#3  Connect to Exchange Online
    $Exchange_Connect = New-Object System.Windows.Forms.Button
    $Exchange_Connect.Top = "5"
    $Exchange_Connect.Left = "150"
    $Exchange_Connect.Anchor ="Left,Top"
    $Exchange_Connect.Text = 'Connect' 
    $Exchange_Connect.add_Click({
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
        Import-PSSession $Session
    })
    $Exchange_Manager.Controls.Add($Exchange_Connect)










#Information
#1  Set Credentials Button
    $Mailboxes = New-Object System.Windows.Forms.Button
    $Mailboxes.Top = "50"
    $Mailboxes.Left = "5"
    $Mailboxes.Anchor = "Left,Top"
    $Mailboxes.Text = 'Get All Mailboxes'
    $Mailboxes.add_Click({
         Get-Mailbox |Select-Object DisplayName, Alias | Sort-Object -Property DisplayName | Out-gridview
    })
    $Exchange_Manager.Controls.Add($Mailboxes)




$Exchange_Manager.ShowDialog()

Solution

  • You need to make $UserCredential a global.

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $Exchange_Manager = New-Object Windows.Forms.Form
    $Exchange_Manager.TopMost = $true
    
    # create a global empty credential object
    $Global:UserCredential = [System.Management.Automation.PSCredential]::Empty
    
    #Session Initation 1
    #1  Set Credentials Button
        $Set_Credential = New-Object System.Windows.Forms.Button
        $Set_Credential.Top = "5"
        $Set_Credential.Left = "5"
        $Set_Credential.Anchor = "Left,Top"
        $Set_Credential.Text = 'Set Credentials'
        $Set_Credential.add_Click({
             $Global:UserCredential = Get-Credential
        })
        $Exchange_Manager.Controls.Add($Set_Credential)
    
    #2  Current Credentials
    
    
    #3  Connect to Exchange Online
        $Exchange_Connect = New-Object System.Windows.Forms.Button
        $Exchange_Connect.Top = "5"
        $Exchange_Connect.Left = "150"
        $Exchange_Connect.Anchor ="Left,Top"
        $Exchange_Connect.Text = 'Connect' 
        $Exchange_Connect.add_Click({
            if ($Global:UserCredential -ne [System.Management.Automation.PSCredential]::Empty) {
                $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$Global:UserCredential" -Authentication Basic -AllowRedirection
                Import-PSSession $Session
            }
        })
        $Exchange_Manager.Controls.Add($Exchange_Connect)
    
    #4  Show the dialog and dispose of it afterwards
        $Exchange_Manager.ShowDialog()
        $Exchange_Manager.Dispose()