2017-06-26 6 views
0

Web.configやテキストファイルなど、自分のAzure Web Appのファイルの内容を変更する必要があります。PowerShell over 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 

どのように私はクーズーコマンドAPI経由でファイルを変更できます。私は、ディレクトリを作成するか、次のようなものを使用してオブジェクトを扱うことができる午前クーズーコマンドラインAPIを使用していますか?私の理想的な状態は、次のようなものを使用して、コマンドラインAPIを介しPowerShellを実行するために、次のようになります。

powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt" 

私はクーズーインタフェースの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 

しかし、それはファイルを編集して、代わりにエラースローしません:

を Newtonsoft.Json.LinqにNewtonsoft.Json.Linq.JObjectをキャストすることはできません

答えて

3

.JTokenこれは右見ていない:

command = powershell.exe -command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt" 

このpowershellコマンドのクライアント側またはKudu側を実行しようとしていますか?私はKuduを推測しています。その場合、あなたはそれをエスケープする必要があります。例えば

command = "powershell.exe -command `"(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt`"" 
+0

私はAPIを介して2番目のオプションを疲れましたが、fooの値をbarに変更しません。これは、CMDデバッグコンソールからpowershell.exe -command "(gc myFile.txt)-replace 'foo'、 'bar' | Out-File myFile.txt 'を使って動作します。 Command APIはPowerShellの呼び出しをサポートしていますか?たぶん私はファイルへのパスをより良いと宣言する必要があります(例:D:\ home \ site \ wwwroot \ myFile.txt) – Kode

+0

それはパスでした。次のようにする必要があります:command = "powershell -command" "(gc D:\ home \ site \ wwwroot \ myFile.txt)-replace 'foo'、 'bar' |アウトファイルD:\ホーム\サイト\ wwwroot \ myFile.txt "" – Kode

関連する問題