1

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.

Sergey
  • 381
  • 6
  • 24

3 Answers3

0

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.

enter image description here

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Thanks, @Tom Sun, however, when testing on different web apps (3 in a row) the same error has occurred - [here](https://imgur.com/a/bw8QqhJ). I've slightly modified your initial script, but without any logic modifications. Can you advise which additional options - like permissions, rights etc should I check in order for my script to execute positively? I'm really stuck with this error. – Sergey Jun 07 '18 at 10:53
  • @Sergey I test it with Powershell Version 5.1.10 in the local environment. Would you mind sharing the PSversion? Do you test it locally? – Tom Sun - MSFT Jun 08 '18 at 02:44
  • If possible, you could use another local machine to test it again. – Tom Sun - MSFT Jun 08 '18 at 02:50
  • PSVersion:5.1.14393.2248. I've checked this script on the other hosts (virtual machines) - the same problem. Also, per [David's recommendation](https://github.com/projectkudu/kudu/wiki/Isolating-WebJobs-and-Deployment-script-issues), I've tried to isolate the issue and execute it on Kudu side - still the [same](https://imgur.com/a/x5skRtc). – Sergey Jun 08 '18 at 07:37
  • It seems that there is something wrong with you WebApp. Have you tried to scale up and scale down back your service plan or try to create a new WebApp in a new serviceplan? If all of that no help, I recommand that you could create [a support ticket](https://learn.microsoft.com/en-us/azure/azure-supportability/how-to-create-azure-support-request). – Tom Sun - MSFT Jun 08 '18 at 08:36
  • No, this web app isn't bugged at all. Also, I'm not sure that the scaling can be a working approach here. However, I've tested with another web app in existing app service plan, still the same error. Anyway, thanks for your efforts, I've raised an issue on github and posted the solution below. – Sergey Jun 08 '18 at 22:29
0

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

Sergey
  • 381
  • 6
  • 24
0

If the accepted answer isn't appropriate for your situation (i.e. you don't want to use -Recurse on the call to Remove-Item because that would blast an item you want to exclude from deletion, when its containing folder is recursively deleted), the following worked for me:

Get-ChildItem d:\mypath -Recurse -Exclude FileToExclude.ext | Select -ExpandProperty FullName | sort length -Descending | foreach { Remove-Item -Path "$_" -Force }

It seems that simply piping the file object to Remove-Item doesn't work because of this bug, but piping the full filename and then using that as the -Path argument, gets around it.

Zam
  • 1,121
  • 10
  • 27