Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ComFreek/5b054ae1701cc46042df to your computer and use it in GitHub Desktop.
Save ComFreek/5b054ae1701cc46042df to your computer and use it in GitHub Desktop.
automail Notifier for Chocolatey package maintainers

What is this?

The audience is intended to be Chocolatey package maintainers who employ automatic packages with Ketarin. (Not already using automatic packages? Better start now!)

Such package mainatainers might wish to be informed when an application gets updated and/or pushed to Chocolatey in response to Ketarin's update procedure. The two scripts contained in this Gist accommodate that request.

How to use

  1. Download this Gist.
  2. Open Ketarin
  3. Click on File, Settings and finally on Commands
  4. Select After updating an application from the dropdown list.
  5. Paste the following command into the text box:
 powershell P:\Chocolatey\automail\send.ps1 -appName "{appname} {nopush}" -version "{version}"

INFO: {nopush} was originally intended to be a variable which can be set on a per-application basis to /disablepush to disable pushing in the chocopkgup command (which should be set under Before updating an application). Including the information whether pushing has been disabled or not is therefore the sole reason to use {nopush} in the command above.) 4. Close all dialogs by subsequently clicking Ok. 3. Run init.ps1 and follow the instructions.
Your server's data (domain/IP, port, use SSL/TLS?) can be found on the Internet in most (if not all) cases.

WARNING Your password will be saved into {path from init.ps1}\config.txt in an encrypted form. There is no secure way to store a password which must be used (i.e. decrypted) on a potentially infected machine! See ConvertTo-SecureString to learn about the encryption method being used.

<#
Function copied from http://blogs.technet.com/b/jamesone/archive/2009/06/24/how-to-get-user-input-more-nicely-in-powershell.aspx
Many thanks to the author James ONeill!
License: Sadly unknown, corrections are welcome! Please always retain this authorship notice!
#>
Function Select-Item
{
<#
.Synopsis
Allows the user to select simple items, returns a number to indicate the selected item.
.Description
Produces a list on the screen with a caption followed by a message, the options are then
displayed one after the other, and the user can one.
Note that help text is not supported in this version.
.Example
PS> select-item -Caption "Configuring RemoteDesktop" -Message "Do you want to: " -choice "&Disable Remote Desktop",
"&Enable Remote Desktop","&Cancel" -default 1
Will display the following
Configuring RemoteDesktop
Do you want to:
[D] Disable Remote Desktop [E] Enable Remote Desktop [C] Cancel [?] Help (default is "E"):
.Parameter Choicelist
An array of strings, each one is possible choice. The hot key in each choice must be prefixed with an & sign
.Parameter Default
The zero based item in the array which will be the default choice if the user hits enter.
.Parameter Caption
The First line of text displayed
.Parameter Message
The Second line of text displayed
#>
Param( [String[]]$choiceList,
[String]$Caption="Please make a selection",
[String]$Message="Choices are presented below",
[int]$default=0
)
$choicedesc = New-Object System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]
$choiceList | foreach { $choicedesc.Add((New-Object "System.Management.Automation.Host.ChoiceDescription" -ArgumentList $_))}
$Host.ui.PromptForChoice($caption, $message, $choicedesc, $default)
}
$path = Join-Path $PSScriptRoot "config.txt"
$credential = Get-Credential -Message "Please enter your username and password of your (or a company's)SMTP server"
$config = @{
"UserName" = $credential.UserName;
"Password" = $credential.Password | ConvertFrom-SecureString;
"To" = Read-Host "Recipient"
"From" = Read-Host "Sender"
"SmtpServer" = Read-Host "SMTP Server";
"SmtpPort" = Read-Host "SMTP Port";
"UseSsl" = if ((Select-Item -Caption "Use SSL (or rather TLS)?" -Choice "&Yes", "&No" -Default 0) -eq 0) { $true } else { $false }
}
$config | ConvertTo-Json > $path
param(
[string] $appName,
[string] $version
)
# Copied from http://stackoverflow.com/a/27992426/603003 (and marginally modified by renaming two variables)
# Thanks to user2656928 (http://stackoverflow.com/users/2656928/user2656928)
# License: CC BY-SA 3.0
$showWindowFunction = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
Add-Type -Name win -Member $showWindowFunction -Namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
# End of copied snippet
$config = Get-Content (Join-Path $PSScriptRoot "config.txt") -Raw | ConvertFrom-Json
# Message data
$subject = "[CHOCO UPDATE] $appName --> $version"
$message = "This is an automatically sent mail from automail (for Ketarin/Chocolatey package maintainers) in response to Ketarin's update process"
# Load from config
$pw = $config.Password | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential $config.UserName, $pw
# Send mail
Send-MailMessage -To $config.To -From $config.From -Subject $subject -Body $message -SmtpServer $config.SmtpServer -Port $config.SmtpPort -UseSsl:$config.UseSsl -Credential $credential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment