ServerEye.Powershell.Helper.psm1
<#
.SYNOPSIS Connect to a new Server-Eye API session. .PARAMETER Credentials If passed the cmdlet will use this credential object instead of asking for username and password. .PARAMETER Code This is the second factor authentication code. .EXAMPLE $session = Connect-Session .LINK https://api.server-eye.de/docs/2/ #> function Connect-Session { [CmdletBinding()] Param( [Parameter(Mandatory=$false)] $Credentials, [Parameter(Mandatory=$false)] [string] $Code ) Process { if (-not $Credentials) { $Credentials = Get-Credential -Message 'Server-Eye Login' } $reqBody = @{ 'email' = $Credentials.UserName 'password' = $Credentials.GetNetworkCredential().Password 'code' = $Code } | ConvertTo-Json try { $res = Invoke-WebRequest -Uri https://api.server-eye.de/2/auth/login -Body $reqBody ` -ContentType "application/json" -Method Post -SessionVariable session } catch { if ($_.Exception.Response.StatusCode.Value__ -eq 420) { $secondFactor = Read-Host -Prompt "Second Factor" return Connect-Session -Credentials $Credentials -Code $secondFactor } else { throw "Could not login. Please check username and password." return } } return $session } } function Disconnect-Session ($Session) { Invoke-WebRequest -Uri https://api.server-eye.de/2/auth/logout -WebSession $Session | Out-Null } function Intern-DeleteJson($url, $session, $apiKey) { if ($authtoken -is [string]) { return (Invoke-RestMethod -Uri $url -Method Delete -Headers @{"x-api-key"=$authtoken} ); } else { return (Invoke-RestMethod -Uri $url -Method Delete -WebSession $authtoken ); } } function Intern-GetJson($url, $authtoken) { if ($authtoken -is [string]) { return (Invoke-RestMethod -Uri $url -Method Get -Headers @{"x-api-key"=$authtoken} ); } else { return (Invoke-RestMethod -Uri $url -Method Get -WebSession $authtoken ); } } function Intern-PostJson($url, $authtoken, $body) { $body = $body | Remove-Null | ConvertTo-Json if ($authtoken -is [string]) { return (Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -Headers @{"x-api-key"=$authtoken} ); } else { return (Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -WebSession $authtoken ); } } function Intern-PutJson ($url, $authtoken, $body) { $body = $body | Remove-Null | ConvertTo-Json if ($authtoken -is [string]) { return (Invoke-RestMethod -Uri $url -Method Put -Body $body -ContentType "application/json" -Headers @{"x-api-key"=$authtoken} ); } else { return (Invoke-RestMethod -Uri $url -Method Put -Body $body -ContentType "application/json" -WebSession $authtoken ); } } function Remove-Null { [cmdletbinding()] Param ( [parameter(ValueFromPipeline)] $obj ) Process { $result = @{} foreach ($key in $_.Keys) { if ($_[$key]) { $result.Add($key, $_[$key]) } } $result } } $moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path "$moduleRoot/functions/*.ps1" | Resolve-Path | ForEach-Object { . $_.ProviderPath } |