2016-12-29 73 views
0

需要在自動化過程中刪除集合。 嘗試執行下面的腳本。讓操作正常工作,但刪除操作失敗,並顯示「(401)Unathorized」錯誤。這是奇怪的原因刪除收集不需要額外的標題。有人可以給出一個提示,有什麼不對?Azure DocumentDB Rest API PowerShell刪除集合401 Unathorized

$accountName = 'someaccountname' 
$connectionKey = 'masterkey' 
$collectionName = 'mycollection' 
$databaseName = 'mydatabase' 

function GetKey([System.String]$Verb = '',[System.String]$ResourceId = '', 
     [System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') { 
    $keyBytes = [System.Convert]::FromBase64String($masterKey) 
    $text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "`n") 
    $body =[Text.Encoding]::UTF8.GetBytes($text) 
    $hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes) 
    $hash = $hmacsha.ComputeHash($body) 
    $signature = [System.Convert]::ToBase64String($hash) 
    [System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature)) 
} 
function BuildHeaders([string]$action = "get",[string]$resType, [string]$resourceId){ 
    $authz = GetKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $connectionKey 
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" 
    $headers.Add("Authorization", $authz) 
    $headers.Add("x-ms-version", '2015-12-16') 
    $headers.Add("x-ms-date", $apiDate) 
    $headers 
} 
function GetUTDate() { 
    $date = get-date 
    $date = $date.ToUniversalTime(); 
    return $date.ToString("r", [System.Globalization.CultureInfo]::InvariantCulture); 
} 
function GetDatabases() { 
    $uri = $rootUri + "/dbs" 
    $hdr = BuildHeaders -resType dbs 
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $hdr 
    $response.Databases 
    Write-Host ("Found " + $Response.Databases.Count + " Database(s)") 
} 
function GetCollections([string]$dbname){ 
    $uri = $rootUri + "/" + $dbname + "/colls" 
    $hdr = BuildHeaders -resType colls -resourceId $dbname 
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $hdr 
    $response.DocumentCollections 
    Write-Host ("Found " + $Response.DocumentCollections.Count + " DocumentCollection(s)") 
} 
function DeleteCollection([string]$dbname){ 
    $uri = $rootUri + "/" + $dbname + "/colls" + "/" + $collectionName 
    $hdrs = BuildHeaders -action DELETE -resType colls -resourceId $collectionName 
    $response = Invoke-RestMethod -Uri $uri -Method Delete -Headers $hdrs 
    Write-Host "DELETE $uri" 
} 
$rootUri = "https://" + $accountName + ".documents.azure.com" 
write-host ("Root URI is " + $rootUri) 

#validate arguments 
$apiDate = GetUTDate 
$db = GetDatabases | where { $_.id -eq $databaseName } 

if ($db -eq $null) { 
    write-error "Could not find database in account" 
    return 
} 

$dbname = "dbs/" + $databaseName 
$collection = GetCollections -dbname $dbname | where { $_.id -eq $collectionName } 

if($collection -eq $null){ 
    write-error "Could not find collection in database" 
    return 
} 
Write-Host 
$Delete = DeleteCollection -dbname $dbname | where { $_.id -eq $collectionName } 

回答

1

通常情況下,無論是Authorization還是x-ms-date標頭未設置(或授權標頭的授權令牌無效),401 unauthorized error都會返回。我使用fiddler捕獲請求並檢查響應,並且我發現授權和x-ms-date標頭都已設置,所以看起來授權標頭設置爲無效授權令牌。根據您的代碼,我做了一些更改,並且該功能可以在我身邊正常工作。

function DeleteCollection([string]$dbname){ 
    $uri = $rootUri + "/" + $dbname + "/colls" + "/" + $collectionName 
    $collectionName = $dbname + "/colls" + "/" + $collectionName 
    $hdrs = BuildHeaders -action DELETE -resType colls -resourceId $collectionName 
    #Write-Host "resourceId $collectionName" 
    $response = Invoke-RestMethod -Uri $uri -Method Delete -Headers $hdrs 
    Write-Host "resource $collectionName" 
    Write-Host "DELETE $uri" 
} 

$集合名應該dbs/{yourdbname}/colls/{yourcollectionname}

enter image description here

+0

謝謝你很多。這是工作。 –

1

如果存在掛起的操作,則可能發生這種情況。重試直到它成功或在刪除之前添加延遲。對我們來說,半秒就足以保證我們再也看不到這一點。