Send installation/uninstallation Email with PC and User Name

Topics: Archive - Deployment Scripts, Archive - Toolkit Extensions
Mar 17, 2015 at 12:04 PM
Edited Mar 17, 2015 at 12:07 PM
Hi,

I am using this great tool to install my applications and wanted to give back.
Not sure if this has been posted before, I was looking for sending out an Email after having installed an application, i.e. from SCCM.
It will take the appName parameter and pass it on to the toolkit file.

Here are the customized script I created for that:

Deploy-Application.ps1 :
<#
.SYNOPSIS
    This script performs the installation or uninstallation of an application(s).  
.DESCRIPTION
    The script is provided as a template to perform an install or uninstall of an application(s). 
    The script either performs an "Install" deployment type or an "Uninstall" deployment type.
    The install deployment type is broken down in to 3 main sections/phases: Pre-Install, Install, and Post-Install.
    The script dot-sources the AppDeployToolkitMain.ps1 script which contains the logic and functions required to install or uninstall an application.
    To access the help section,
.EXAMPLE
    Deploy-Application.ps1
.EXAMPLE
    Deploy-Application.ps1 -DeploymentMode "Silent"
.EXAMPLE
    Deploy-Application.ps1 -AllowRebootPassThru -AllowDefer
.EXAMPLE
    Deploy-Application.ps1 -Uninstall 
.PARAMETER DeploymentType
    The type of deployment to perform. [Default is "Install"]
.PARAMETER DeployMode
    Specifies whether the installation should be run in Interactive, Silent or NonInteractive mode.
    Interactive = Default mode
    Silent = No dialogs
    NonInteractive = Very silent, i.e. no blocking apps. Noninteractive mode is automatically set if an SCCM task sequence or session 0 is detected.
.PARAMETER AllowRebootPassThru
    Allows the 3010 return code (requires restart) to be passed back to the parent process (e.g. SCCM) if detected from an installation. 
    If 3010 is passed back to SCCM a reboot prompt will be triggered.
.PARAMETER TerminalServerMode
    Changes to user install mode and back to user execute mode for installing/uninstalling applications on Remote Destkop Session Host/Citrix servers
.NOTES
.LINK 
    Http://psappdeploytoolkit.codeplex.com
"#>
Param (
    [ValidateSet("Install","Uninstall")] 
    [string]$DeploymentType = "Install",
    [ValidateSet("Interactive","Silent","NonInteractive")]
    [string]$DeployMode = "Interactive",
    [switch] $AllowRebootPassThru = $false,
    [switch] $TerminalServerMode = $false
)

#*===============================================
#* VARIABLE DECLARATION
Try {
#*===============================================

#*===============================================
# Variables: Application

$appVendor = "Vendor Name"
$appName = "Appname"
$appVersion = "version"
$appArch = "x32"
$appLang = "EN"
$appRevision = "01"
$appScriptVersion = "1.0.0"
$appScriptDate = "17/03/2015"
$appScriptAuthor = "Author"

#*===============================================
# Variables: Script - Do not modify this section

$deployAppScriptFriendlyName = "Deploy Application"
$deployAppScriptVersion = [version]"3.1.2"
$deployAppScriptDate = "17/03/2015"
$deployAppScriptParameters = $psBoundParameters

# Variables: Environment
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
# Dot source the App Deploy Toolkit Functions
."$scriptDirectory\AppDeployToolkit\AppDeployToolkitMain.ps1"

#*===============================================
#* END VARIABLE DECLARATION
#*===============================================

#*===============================================
#* PRE-INSTALLATION
If ($deploymentType -ne "uninstall") { $installPhase = "Pre-Installation"
#*===============================================

    # Prompt the user to close the following applications if they are running:
    Show-InstallationWelcome -CloseApps "blah blah" 
    # Show Progress Message (with the default message)
    Show-InstallationProgress 



#*===============================================
#* INSTALLATION 
#*
#*
#*
$installPhase = "Installation"
#*===============================================




    # Install the patch
#*===============================================
#* Send Uninstallation Email, parameter is passed on to \AppDeployToolkit\AppDeployToolkitExtensions.ps1
    sendinstallationMail($appName) 


#*===============================================
#* POST-INSTALLATION
$installPhase = "Post-Installation"
#*===============================================


#*===============================================
#* UNINSTALLATION
} ElseIf ($deploymentType -eq "uninstall") { $installPhase = "Uninstallation"
#*===============================================

    Show-InstallationProgress -StatusMessage "Uninstalling Application $installTitle. Please Wait..." 
    


Execute-MSI -Action uninstall -Path "blahblah"


#*===============================================
#* Send Uninstallation Email, parameter is passed on to \AppDeployToolkit\AppDeployToolkitExtensions.ps1
    senduninstallationMail($appName)

#*===============================================
#* END SCRIPT BODY
} } Catch {$exceptionMessage = "$($_.Exception.Message) `($($_.ScriptStackTrace)`)"; Write-Log "$exceptionMessage"; Show-DialogBox -Text $exceptionMessage -Icon "Stop"; Exit-Script -ExitCode 1} # Catch any errors in this script 
Exit-Script -ExitCode 0 # Otherwise call the Exit-Script function to perform final cleanup operations
#*===============================================
AppDeployToolkitExtensions.ps1 :
<#
.SYNOPSIS
    This script is a template that allows you to extend the toolkit with your own custom functions.
.DESCRIPTION
    The script is automatically dot-sourced by the appdeploytoolkitmain.ps1 script.
.NOTES
.LINK 
    Http://psappdeploytoolkit.codeplex.com
"#>

#*=============================================
#* VARIABLE DECLARATION
#*=============================================

# Variables: Script
$appDeployExtScriptFriendlyName = "App Deploy Toolkit Extensions"
$appDeployExtScriptVersion = "1.0.0"
$appDeployExtScriptDate = "08/16/2013"
$appDeployExtScriptParameters = $psBoundParameters

#*=============================================
#* FUNCTION LISTINGS
#*=============================================

# Your custom functions go here

#*=============================================
#* END FUNCTION LISTINGS
#*=============================================

#*=============================================
#* SCRIPT BODY
#*=============================================

 function sendinstallationMail([String] $AppToBeManaged)
 { 

   #  Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "smtp hostname or IP"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure 
     $msg.From = "mailaddress@domain.com"
     $msg.ReplyTo = "mailaddress@domain.com"
     $msg.To.Add("mailaddress@domain.com")
     $msg.subject = "$AppToBeManaged Installation"
     $CurrentUser =  Get-WmiObject -ComputerName $env:ComputerName -Class Win32_ComputerSystem | Select Username
     $msg.body = "Your Software  $AppToBeManaged  has been successfully installed on $env:ComputerName for user $CurrentUser."

     #Sending email 
     $smtp.Send($msg)
  
}


 function senduninstallationMail([String] $AppToBeManaged)
 {

     #Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "smtp.hostname or IP"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure 
     $msg.From = "mailaddress@domain.com"
     $msg.ReplyTo = "mailaddress@domain.com"
     $msg.To.Add("mailaddress@domain.com")
     $msg.subject = "$AppToBeManaged Uninstallation"
     $CurrentUser =  Get-WmiObject -ComputerName $env:ComputerName -Class Win32_ComputerSystem | Select Username
     $msg.body = "Your Software  $AppToBeManaged  has been successfully uninstalled on $env:ComputerName for user $CurrentUser."

     #Sending email 
     $smtp.Send($msg)
  
}

If ($scriptParentPath -ne "") {  
    Write-Log "Script [$($MyInvocation.MyCommand.Definition)] dot-source invoked by [$(((Get-Variable MyInvocation).Value).ScriptName)]";
}
Else {
    Write-Log "Script [$($MyInvocation.MyCommand.Definition)] invoked directly";  
}

#*=============================================
#* END SCRIPT BODY
#*=============================================App