Kudu REST API Command endpoint error when executing powershell

后端 未结 3 1244
情书的邮戳
情书的邮戳 2021-01-25 01:46

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         


        
相关标签:
3条回答
  • 2021-01-25 02:02

    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

    0 讨论(0)
  • 2021-01-25 02:08

    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.

    0 讨论(0)
  • 2021-01-25 02:10

    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.

    0 讨论(0)
提交回复
热议问题