Weedu.psm1
Add-Type -AssemblyName System.Web $script:hostUrl= $null $script:sessionVar = $null function Set-UserCredentialsAndUrl { param( [Parameter(Mandatory=$true)] [string]$Benutzername, [Parameter(Mandatory=$true)] [Security.SecureString]$Kennwort, [Parameter(Mandatory = $true, HelpMessage = "Url zu welcher die Verbindung hergestellt werden soll. Bsp.: https://akad-de.test-weedu.ch")] [string]$HostUrl ) process{ try{ $password = ConvertTo-PlainText $Kennwort $pair = "$($Benutzername):$($password)" $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) $basicAuthValue = "Basic $encodedCreds" $Headers = @{ Accept = "application/json, text/javascript, */*" Authorization = $basicAuthValue } $result = Invoke-WebRequest -Uri "$($HostUrl)/sts/authentication" -Method Post -Headers $Headers -SessionVariable session if($result.StatusCode.Value__ -eq 200) { $script:sessionVar = $session $script:hostUrl ="$($HostUrl)" } else { Write-Host "Login Fehlgeschlagen. Erneut versuchen" -foregroundcolor "magenta" } } catch [System.Net.WebException]{ [System.Net.HttpWebResponse]$result = [System.Net.HttpWebResponse] $_.Exception.Response $httperrorcode = $result.StatusCode.Value__ $error.clear() Write-Host "Login Fehlgeschlagen. Erneut versuchen" -foregroundcolor "magenta" } } } # function Set-WQLAbfrage{ param( [Parameter(Mandatory=$true, HelpMessage = "WQL Abfrage. Bsp: Personen [ohne Aktionen]")] [string]$WQLAbfrage ) process{ try{ if(!$script:sessionVar) { Write-Host "Bitte einloggen" -foregroundcolor "magenta" break } $Headers = @{ Accept = "application/json, text/javascript, */*" "Content-Type" = "application/json; charset=UTF-8" } $Body = "{page:1,pageSize:50}" $query = "{0}/{1}?auswahlSpalte={2}&wql={3}" -f "$($script:hostUrl)", "index/Abfragen/IndexData", "false", [System.Web.HttpUtility]::UrlEncode("$($WQLAbfrage)"); $result = Invoke-WebRequest -Uri "$($query)" -Method Post -Headers $Headers -WebSession $script:sessionVar -Body $Body $httperrorcode = $result.Statuscode if($result.StatusCode.Value__-eq 200) { $resultConverted = $result | ConvertFrom-Json Return $resultConverted.result.data } else { Write-Host "Bei der Abfrage ist was schief gelaufen. Bitte WQL-Abfrage überprüfen oder erneut anmelden" -foregroundcolor "magenta" } } catch [System.Net.WebException]{ [System.Net.HttpWebResponse]$result = [System.Net.HttpWebResponse] $_.Exception.Response $httperrorcode = $result.StatusCode.Value__ $error.clear() Write-Host "Bei der Abfrage ist was schief gelaufen. Bitte WQL-Abfrage überprüfen oder erneut anmelden" -foregroundcolor "magenta" } } } Function ConvertTo-PlainText( [security.securestring]$secure ) { $marshal = [Runtime.InteropServices.Marshal] $marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($secure) ) } Export-ModuleMember -function Set-UserCredentialsAndUrl Export-ModuleMember -function Set-WQLAbfrage |