我如何在关闭我的 powershell gui 时更改事件?
how can i change the event while closing my powershell gui?
我目前正在开发 powershell gui,我想在结束事件中添加一个命令。
启动 gui 时,我正在加载一个会话,我想在离开时将其关闭。
这是我的脚本部分:
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()
其余工作正常,但这部分似乎有问题。
如果有人有想法我很感兴趣。
如果我没理解错的话,您关闭时的表格会显示“希望退出?”提问成功,则成功执行Get-PSSession | Remove-PSSession
。但是一旦全部完成,您会看到您的 Exchange 连接仍然有效。
如果是这种情况,以下方法应该有效 - 使用“script:”范围作为 $session 变量。
代码片段:
$script:session = New-PSSession -ConfigurationName Microsoft.exchange -URI "server" -Authentication Kerberos
Import-PSSession $script:session | Out-Null
Remove-PSSession $script:session
完整代码:
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()
注意 由于我们使用“script:”前缀引用会话变量,您应将代码保存在 *.ps1 文件中并 运行 它作为脚本,例如PS> C:\scripts\myscript.ps1
参考:
about_Scopes
我目前正在开发 powershell gui,我想在结束事件中添加一个命令。 启动 gui 时,我正在加载一个会话,我想在离开时将其关闭。
这是我的脚本部分:
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()
其余工作正常,但这部分似乎有问题。 如果有人有想法我很感兴趣。
如果我没理解错的话,您关闭时的表格会显示“希望退出?”提问成功,则成功执行Get-PSSession | Remove-PSSession
。但是一旦全部完成,您会看到您的 Exchange 连接仍然有效。
如果是这种情况,以下方法应该有效 - 使用“script:”范围作为 $session 变量。
代码片段:
$script:session = New-PSSession -ConfigurationName Microsoft.exchange -URI "server" -Authentication Kerberos
Import-PSSession $script:session | Out-Null
Remove-PSSession $script:session
完整代码:
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()
注意 由于我们使用“script:”前缀引用会话变量,您应将代码保存在 *.ps1 文件中并 运行 它作为脚本,例如PS> C:\scripts\myscript.ps1
参考: about_Scopes