Search code examples
restpowershellazurekudu

Kudu REST API Command endpoint error when executing powershell


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.


Solution

  • 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