Initial commit
This commit is contained in:
53
Microsoft.SelfService.Portal.PowerShell.Core.psm1
Normal file
53
Microsoft.SelfService.Portal.PowerShell.Core.psm1
Normal file
@@ -0,0 +1,53 @@
|
||||
function Invoke-SSPRequest {
|
||||
Param(
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet('Get','Post','Put','Delete')]
|
||||
[System.String] $Method = 'Get',
|
||||
[Parameter(Mandatory=$true)]
|
||||
[System.String] $Endpoint,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[System.String] $Query,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[System.String] $Body
|
||||
)
|
||||
|
||||
if($Query){
|
||||
$Uri = $("https://localhost:7260/api/"+$Endpoint+"/"+$Query)
|
||||
}else{
|
||||
$Uri = $("https://localhost:7260/api/"+$Endpoint)
|
||||
}
|
||||
|
||||
switch($Method){
|
||||
{($_ -eq "Get") -or ($_ -eq "Delete")} {
|
||||
$Parameter = @{}
|
||||
$Parameter.Add("Method",$Method)
|
||||
$Parameter.Add("Uri", $Uri)
|
||||
$Parameter.Add("UseDefaultCredentials",$true)
|
||||
$Parameter.Add("ContentType","application/json")
|
||||
}
|
||||
|
||||
{($_ -eq "Post") -or ($_ -eq "Put")} {
|
||||
$Parameter = @{}
|
||||
$Parameter.Add("Method",$Method)
|
||||
$Parameter.Add("Uri", $Uri)
|
||||
$Parameter.Add("Body", $Body)
|
||||
$Parameter.Add("UseDefaultCredentials",$true)
|
||||
$Parameter.Add("ContentType","application/json")
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
# Versuch, die Anfrage auszuführen
|
||||
$Response = Invoke-RestMethod @Parameter
|
||||
return $Response
|
||||
} catch [System.Net.WebException] {
|
||||
# Fehler abfangen und Antwort auswerten
|
||||
$ErrorResponse = $_.Exception.Response
|
||||
if ($ErrorResponse -ne $null) {
|
||||
$Reader = New-Object System.IO.StreamReader($ErrorResponse.GetResponseStream())
|
||||
return $($Reader.ReadToEnd() | ConvertFrom-Json)
|
||||
} else {
|
||||
Write-Host "Kein Antwortkörper verfügbar."
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user