Created
August 8, 2019 14:26
-
-
Save phaniav/6ada460d24cafedd4f6803da66e575d7 to your computer and use it in GitHub Desktop.
Kudu utility PowerShell functions - https://jeffdarchuk.com/2019/03/12/remotely-triggering-sitecore-operations-in-azure/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){ | |
if ([string]::IsNullOrWhiteSpace($slotName) -or $slotName.ToLower() -eq "production"){ | |
$resourceType = "Microsoft.Web/sites/config" | |
$resourceName = "$webAppName/publishingcredentials" | |
} | |
else{ | |
$resourceType = "Microsoft.Web/sites/slots/config" | |
$resourceName = "$webAppName/$slotName/publishingcredentials" | |
} | |
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force | |
return $publishingCredentials | |
} | |
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){ | |
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName | |
$ret = @{} | |
$ret.header = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword)))) | |
$ret.url = $publishingCredentials.Properties.scmUri | |
return $ret | |
} | |
function Get-FileFromWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath){ | |
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName | |
$kuduApiAuthorisationToken = $KuduAuth.header | |
$kuduApiUrl = $KuduAuth.url + "/api/vfs/site/wwwroot/$kuduPath" | |
Write-Host " Downloading File from WebApp. Source: '$kuduApiUrl'." -ForegroundColor DarkGray | |
$tmpPath = "$($env:TEMP)\$([guid]::NewGuid()).xml" | |
$null = Invoke-RestMethod -Uri $kuduApiUrl ` | |
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` | |
-Method GET ` | |
-ContentType "multipart/form-data" ` | |
-OutFile $tmpPath | |
$ret = Get-Content $tmpPath | Out-String | |
Remove-Item $tmpPath -Force | |
return $ret | |
} | |
function Write-FileToWebApp($resourceGroupName, $webAppName, $slotName = "", $fileContent, $kuduPath){ | |
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName | |
$kuduApiAuthorisationToken = $KuduAuth.header | |
$kuduApiUrl = $KuduAuth.url + "/api/vfs/site/wwwroot/$kuduPath" | |
Write-Host " Writing File to WebApp. Destination: '$kuduApiUrl'." -ForegroundColor DarkGray | |
Invoke-RestMethod -Uri $kuduApiUrl ` | |
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` | |
-Method Put ` | |
-ContentType "multipart/form-data"` | |
-Body $fileContent | |
} | |
function Write-FileFromPathToWebApp($resourceGroupName, $webAppName, $slotName = "", $filePath, $kuduPath){ | |
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName | |
$kuduApiAuthorisationToken = $KuduAuth.header | |
$kuduApiUrl = $KuduAuth.url + "/api/vfs/site/wwwroot/$kuduPath" | |
Write-Host " Writing File to WebApp. Destination: '$kuduApiUrl'." -ForegroundColor DarkGray | |
Invoke-RestMethod -Uri $kuduApiUrl ` | |
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` | |
-Method Put ` | |
-ContentType "multipart/form-data"` | |
-InFile $filePath | |
} | |
function Write-ZipToWebApp($resourceGroupName, $webAppName, $slotName = "", $zipFile, $kuduPath){ | |
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName | |
$kuduApiAuthorisationToken = $KuduAuth.header | |
$kuduApiUrl = $KuduAuth.url + "/api/zip/site/wwwroot/$kuduPath" | |
Write-Host " Writing Zip to WebApp. Destination: '$kuduApiUrl'." -ForegroundColor DarkGray | |
Invoke-RestMethod -Uri $kuduApiUrl ` | |
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` | |
-Method Put ` | |
-ContentType "multipart/form-data"` | |
-InFile $zipFile | |
} | |
function Remove-FileFromWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath){ | |
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName | |
$kuduApiAuthorisationToken = $KuduAuth.header | |
$kuduApiUrl = $KuduAuth.url + "/api/vfs/site/wwwroot/$kuduPath" | |
Write-Host " Writing File to WebApp. Destination: '$kuduApiUrl'." -ForegroundColor DarkGray | |
Invoke-RestMethod -Uri $kuduApiUrl ` | |
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` | |
-Method Delete ` | |
-ContentType "multipart/form-data" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment