Trackyon.Invest.functions.ps1
function _applyTypes { [CmdletBinding()] param( $items ) foreach ($item in $items) { $item.PSObject.TypeNames.Insert(0, 'Trackyon.Invest.stock') } } function _isOnWindows { $os = Get-OperatingSystem return $os -eq 'Windows' } function _testAdministrator { $user = [Security.Principal.WindowsIdentity]::GetCurrent() (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) } function _buildLevelDynamicParam { param () # # Only add these options on Windows Machines if (_isOnWindows) { $ParameterName = 'Level' # Create the dictionary $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary # Create the collection of attributes $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] # Create and set the parameters' attributes $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $false $ParameterAttribute.HelpMessage = "On Windows machines allows you to store the default project at the process, user or machine level. Not available on other platforms." # Add the attributes to the attributes collection $AttributeCollection.Add($ParameterAttribute) # Generate and set the ValidateSet if (_testAdministrator) { $arrSet = "Process", "User", "Machine" } else { $arrSet = "Process", "User" } $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) # Add the ValidateSet to the attributes collection $AttributeCollection.Add($ValidateSetAttribute) # Create and return the dynamic parameter $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) return $RuntimeParameterDictionary } } function Connect-TkyAccount { [CmdletBinding(DefaultParameterSetName = 'FromCommandLine')] param ( [Parameter(Position = 0, Mandatory)] [securestring] $key ) DynamicParam { _buildLevelDynamicParam } begin { if (_isOnWindows) { $Level = $PSBoundParameters['Level'] } } process { $credential = New-Object System.Management.Automation.PSCredential "nologin", $key $token = $credential.GetNetworkCredential().Password if (_isOnWindows) { if (-not $Level) { $Level = "Process" } [System.Environment]::SetEnvironmentVariable("Trackyon_Subscription_Key", $token, $Level) } # You always have to set at the process level or they will Not # be seen in your current session. $env:Trackyon_Subscription_Key = $token } } function Disconnect-TkyAccount { [CmdletBinding()] param () DynamicParam { _buildLevelDynamicParam } begin { if (_isOnWindows) { # Bind the parameter to a friendly variable $Level = $PSBoundParameters['Level'] } } process { if (_isOnWindows) { if (-not $Level) { $Level = "Process" } if (_isOnWindows) { [System.Environment]::SetEnvironmentVariable("Trackyon_Subscription_Key", $null, $Level) } } # You always have to set at the process level or they will Not # be seen in your current session. $env:Trackyon_Subscription_Key = $null Write-Output "Disconnected" } } function Get-TkyDate { [CmdletBinding(DefaultParameterSetName = 'FromCommandLine')] param ( [Parameter(ParameterSetName = 'FromCommandLine', ValueFromPipeline = $True, Position = 0)] [string[]] $tickers, [Parameter(ParameterSetName = 'FromFile', ValueFromPipelineByPropertyName = $True)] [string] $tickerfile, [switch] $prices ) process { $uri = 'https://exdateapimanagement.azure-api.net/getstocks/GetStocks' if ($prices.IsPresent) { Write-Verbose 'Prices requested' $uri += '?with_prices=true' } # The order is import so the file and command line are read before # the environment variable if ($tickerfile) { Write-Verbose 'Reading tickers from file' $tickers = ConvertFrom-Json $(Get-Content $tickerfile -Raw) } elseif ($tickers) { Write-Verbose 'Reading tickers from command line' # This is a no-op because it already passed in as an array that # will be used below to create the body. } elseif ($env:Trackyon_Tickers) { # Read from environment variable Write-Verbose 'Reading tickers from environment variable' $tickers = ConvertFrom-Json $env:Trackyon_Tickers } Write-Verbose "tickers = $tickers" if (-not $tickers) { Write-Error 'Could not find tickers.' return } $body = ConvertTo-Json @{ tickers = $tickers } -Compress if (-not $env:Trackyon_Subscription_Key) { Write-Error 'Could not read subscription key. Run Connect-TkyAccount.' } else { $headers = @{ 'Trackyon-Subscription-Key' = $env:Trackyon_Subscription_Key } $resp = Invoke-RestMethod -Method Post ` -Uri $uri ` -Headers $headers ` -Body $body _applyTypes $resp Write-Output $resp | Sort-Object -Property purchase_By_Date } } } |