首页 > 解决方案 > Powershell shell window runs even if am closing the GUI form

问题描述

I have a basic question regarding powershell, I have created a script for giving permissions on public folder and I have used GUI form as well.

The problem is that, if I am closing the gui form using corner X, the shell window still runs and searching for mailbox.

Kindly help me to modify it so that when I close the form, form and shell both get closed.

[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"
}

标签: powershell

解决方案


Change the code for the button click event handlers to

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

and

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

For neatness, also add these lines:

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

Next, capture the result of the dialog (whatever action closes the dialog) so you can test what happened with:

$result = $objForm.ShowDialog()

Do the rest of the code only if the dialog was exited using the OK button:

if ($result -eq 'OK') {
    # your code to search for the mailbox goes here
}

# Important! Dispose of the form when done
$objForm.Dispose()

P.S. Better use

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

because the LoadWithPartialName method is declared obsolete.


To explain better, here your complete code with the above fixes. Since I can obviously not check any of the code that searches the mailbox and adds permissions to the public folder, I have added this commented out between <# and #>

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()

推荐阅读