Reputation: 1992
I wrote a simple Powershell script that grabs all the websites from IIS, loops over them, discovering all images in the directory tree, then sets the data to an Excel file and saves the file.
Now, I need to change it to connect to all websites in an Azure subscription. How would I do that?
Here is the current script:
# Create the Excel doc
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $True
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
# Add the Workbook
$Workbook = $Excel.Workbooks.Add()
# Name the Worksheet
$Worksheet= $Workbook.Worksheets.Item(1)
$Worksheet.Name = 'Images'
# Set the title row
$TitleRow = 1
# Now, name the columns
$Worksheet.Cells.Item($TitleRow,1) = 'ImageTitle'
$Worksheet.Cells.Item($TitleRow,2) = 'WebSite'
$Worksheet.Cells.Item($TitleRow,3) = 'MetaData'
$Worksheet.Cells.Item($TitleRow,4) = 'URL'
# Make the cells bold
$Worksheet.Cells.Item($TitleRow,1).Font.Bold = $True
$Worksheet.Cells.Item($TitleRow,2).Font.Bold = $True
$Worksheet.Cells.Item($TitleRow,3).Font.Bold = $True
$Worksheet.Cells.Item($TitleRow,4).Font.Bold = $True
# Get the list of websites
$WebSites = Get-Website | Select Name, PhysicalPath
# Initialize the looping variable
$i = 2
#loop over the sites and write to console
ForEach ($Site in $WebSites)
{
#get all files that are in the root directory -> children
$Files = Get-ChildItem $Site.physicalPath -Force -Recurse -Include *.png, *.jpg, *.jpeg, *.bmp
ForEach ($File in $Files)
{
$Excel.Cells.Item($i,1) = $File.Name
$Excel.Cells.Item($i,2) = $Site.Name
$Excel.Cells.Item($i,3) = ''
$Excel.Cells.Item($i,4) = $File.FullName
$i++
}
}
# Now, adjust the columns to fit
$UsedRange = $Worksheet.UsedRange
$UsedRange.EntireColumn.AutoFit() | Out-Null
$Workbook.SaveAs("C:\Scripts\Images.xlsx", $xlFixedFormat)
$Excel.Quit()
Edit:
As requested by Byron, here is the snippet of code for connecting to Azure through Kudu's Rest Api and getting files (I have copy and pasted the functions from the Kudu API module for clarity):
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$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
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Fill-MimeTypes(){
return @("image/gif", "image/x-icon", "image/jpeg", "image/png", "image/tiff", "image/bmp")
}
$MimeTypes = Fill-MimeTypes
[System.Collections.ArrayList]$Directories = @()
#Login to Azure Account
Login-AzureRmAccount
#Get the Azure subscription
Select-AzureRmSubscription -SubscriptionName [Your subscription name]
#Get the resource group name
####$resourceGroup = Get-AzureRmResourceGroup | where ()
$resourceGroupName = [Your resource group name]
#Get the WebApp name
$Resources = Find-AzureRmResource -ResourceType Microsoft.Web/sites -ResourceGroupNameContains [Your web app name]
ForEach($Resource in $Resources)
{
#Get the WebAppName
$WebAppName = $Resource.Name
#Now, get the publishing creds
$publishingCredentialsHeader = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $WebAppName $null
$ApiUrl = "https://$WebAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/"
#Now get the list of files in the wwwroot directory
$InitialList = Invoke-RestMethod -Uri $ApiUrl -Headers @{Authorization=$publishingCredentialsHeader} -Method GET -ContentType "application/json"
ForEach($Item in $InitialList)
{
If($MimeTypes -Contains $Item.mime)
{
Write-Host $Item.name
}
If ($Item.mime -eq "inode/directory")
{
$Directories.Add($Item.href)
}
}
}
ForEach($Directory in $Directories)
{
Write-Host $Directory
}
Upvotes: 1
Views: 214
Reputation: 1182
ARM API will only let you access the management plane, or the metadata of the resource.
To list the files you will need to use the VFS API's provided by Kudu: https://github.com/projectkudu/kudu/wiki/REST-API
Upvotes: 1
Reputation: 2513
This would probably be broken down into the following steps:
Upvotes: 1