通过Kudu Command API运行PowerShell以编辑文件 [英] Run PowerShell over Kudu Command API to Edit Files
问题描述
我需要修改Azure Web App的文件内容,例如Web.config和文本文件.使用Kudu命令行API,我可以使用以下内容创建目录或处理对象:
I need to modify the contents of files of my Azure Web App such as the Web.config and text files. Using the Kudu command line API I am able to create a directory or deal with objects using something like the following:
$username = "`$myuser"
$password = "mypass"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$apiUrl = "https://mywebapp.scm.azurewebsites.net/api/command"
$commandBody = @{
command = "md D:\home\site\wwwroot\newDirectory"
}
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody) | Out-Null
如何通过Kudu Command API修改文件?我的理想状态是使用以下命令通过命令行API执行PowerShell:
How can I modify files via the Kudu Command API? My ideal state would be to execute PowerShell over the command line API using something like the following:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
当我在Kudu界面的CMD调试控制台中输入此命令时,上述命令有效,但是我需要通过API调用此命令.我尝试了以下方法:
The above command works when I enter this in the CMD Debug Console of the Kudu interface, but I need to call this over the API. I tried the following:
$username = "`$myuser"
$password = "mypass"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$apiUrl = "https://mywebapp.scm.azurewebsites.net/api/command"
$commandBody = @{
command = powershell.exe -command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
}
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody) | Out-Null
但是,它不会编辑文件,而是引发以下错误:
However, it doesn't edit the file and instead throws an error of:
无法将Newtonsoft.Json.Linq.JObject强制转换为 Newtonsoft.Json.Linq.JToken
Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken
推荐答案
这看起来不正确:
command = powershell.exe -command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
您是否要运行此powershell命令客户端或Kudu端?我猜是Kudu,在这种情况下,您将需要对其进行转义.例如
Are you trying to run this powershell command client side or Kudu side? I'm guessing Kudu, in which case you will need to escape it. e.g.
command = "powershell.exe -command `"(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt`""
这篇关于通过Kudu Command API运行PowerShell以编辑文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!