[PowerShell] appsettings.json から情報を取り出す
appsettings.json
(値はダミー)
{ "applicationId": "56c0ac2c-c858-4781-ad48-749312fb2fdd", "clientSecret": "bXw7Q~Pn0fv8NHXBZdWzgkRm2gzFd-.fsZx~O", "directoryId": "a9fc19ab-08cf-44a9-b2dc-1fd0542d6b4f" }
呼出し側。appsettings.json と同じ階層に。
$config = Get-Content -Path $PSScriptRoot\appsettings.json | ConvertFrom-Json $applicationId = $config.applicationId $clientSecret = $config.clientSecret $directoryId = $config.directoryId $scope = "https://graph.microsoft.com/.default" $response = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$directoryId/oauth2/v2.0/token" -Method Post -Body @{client_id=$applicationId; scope=$scope; client_secret=$clientSecret; grant_type="client_credentials"} $accessToken = $response.access_token $headers = @{"Authorization" = "Bearer " + $accessToken} $users = Invoke-WebRequest -Uri https://graph.microsoft.com/v1.0/users -Headers $headers | ConvertFrom-Json $users.value | Select-Object UserPrincipalName