Parts/Url.Macro.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<#
.Synopsis
    The Macro for -URL based functions
.Description
    The process{} block for any macro that uses -URL.
.Notes
    Macros must be included inline, in order to get the correct invocation name.
#>


$callStack = @(Get-PSCallStack)
$isBeingAssigned = -not "$($callstack[-1].Position.Text)".Trim().StartsWith($MyInvocation.InvocationName)
$isBeingPiped = $MyInvocation.PipelinePosition -lt $MyInvocation.PipelineLength

Write-Verbose "Original URL: $url"
$url = $urlReplaceRegex.Replace("$url", $urlReplacer)
Write-Verbose "Replaced URL: $url"
$realUri = [uri]$url

if ($WhatIfPreference) { return $url }
if (-not $realUri) { return }
if (-not $isBeingAssigned -and -not $isBeingPiped -and -not $underlyingParameters.Count -and
    ($psVersionTable.Platform -eq 'Windows' -or -not $psVersionTable.PlatForm)) {
    if (-not $args) {
        Start-Process -FilePath $realUri
    } else {
        Start-Process -FilePath ("$realUri".TrimEnd('?') + '?' + $(@($args -join '&')))
    }
} else {

    Invoke-RestMethod -Uri $realUri @underlyingParameters
}