Kod*_*ode 2 powershell cmd azure kudu azure-web-app-service
我需要修改我的 Azure Web 应用程序文件的内容,例如 Web.config 和文本文件。使用 Kudu 命令行 API,我可以使用以下内容创建目录或处理对象:
$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
Run Code Online (Sandbox Code Playgroud)
如何通过 Kudu Command API 修改文件?我的理想状态是使用以下内容通过命令行 API 执行 PowerShell:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
Run Code Online (Sandbox Code Playgroud)
当我在 Kudu 界面的 CMD 调试控制台中输入此命令时,上述命令有效,但我需要通过 API 调用它。我尝试了以下方法:
$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
Run Code Online (Sandbox Code Playgroud)
但是,它不会编辑文件,而是抛出以下错误:
无法将 Newtonsoft.Json.Linq.JObject 转换为 Newtonsoft.Json.Linq.JToken
这看起来不对:
command = powershell.exe -command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
Run Code Online (Sandbox Code Playgroud)
您是在尝试运行这个 powershell 命令客户端还是 Kudu 端?我猜是 Kudu,在这种情况下你需要逃避它。例如
command = "powershell.exe -command `"(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt`""
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2854 次 |
最近记录: |