Kudu REST API Command endpoint error when executing powershell

二次信任 提交于 2019-12-13 05:38:54

问题


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



来源:https://stackoverflow.com/questions/50710299/kudu-rest-api-command-endpoint-error-when-executing-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!