IFTTT_Maker_PowerShell.psm1
Set-StrictMode -Version Latest <# .SYNOPSIS Triggers an event on the IFTTT Maker Channel. .DESCRIPTION Send-Maker triggers an event on the IFTTT Maker Channel. The event can have up to 3 values that can be passed. .PARAMETER EventName The name of the event to trigger on the IFTTT Maker Channel. .PARAMETER Key The secret key you got from IFTTT for triggering events on the Maker Channel. .PARAMETER Value1 First value passed to the event (ingredient Value1). .PARAMETER Value2 Second value passed to the event (ingredient Value2). .PARAMETER Value3 Third value passed to the event (ingredient Value3). .PARAMETER Save Defines if the current config should be saved. .PARAMETER Delete Deletes the current config if it exists. #> Function Send-IFTTTMaker { #region Params [CmdletBinding()] param ( [ValidateNotNullOrEmpty()] [Parameter(Position=0,HelpMessage="The secret key you got from IFTTT for triggering events on the Maker Channel.")] [string] $Key, [ValidateNotNullOrEmpty()] [Parameter(Position=1,HelpMessage="The name of the event to trigger on the IFTTT Maker Channel")] [string] $EventName, [ValidateNotNullOrEmpty()] [Parameter(Position=2,ValueFromPipeline=$true,HelpMessage="First value passed to the event (ingredient Value1).")] [string] $Value1, [ValidateNotNullOrEmpty()] [Parameter(Position=3,HelpMessage="Second value passed to the event (ingredient Value2).")] [string] $Value2, [ValidateNotNullOrEmpty()] [Parameter(Position=4,HelpMessage="Third value passed to the event (ingredient Value3).")] [string] $Value3, [Parameter(HelpMessage="Defines if the current config should be saved.")] [switch]$Save, [Parameter(HelpMessage="Deletes the current config if it exists.")] [switch]$Delete ) #endregion $ErrorActionPreference = "Stop" # Die if Exception! $Site="https://maker.ifttt.com/trigger/" $SaveFile="$env:APPDATA\Send-IFTTTMaker\info.json" $JsonBody =$null Set-Variable -Name Key,EventName,Value1,Value2,Value3,JsonBody,Site -Option AllScope #region Sub Functions function Load-Config { $Success = $false $SaveFolder = Split-Path -Path $SaveFile $d =New-Item -ItemType Directory -Force -Path $SaveFolder If (Test-Path $SaveFile) { Write-Verbose "Attempting to load config file..." $Config = Get-Content $SaveFile | ConvertFrom-Json if($Config.Key -ne "" -and $Key -eq "") {$Key = $Config.Key} if($Config.EventName -ne "" -and $EventName -eq "") {$EventName = $Config.EventName} if($Config.Value1 -ne "" -and $Value1 -eq "") {$Value1 = $Config.Value1} if($Config.Value2 -ne "" -and $Value2 -eq "") {$Value2 = $Config.Value2} if($Config.Value3 -ne "" -and $Value3 -eq "") {$Value3 = $Config.Value3} Write-Verbose "Successfully loaded config file." } Write-Verbose "Parsing config as Json." $_JsonBody = @{} if($Value1 -ne "") { $_JsonBody.value1=$Value1 if($Value2 -ne "") { $_JsonBody.value2=$Value2 if($Value3 -ne "") { $_JsonBody.value3=$Value3}}} $JsonBody =ConvertTo-Json $_JsonBody Write-Verbose "successfully completed parsing config as Json." $Site = "$Site$EventName/with/key/$Key" if(-not($Key)){Throw New-Object System.ArgumentNullException "Key","Parameter must be provided."} if(-not($EventName)){Throw New-Object System.ArgumentNullException "EventName","Parameter must be provided."} if(-not($Site)){Throw New-Object System.ArgumentNullException "Site","Parameter must be provided."} } function Save-Config { $JsonExport = @{} $JsonExport.Value1= $Value1 $JsonExport.Value2= $Value2 $JsonExport.Value3= $Value3 $JsonExport.Key = $Key $JsonExport.EventName = $EventName Write-Verbose "Saving config file..." $JsonExport | ConvertTo-Json | Set-Content $SaveFile Write-Verbose "Successfully saved config file." } function Delete-Config { Write-Verbose "Deleteing config file" Remove-Item $SaveFile Return $true } #endregion if($Delete) { Delete-Config return $true } Load-Config if($Save) { Save-Config } $WebResponse = Invoke-WebRequest -Uri $Site -Method Post -ContentType "application/json" -Body $JsonBody if($VerbosePreference){return $WebResponse} return $WebResponse.Content } |