-->

Kudu REST API Command endpoint error when executin

2019-09-03 08:26发布

问题:

When trying to execute the POST to /api/command according to this description the following error occurs:

PS C:\> $Result.Error
remove-item : The Win32 internal error "The handle is invalid" 0x6 occurred 
while getting the console mode. Contact Microsoft Customer Support Services.
At line:1 char:44
+ get-childitem * -recurse | remove-item -force
+                                            ~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Remove-Item], HostExce 
   ption
    + FullyQualifiedErrorId : GetConsoleMode,Microsoft.PowerShell.Commands.Rem 
   oveItemCommand

The piece of POSH script I'm using to perform this operation:

$json = @"
{
    "command": 'powershell.exe -command `"get-childitem * -recurse | remove-item -force`"',
    "dir" : 'site\\wwwroot',
 }
"@

$kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"

$progressPreference = "silentlyContinue"

$Result = Invoke-RestMethod -Uri $kuduApiUrl `
                        -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                        -Body $json `
                        -Method POST `
                        -ContentType "application/json"

I've found a lot of blogs specifying this is related to the interactive console output, however, setting $ProgressPreference to SilentlyContinue hadn't helped a lot.

回答1:

I can't reproudce the issue that you mentioned. I test with following code,you could refer to it.

$PublishingUsername = "`$userName"

$publishingPassword = "password"

$apiUrl = "https://webAppName.scm.azurewebsites.net/api/command"

$json = @"
{
    "command": 'powershell.exe -command `"get-childitem * -recurse | remove-item -force`"',
    "dir" : 'site\\wwwroot'
 }
"@

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $PublishingUsername, $publishingPassword)))

$Result = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method Post -Body $json -ContentType "application/json"

Test Result: I aslo check the kudu console that, all of the items under the folder site\wwwroot are deleted.



回答2:

After checking this issue on github the correct POSH command should be:

get-childitem -recurse | remove-item -recurse -force

It works well when executing directly from Kudu console or even REST API /api/command endpoint