Public/Invoke-TwilioCli.ps1
|
using namespace System.Management.Automation function Invoke-TwilioCli { #.DESCRIPTION # Creates a custom TwilioCli object and Invokes methods on it. # .EXAMPLE # "This is my secret message" | TwilioCli SignMessage # .NOTES # If you want more control you can directly use the [Twilio] class :) # .OUTPUTS # [PSCustomObject] #.LINK # https://github.com/chadnpc/cliHelper.twilio/blob/main/Public/Invoke-TwilioCli.ps1 [CmdletBinding()] [Alias('twilio', 'TwilioCli')] [OutputType({ [Twilio]::ReturnTypes })] param( [Parameter(Mandatory = $false, Position = 0)] [Alias('m')][AllowEmptyString()] [ArgumentCompleter({ [OutputType([System.Management.Automation.CompletionResult])] param( [string] $CommandName, [string] $ParameterName, [string] $WordToComplete, [System.Management.Automation.Language.CommandAst] $CommandAst, [System.Collections.IDictionary] $FakeBoundParameters ) $CompletionResults = [System.Collections.Generic.List[CompletionResult]]::new() $matchingMethods = [Twilio]::Methods.Where({ $_.Name -like "$WordToComplete*" -and $_.CustomAttributes.AttributeType.Name -notcontains "HiddenAttribute" }) foreach ($method in $matchingMethods) { $paramst = ($method.GetParameters() | Select-Object @{l = '_'; e = { "[$($_.ParameterType.Name)]`$$($_.Name)" } })._ -join ', ' $toolTip = "[{0}] {1}({2})" -f $method.ReturnType.Name, $method.Name, $paramst $CompletionResults.Add([System.Management.Automation.CompletionResult]::new($method.Name, $toolTip, 'Method', $toolTip)) } return $CompletionResults })] [string]$Method, [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('i')][ValidateNotNullOrEmpty()] $InputObject ) begin { $directlyUseInputObject = $false $buff = [System.Collections.Generic.List[byte]]::new() $meth = [string]::IsNullOrWhiteSpace($Method) ? "ShowHelp" : $Method $cliargLine = ![Twilio]::Methods.Name.Contains($meth) -or $meth.Contains("-") } process { if ($PSBoundParameters.ContainsKey('InputObject') -and !$cliargLine) { if ($InputObject -is [byte]) { [void]$buff.Add($InputObject) } else { $directlyUseInputObject = $true } } } end { if ($directlyUseInputObject) { $r = [Twilio]::$meth($InputObject) if ($null -ne $r) { Write-Output -NoEnumerate -InputObject $r } } elseif ($buff.Count -gt 0 -and !$cliargLine) { $r = [Twilio]::$meth($buff.ToArray()) if ($null -ne $r) { Write-Output -NoEnumerate -InputObject $r } } elseif (!$PSBoundParameters.ContainsKey('InputObject') -and !$cliargLine) { return [Twilio]::$meth() } else { return [Twilio]::Run($meth) } } } |