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 int
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
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.
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.