-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-UTF8RestMethod.ps1
65 lines (65 loc) · 2.48 KB
/
Get-UTF8RestMethod.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
64
65
Function Get-UTF8RestMethod {
[OutputType([string])]
[CmdletBinding()]
Param(
[string]$uri
)
Try
{
Add-Type -AssemblyName System.IO
Add-Type -AssemblyName System.Net
Add-Type -AssemblyName System.Net.Http
}
Catch
{
[array]$error_clone = $Error.Clone()
[string]$error_message = $error_clone | Where-Object { $null -ne $_.Exception } | Select-Object -First 1 | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty Message
Write-Warning "Error: Unable to load assemblies due to [$error_message]"
Return
}
Try
{
[System.Net.WebRequest]$web_request = [System.Net.WebRequest]::Create($uri)
}
Catch
{
[array]$error_clone = $Error.Clone()
[string]$error_message = $error_clone | Where-Object { $null -ne $_.Exception } | Select-Object -First 1 | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty Message
Write-Warning "Error: Unable to create web request due to [$error_message]"
Return
}
Try
{
[System.Net.WebResponse]$response = $web_request.GetResponse()
}
Catch
{
[array]$error_clone = $Error.Clone()
[string]$error_message = $error_clone | Where-Object { $null -ne $_.Exception } | Select-Object -First 1 | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty Message
Write-Warning "Error: Web response failed due to [$error_message]"
Return
}
Try
{
[System.IO.StreamReader]$reader = New-Object -TypeName System.IO.StreamReader($response.GetResponseStream())
}
Catch
{
[array]$error_clone = $Error.Clone()
[string]$error_message = $error_clone | Where-Object { $null -ne $_.Exception } | Select-Object -First 1 | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty Message
Write-Warning "Error: StreamReader failed to get the response stream due to [$error_message]"
Return
}
Try
{
[string]$response_string = $reader.ReadToEnd()
}
Catch
{
[array]$error_clone = $Error.Clone()
[string]$error_message = $error_clone | Where-Object { $null -ne $_.Exception } | Select-Object -First 1 | Select-Object -ExpandProperty Exception | Select-Object -ExpandProperty Message
Write-Warning "Error: StreamReader failed to read the response stream due to [$error_message]"
Return
}
Return $response_string
}