Home > Mobile >  how can i change the event while closing my powershell gui?
how can i change the event while closing my powershell gui?

Time:02-18

I'm currently working on a powershell gui and i wanted to add a command to the closing event. While starting the gui, i'm loading a session and i want to close it when leaving.

here is the part of my script:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyle()


$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '1300,700'
$Form.Text = "Test close event"
$Form.Add_Closing({
     Param($sender,$e)
     $result = [System.Windows.Forms.MessageBox]::show(`
                                                       "wish to quit?",`
                                                       "quit",[System.Windows.Forms.MessageBoxButtons]::YesNoCancel)
                                                   If($result -ne [System.Windows.Forms.DialogResult]::Yes)
                                                     {
                                                      Get-PSSession |Remove-PSSession
                                                      $e.Cancel = $true
                                                      }    

                 })

$session = New-PSSession -ConfigurationName Microsoft.exchange -URI "server" -Authentication Kerberos
Import-PSSession $session | Out-Null 

... Rest of the script ...

$Form.ShowDialog()  

The rest work fine but this part seems to pose problem. If someone has an idea i'm interested.

CodePudding user response:

If I understood correctly, your form on closing displays the "wish to quit?" question successfully, then it successfully executes Get-PSSession | Remove-PSSession. But once all finished you see your Exchange connection is still active.

If that's the case, the following approach should work - use "script:" scope for your $session variable.

code fragments:

$script:session = New-PSSession -ConfigurationName Microsoft.exchange -URI "server" -Authentication Kerberos

Import-PSSession $script:session | Out-Null

Remove-PSSession $script:session

Full code:


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyle()


$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '1300,700'
$Form.Text = "Test close event"
$Form.Add_Closing({
     Param($sender,$e)
     $result = [System.Windows.Forms.MessageBox]::show(`
                                                       "wish to quit?",`
                                                       "quit",[System.Windows.Forms.MessageBoxButtons]::YesNoCancel)
                                                   If($result -ne [System.Windows.Forms.DialogResult]::Yes)
                                                     {
                                                      Remove-PSSession $script:session
                                                      $e.Cancel = $true
                                                      }    

                 })

$script:session = New-PSSession -ConfigurationName Microsoft.exchange -URI "server" -Authentication Kerberos
Import-PSSession $script:session | Out-Null 

... Rest of the script ...

$Form.ShowDialog()  

Note Since we are referencing session variable using "script:" prefix, you shall save your code in *.ps1 file and run it as a script, e.g. PS> C:\scripts\myscript.ps1

Reference: about_Scopes

  • Related