使用powershell连接Azure数据目录REST API

使用powershell连接Azure数据目录REST API,powershell,powershell-2.0,powershell-3.0,powershell-4.0,azure-powershell,Powershell,Powershell 2.0,Powershell 3.0,Powershell 4.0,Azure Powershell,我想使用powershell脚本连接到Azure数据目录RestAPI,下面是我无法运行的脚本 使用ADAL获取访问令牌 我收到下面的错误消息 调用RestMethod: 401-未经授权:由于凭据无效,访问被拒绝。 服务器错误 401-未经授权:由于凭据无效,访问被拒绝。 您没有使用您提供的凭据查看此目录或页面的权限 第1行字符:13 +$response=Invoke RestMethod-Method GET-Uri$url-Headers$Headers + ~~

我想使用powershell脚本连接到Azure数据目录RestAPI,下面是我无法运行的脚本

使用ADAL获取访问令牌 我收到下面的错误消息 调用RestMethod: 401-未经授权:由于凭据无效,访问被拒绝。 服务器错误

401-未经授权:由于凭据无效,访问被拒绝。 您没有使用您提供的凭据查看此目录或页面的权限

第1行字符:13 +$response=Invoke RestMethod-Method GET-Uri$url-Headers$Headers + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[调用RestMethod],WebException +FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


请帮助我解决此问题

您可能需要通过Azure AD授予应用程序对Azure数据目录的访问权限


请注意,这可能需要管理员批准才能完成

您是使用服务主体、应用程序还是广告用户进行连接?以下是用于连接的详细信息AAD图形API$accessToken=“.\Token.txt”$Output=“.\Output.json”#应用程序和租户配置$clientId=“****************************************”$resourceId=“$redirectUri=新对象系统.uri(“)$login=”“您好,感谢您的回复。我们拥有访问azure数据目录的权限,但我们仍然面临同样的问题。
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ("{0}" -f $login)

$authenticationResult = $authContext.AcquireToken($ResourceId, $ClientId, $redirectUri, $PromptBehavior);

($token = $authenticationResult.AccessToken) | Out-File $accessToken

$authContext.AcquireToken($ResourceId, $ClientId, $redirectUri, $PromptBehavior)


$headers = @{ 

    "Authorization" = ("Bearer {0}" -f $token);

}

$url = "https://api.azuredatacatalog.com/catalogs/DefaultCatalog/search/search?searchTerms=name:=Orders&count=10&api-version=2016-03-30"

$response = Invoke-RestMethod -Method GET -Uri $url -Headers $headers