即使正在关闭GUI窗体,Powershell shell窗口也会运行



我有一个关于powershell的基本问题,我已经创建了一个用于授予公用文件夹权限的脚本,我也使用了GUI表单。

问题是,如果我使用角X关闭gui表单,shell窗口仍然会运行并搜索邮箱。

请帮我修改它,这样当我关闭窗体时,窗体和外壳都会关闭。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
#This creates the form and sets its size and position
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Giving permission on public folder"
$objForm.Size = New-Object System.Drawing.Size(300,515) 
$objForm.StartPosition = "CenterScreen"
# Add event for key press
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{$objForm.Close()}})
#This creates a label for the TextBox1
$pfname = New-Object System.Windows.Forms.Label
$pfname.Location = New-Object System.Drawing.Size(10,20) 
$pfname.Size = New-Object System.Drawing.Size(280,20) 
$pfname.Text = "Enter Public folder email address:"
$objForm.Controls.Add($pfname) 
#This creates the TextBox1
$pfnametextbox = New-Object System.Windows.Forms.TextBox 
$pfnametextbox.Location = New-Object System.Drawing.Size(10,40) 
$pfnametextbox.Size = New-Object System.Drawing.Size(260,20)
$pfnametextbox.TabIndex = 0 
$objForm.Controls.Add($pfnametextbox)
#This creates a label for the TextBox2
$useremail = New-Object System.Windows.Forms.Label
$useremail.Location = New-Object System.Drawing.Size(10,70) 
$useremail.Size = New-Object System.Drawing.Size(280,20) 
$useremail.Text = "Enter email address of the user"
$objForm.Controls.Add($useremail)  
#This creates the TextBox2
$useremailbox = New-Object System.Windows.Forms.TextBox 
$useremailbox.Location = New-Object System.Drawing.Size(10,90) 
$useremailbox.Size = New-Object System.Drawing.Size(260,20)
$useremailbox.TabIndex = 1  
$objForm.Controls.Add($useremailbox)
#This creates a checkbox called Owner
$ownercheckbox = New-Object System.Windows.Forms.Checkbox 
$ownercheckbox.Location = New-Object System.Drawing.Size(10,140) 
$ownercheckbox.Size = New-Object System.Drawing.Size(500,20)
$ownercheckbox.Text = "Owner"
$ownercheckbox.TabIndex = 2
$objForm.Controls.Add($ownercheckbox)
#This creates a checkbox called Editor
$editorCheckbox = New-Object System.Windows.Forms.Checkbox 
$editorCheckbox.Location = New-Object System.Drawing.Size(10,160) 
$editorCheckbox.Size = New-Object System.Drawing.Size(500,20)
$editorCheckbox.Text = "Editor"
$editorCheckbox.TabIndex = 2
$objForm.Controls.Add($editorCheckbox)
#This creates a checkbox Reviewer 
$reviewercheckbox = New-Object System.Windows.Forms.Checkbox 
$reviewercheckbox.Location = New-Object System.Drawing.Size(10,180) 
$reviewercheckbox.Size = New-Object System.Drawing.Size(500,20)
$reviewercheckbox.Text = "Reviewer"
$reviewercheckbox.TabIndex = 3
$objForm.Controls.Add($reviewercheckbox)
#This creates a checkbox called Contributor
$contributorcheckbox = New-Object System.Windows.Forms.Checkbox 
$contributorcheckbox.Location = New-Object System.Drawing.Size(10,200) 
$contributorcheckbox.Size = New-Object System.Drawing.Size(500,20)
$contributorcheckbox.Text = "Contributor"
$contributorcheckbox.TabIndex = 4
$objForm.Controls.Add($contributorcheckbox)
#This creates a checkbox called Publishing Editor 
$publishingeditorcheckbox = New-Object System.Windows.Forms.Checkbox 
$publishingeditorcheckbox.Location = New-Object System.Drawing.Size(10,220) 
$publishingeditorcheckbox.Size = New-Object System.Drawing.Size(500,20)
$publishingeditorcheckbox.Text = "Publishing Editor"
$publishingeditorcheckbox.TabIndex = 5
$objForm.Controls.Add($publishingeditorcheckbox)
#This creates a checkbox called Send on Behalf 
$sendonbehalfcheckbox = New-Object System.Windows.Forms.Checkbox 
$sendonbehalfcheckbox.Location = New-Object System.Drawing.Size(140,240) 
$sendonbehalfcheckbox.Size = New-Object System.Drawing.Size(500,20)
$sendonbehalfcheckbox.Text = "Send on Behalf"
$sendonbehalfcheckbox.TabIndex = 6
$objForm.Controls.Add($sendonbehalfcheckbox)
#This creates a checkbox called Send As 
$sendascheckbox = New-Object System.Windows.Forms.Checkbox 
$sendascheckbox.Location = New-Object System.Drawing.Size(140,260) 
$sendascheckbox.Size = New-Object System.Drawing.Size(500,20)
$sendascheckbox.Text = "Send As"
$sendascheckbox.TabIndex = 7
$objForm.Controls.Add($sendascheckbox)
#This creates the Ok button and sets the event
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(120,440)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$OKButton.TabIndex = 8
$objForm.Controls.Add($OKButton)
#This creates the Cancel button and sets the event
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(195,440)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::CANCEL
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close(), [environment]::exit(0) })
#$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.TabIndex = 9
$objForm.Controls.Add($CancelButton)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$mailbox = $pfnametextbox.Text
$recipient = Get-Recipient $mailbox -ea 0
$name = $mailbox | Get-mailPublicFolder
$EntryID = ($name | Get-mailPublicFolder).EntryID
$PPath = (Get-PublicFolder $EntryID).ParentPath
$Dname = (Get-PublicFolder $EntryID).Name
$Path = $PPath + "" + $Dname
$SendOnBehalf = ((Get-MailPublicFolder $name).grantsendonbehalfto).name
$users = $useremailbox.Text
# $rights = read-host 'please enter the rights type ie, owner, editor,reviewer, contributor, publishing editor. '
If (!($recipient)){
Write-Host "Mailbox doesn not exist" -ForegroundColor Red
break
}
If ($editorCheckbox.Checked = $true){
Add-PublicFolderClientPermission -Identity "$path" -AccessRights Editor -User "$users"
}

将按钮点击事件处理程序的代码更改为

$OKButton.Add_Click({ 
$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.Close()
})

$CancelButton.Add_Click({
$objForm.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Close()
})

为了整洁,还要添加以下行:

$objForm.CancelButton = $CancelButton
$objForm.AcceptButton = $OKButton

接下来,捕获对话框的结果(无论什么操作关闭对话框(,这样您就可以用测试发生了什么

$result = $objForm.ShowDialog()

只有当使用OK按钮退出对话框时,才执行其余代码:

if ($result -eq 'OK') {
# your code to search for the mailbox goes here
}
# Important! Dispose of the form when done
$objForm.Dispose()

p.S.更好地使用

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

因为LoadWithPartialName方法已被声明为过时。


为了更好地解释,这里是您的完整代码和上述修复程序。由于我显然无法检查任何搜索邮箱和向公用文件夹添加权限的代码,所以我在<##>之间添加了这个注释

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#This creates the form and sets its size and position
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Giving permission on public folder"
$objForm.Size = New-Object System.Drawing.Size(300,515) 
$objForm.StartPosition = "CenterScreen"
#This creates a label for the TextBox1
$pfname = New-Object System.Windows.Forms.Label
$pfname.Location = New-Object System.Drawing.Size(10,20) 
$pfname.Size = New-Object System.Drawing.Size(280,20) 
$pfname.Text = "Enter Public folder email address:"
$objForm.Controls.Add($pfname) 
#This creates the TextBox1
$pfnametextbox = New-Object System.Windows.Forms.TextBox 
$pfnametextbox.Location = New-Object System.Drawing.Size(10,40) 
$pfnametextbox.Size = New-Object System.Drawing.Size(260,20)
$pfnametextbox.TabIndex = 0 
$objForm.Controls.Add($pfnametextbox)
#This creates a label for the TextBox2
$useremail = New-Object System.Windows.Forms.Label
$useremail.Location = New-Object System.Drawing.Size(10,70) 
$useremail.Size = New-Object System.Drawing.Size(280,20) 
$useremail.Text = "Enter email address of the user"
$objForm.Controls.Add($useremail)  
#This creates the TextBox2
$useremailbox = New-Object System.Windows.Forms.TextBox 
$useremailbox.Location = New-Object System.Drawing.Size(10,90) 
$useremailbox.Size = New-Object System.Drawing.Size(260,20)
$useremailbox.TabIndex = 1  
$objForm.Controls.Add($useremailbox)
#This creates a checkbox called Owner
$ownercheckbox = New-Object System.Windows.Forms.Checkbox 
$ownercheckbox.Location = New-Object System.Drawing.Size(10,140) 
$ownercheckbox.Size = New-Object System.Drawing.Size(500,20)
$ownercheckbox.Text = "Owner"
$ownercheckbox.TabIndex = 2
$objForm.Controls.Add($ownercheckbox)
#This creates a checkbox called Editor
$editorCheckbox = New-Object System.Windows.Forms.Checkbox 
$editorCheckbox.Location = New-Object System.Drawing.Size(10,160) 
$editorCheckbox.Size = New-Object System.Drawing.Size(500,20)
$editorCheckbox.Text = "Editor"
$editorCheckbox.TabIndex = 2
$objForm.Controls.Add($editorCheckbox)
#This creates a checkbox Reviewer 
$reviewercheckbox = New-Object System.Windows.Forms.Checkbox 
$reviewercheckbox.Location = New-Object System.Drawing.Size(10,180) 
$reviewercheckbox.Size = New-Object System.Drawing.Size(500,20)
$reviewercheckbox.Text = "Reviewer"
$reviewercheckbox.TabIndex = 3
$objForm.Controls.Add($reviewercheckbox)
#This creates a checkbox called Contributor
$contributorcheckbox = New-Object System.Windows.Forms.Checkbox 
$contributorcheckbox.Location = New-Object System.Drawing.Size(10,200) 
$contributorcheckbox.Size = New-Object System.Drawing.Size(500,20)
$contributorcheckbox.Text = "Contributor"
$contributorcheckbox.TabIndex = 4
$objForm.Controls.Add($contributorcheckbox)
#This creates a checkbox called Publishing Editor 
$publishingeditorcheckbox = New-Object System.Windows.Forms.Checkbox 
$publishingeditorcheckbox.Location = New-Object System.Drawing.Size(10,220) 
$publishingeditorcheckbox.Size = New-Object System.Drawing.Size(500,20)
$publishingeditorcheckbox.Text = "Publishing Editor"
$publishingeditorcheckbox.TabIndex = 5
$objForm.Controls.Add($publishingeditorcheckbox)
#This creates a checkbox called Send on Behalf 
$sendonbehalfcheckbox = New-Object System.Windows.Forms.Checkbox 
$sendonbehalfcheckbox.Location = New-Object System.Drawing.Size(140,240) 
$sendonbehalfcheckbox.Size = New-Object System.Drawing.Size(500,20)
$sendonbehalfcheckbox.Text = "Send on Behalf"
$sendonbehalfcheckbox.TabIndex = 6
$objForm.Controls.Add($sendonbehalfcheckbox)
#This creates a checkbox called Send As 
$sendascheckbox = New-Object System.Windows.Forms.Checkbox 
$sendascheckbox.Location = New-Object System.Drawing.Size(140,260) 
$sendascheckbox.Size = New-Object System.Drawing.Size(500,20)
$sendascheckbox.Text = "Send As"
$sendascheckbox.TabIndex = 7
$objForm.Controls.Add($sendascheckbox)
#This creates the Ok button and sets the event
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(120,440)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.TabIndex = 8
$OKButton.Add_Click({ 
# this is what the dialog returns after ShowDialog()
$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.Close()
})
$objForm.Controls.Add($OKButton)
#This creates the Cancel button and sets the event
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(195,440)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::CANCEL
$CancelButton.Text = "Cancel"
$CancelButton.TabIndex = 9
$CancelButton.Add_Click({
# this is what the dialog returns after ShowDialog()
$objForm.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Close()
})
$objForm.Controls.Add($CancelButton)
# if you let the Form know what the Cancel and OK buttons are, 
# there is no more need for doing `$objForm.Add_KeyDown()` and
# pressing Esc automatically closes and Cancels the dialog.
$objForm.CancelButton = $CancelButton
$objForm.AcceptButton = $OKButton
$objForm.Add_Shown({$objForm.Activate()})
# capture the dialog output (what closes the form)
$result = $objForm.ShowDialog()
if ($result -eq 'OK') {
# your code to search for the mailbox goes here
# As I cannot test this myself, I have commented it out
<#
$mailbox = $pfnametextbox.Text
$recipient = Get-Recipient $mailbox -ErrorAction SilentlyContinue
If (!$recipient){
Write-Host "Mailbox doesn not exist" -ForegroundColor Red -ErrorAction SilentlyContinue
break
}
else {
$name = $mailbox | Get-mailPublicFolder
$EntryID = ($name | Get-mailPublicFolder).EntryID
$PPath = (Get-PublicFolder $EntryID).ParentPath
$Dname = (Get-PublicFolder $EntryID).Name
$Path = $PPath + "" + $Dname
$SendOnBehalf = ((Get-MailPublicFolder $name).grantsendonbehalfto).name
$users = $useremailbox.Text
# $rights = read-host 'please enter the rights type ie, owner, editor,reviewer, contributor, publishing editor. '
If ($editorCheckbox.Checked = $true){
Add-PublicFolderClientPermission -Identity "$path" -AccessRights Editor -User "$users"
}
}
#>
}
else {
Write-Host "The dialog was Cancelled"
}
# Important! Dispose of the form when done
$objForm.Dispose()

最新更新