-
Notifications
You must be signed in to change notification settings - Fork 14
/
ConvertTo-ServiceUrl.ps1
63 lines (53 loc) · 1.9 KB
/
ConvertTo-ServiceUrl.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function ConvertTo-ServiceUrl
{
<#
.Synopsis
Converts parameters into a full URL for a Pipeworks Service
.Description
Converts parameters into a full URL for a Pipeworks web service.
This allows you to easily call another a Pipeworks web service with a uniform URL.
This can work because all Pipeworks services have a very uniform URL format:
ServiceUrl/Command/?Command_Parameter1=Value&Command_Parameter2=Value
.Example
ConvertTo-ServiceUrl -ServiceUrl "http://powershellpipeworks.com/" -CommandName "Write-ScriptHTML" -Parameter @{
Text = "'hello world'"
}
.Link
ConvertTo-ModuleService
#>
[OutputType([string])]
param(
# The root URL of the web service
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
[Uri]
$ServiceUrl,
# The name of the command in the Pipeworks module.
[Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true)]
[string]
$CommandName,
# The name of the command in the Pipeworks module.
[Parameter(Mandatory=$true,Position=2,ValueFromPipelineByPropertyName=$true)]
[Alias('Parameters')]
[Hashtable]
$Parameter,
# If set, will get a URL to return the XML
[switch]
$AsXml
)
process {
#region Create a GET string
# Carry over the parameters
$actionUrl = foreach ($kv in $Parameter.GetEnumerator()) {
"${CommandName}_$($kv.Key)=$([Web.HttpUtility]::UrlEncode($kv.Value))"
}
$actionUrl = "/${CommandName}/?" + ($actionUrl -join '&')
#endregion Create a GET string
if ($AsXml) {
$actionUrl += "&AsXml=true"
}
if ($ServiceUrl) {
$actionUrl = "$ServiceUrl".TrimEnd("/") + $actionUrl
}
$actionUrl
}
}