forked from treestryder/powershell_module_workdayapi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvoke-WorkdayRequest.ps1
More file actions
128 lines (107 loc) · 4.24 KB
/
Invoke-WorkdayRequest.ps1
File metadata and controls
128 lines (107 loc) · 4.24 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function Invoke-WorkdayRequest {
<#
.SYNOPSIS
Sends XML requests to Workday API, with proper authentication and receives XML response.
.DESCRIPTION
Sends XML requests to Workday API, with proper authentication and receives XML response.
Used for all communication to Workday in this module and may be used to send
custom XML requests.
.PARAMETER Request
The Workday request XML to be sent to Workday.
See https://community.workday.com/custom/developer/API/index.html for more information.
.PARAMETER Uri
Endpoint Uri for the request.
.PARAMETER Username
Username used to authenticate with Workday. If empty, the value stored
using Set-WorkdayCredential will be used.
.PARAMETER Password
Password used to authenticate with Workday. If empty, the value stored
using Set-WorkdayCredential will be used.
.EXAMPLE
$response = Invoke-WorkdayRequest -Request '<bsvc:Server_Timestamp_Get xmlns:bsvc="urn:com.workday/bsvc" />' -Uri https://SERVICE.workday.com/ccx/service/TENANT/Human_Resources/v25.1
$response.Server_Timestamp
wd version Server_Timestamp_Data
-- ------- ---------------------
urn:com.workday/bsvc v25.1 2015-12-02T12:18:30.841-08:00
.INPUTS
Workday XML
.OUTPUTS
Workday XML
#>
[CmdletBinding()]
[OutputType([XML])]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[xml]$Request,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Uri,
[string]$Username,
[string]$Password
)
if ($WorkdayConfiguration.Credential -is [PSCredential]) {
if ([string]::IsNullOrWhiteSpace($Username)) { $Username = $WorkdayConfiguration.Credential.Username }
if ([string]::IsNullOrWhiteSpace($Password)) { $Password = $WorkdayConfiguration.Credential.GetNetworkCredential().Password }
}
$WorkdaySoapEnvelope = [xml] @'
<soapenv:Envelope xmlns:bsvc="urn:com.workday/bsvc" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>IntegrationUser@Tenant</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<bsvc:RequestNode xmlns:bsvc="urn:com.workday/bsvc" />
</soapenv:Body>
</soapenv:Envelope>
'@
$WorkdaySoapEnvelope.Envelope.Header.Security.UsernameToken.Username = $Username
$WorkdaySoapEnvelope.Envelope.Header.Security.UsernameToken.Password.InnerText = $Password
$WorkdaySoapEnvelope.Envelope.Body.InnerXml = $Request.OuterXml
Write-Debug "Request: $($WorkdaySoapEnvelope.OuterXml)"
$headers= @{
'Content-Type' = 'text/xml;charset=UTF-8'
}
$o = [pscustomobject][ordered]@{
Success = $false
Message = 'Unknown Error'
Xml = $null
}
$o.psobject.TypeNames.Insert(0, "WorkdayResponse")
$response = $null
try {
$response = Invoke-RestMethod -Method Post -Uri $Uri -Headers $headers -Body $WorkdaySoapEnvelope
$o.Xml = [xml]$response.Envelope.Body.InnerXml
$o.Message = ''
$o.Success = $true
}
catch [System.Net.WebException] {
$o.Success = $false
$reader = New-Object System.IO.StreamReader -ArgumentList $_.Exception.Response.GetResponseStream()
$response = $reader.ReadToEnd()
$reader.Close()
try {
$xml = [xml]$response
$o.Xml = [xml]$xml.Envelope.Body.InnerXml
# Put the first Workday Exception into the Message property.
if ($o.Xml.InnerXml.StartsWith('<SOAP-ENV:Fault ')) {
$o.Success = $false
$o.Message = "$($o.Xml.Fault.faultcode): $($o.Xml.Fault.faultstring)"
}
}
catch {
$o.Message = $response
}
}
catch {
$o.Success = $false
$o.Message = $_.ToString()
}
finally {
Write-Output $o
}
}