Creating a Yes/No MessageBox in a NuGet install/uninstall script

Posted by ParadigmShift on Geeks with Blogs See other posts from Geeks with Blogs or by ParadigmShift
Published on Mon, 19 Nov 2012 17:59:52 GMT Indexed on 2012/11/19 23:03 UTC
Read the original article Hit count: 226

Filed under:

Sometimes getting a little feedback during the install/uninstall process of a NuGet package could be really useful. Instead of accounting for all possible ways to install your NuGet package for every user, you can simplify the installation by clarifying with the user what they want.

This example shows how to generate a windows yes/no message box to get input from the user in the PowerShell install or uninstall script.

We’ll use the prompt on the uninstall to confirm if the user wants to delete a custom setting that the initial install placed in their configuration.  Obviously you could use the prompt in any way you want.


The objects of the message box are generated similar to the controls in the code behind of a WinForm.

At the beginning of your script enter this:

param($installPath, $toolsPath, $package, $project)
 
# Set up path variables
$solutionDir = Get-SolutionDir
$projectName = (Get-Project).ProjectName
$projectPath = Join-Path $solutionDir $projectName
 
################################################################################################
# WinForm generation for prompt
################################################################################################
function Ask-Delete-Custom-Settings {
    [void][reflection.assembly]::loadwithpartialname("System.Windows.Forms") 
    [Void][reflection.assembly]::loadwithpartialname("System.Drawing")
 
    $title = "Package Uninstall"
    $message = "Delete the customized settings?"
        
    #Create form and controls
    $form1 = New-Object System.Windows.Forms.Form
    $label1 = New-Object System.Windows.Forms.Label
    $btnYes = New-Object System.Windows.Forms.Button
    $btnNo = New-Object System.Windows.Forms.Button
 
    #Set properties of controls and form
    ############
    #  label1  #
    ############
    $label1.Location = New-Object System.Drawing.Point(12,9)
    $label1.Name = "label1"
    $label1.Size = New-Object System.Drawing.Size(254,17)
    $label1.TabIndex = 0
    $label1.Text = $message
 
    #############
    #   btnYes  #
    #############
    $btnYes.Location = New-Object System.Drawing.Point(156,45)
    $btnYes.Name = "btnYes"
    $btnYes.Size = New-Object System.Drawing.Size(48,25)
    $btnYes.TabIndex = 1
    $btnYes.Text = "Yes"
 
    ###########
    #  btnNo  #
    ###########
    $btnNo.Location = New-Object System.Drawing.Point(210,45)
    $btnNo.Name = "btnNo"
    $btnNo.Size = New-Object System.Drawing.Size(48,25)
    $btnNo.TabIndex = 2
    $btnNo.Text = "No"
 
    ###########
    #  form1  #
    ###########
    $form1.ClientSize = New-Object System.Drawing.Size(281,86)
    $form1.Controls.Add($label1)
    $form1.Controls.Add($btnYes)
    $form1.Controls.Add($btnNo)    
    $form1.Name = "Form1"
    $form1.Text = $title
    
    #Event Handler
    $btnYes.add_Click({btnYes_Click})
    $btnNo.add_Click({btnNo_Click})
    
    return $form1.ShowDialog()
}
function btnYes_Click
{
    #6 = Yes
    $form1.DialogResult = 6
}
function btnNo_Click
{    
    #7 = No
    $form1.DialogResult = 7
}
################################################################################################

This has also wired up the click events to the form.  This is all it takes to create the message box.


Now we have to actually use the message box and get the user’s response or this is all pointless.  We’ll then delete the section of the application/web configuration called <Custom.Settings>

    [xml] $configXmlContent = Get-Content $configFile
 
    Write-Host "Please respond to the question in the Dialog Box."
    $dialogResult = Ask-Delete-Custom-Settings
    #6 = Yes
    #7 = No
    Write-Host "dialogResult = $dialogResult"
    if ($dialogResult.ToString() -eq "Yes")
    {
        Write-Host "Deleting customized settings"
        $customSettingsNode = $configXmlContent.configuration.Item("Custom.Settings")
        $configXmlContent.configuration.RemoveChild($customSettingsNode)
        $configXmlContent.Save($configFile)
    }
    if ($dialogResult.ToString() -eq "No")
    {
        Write-Host "Do not delete customized settings"
    }

The part where I check if ($dialog.Result.ToString() –eq “Yes”) could just as easily check the value for either 6 or 7 (Yes or No).  I just personally decided I liked this way better.

 


Shahzad Qureshi is a Software Engineer and Consultant in Salt Lake City, Utah, USA

His certifications include:

Microsoft Certified System Engineer
3CX Certified Partner
Global Information Assurance Certification – Secure Software Programmer – .NET

He is the owner of Utah VoIP Store at http://www.utahvoipstore.com/ and SWS Development at http://www.swsdev.com/ and publishes windows apps under the name Blue Voice.

© Geeks with Blogs or respective owner