获取所有用户的列表

下面的示例展示了如何登录到API并检索people目录中的所有用户列表。

Add-Type -AssemblyName System.Net.Http [Net. Net]ServicePointManager]::SecurityProtocol = [Net. Net.]安全ProtocolType]::Tls12 $username = "{username}" $password = "{password}" $tenantGuid = "{tenantguid}" $apiuri = "{apiurl}" function LogIn(){ # Write a syncoption element using the passed values $log = @{ "grant_type" = "password" "username" = $username "password" = $password} $header = @{ "X-Tenant" = $tenantGuid} $api = Invoke-Restmethod -Uri ($apiuri + "/token") -Method post -Body $log -ContentType "application/x-www-form-urlencoded" -Headers $header return $api.access_token } function GetPeople([string] $token){ $authenticateHeader = @{ "X-Tenant" = $tenantGuid "Authorization" = "Bearer " + $token} $peopleUri = ($apiuri + "/api/people") # Initialise offset value at 0 $offset = 0 $limit = 50 $continue = $true while($continue){ $uriQ = $peopleUri + "?limit=" + $limit + "&offset=" + $offset # Launch + access API $people = Invoke-RestMethod -Uri $uriQ -Method GET -Body $log -ContentType $content -headers $authenticateHeader foreach($person in $people.results){ ProcessPerson $person } if($people.results.count -ne $limit){ $continue = $false; } $offset += $limit } } function ProcessPerson($person){ #Do something to the person Write-Host $person.FullName; } $token = LogIn GetPeople $token

Baidu
map