2017-07-28 59 views
1

我編寫了一個簡單的Powershell腳本,它從IIS抓取所有網站,遍歷它們,發現目錄樹中的所有圖像,然後將數據設置爲Excel文件並保存文件。更改Powershell腳本以將網站從IIS獲取到Azure

現在,我需要將其更改爲連接到Azure訂閱中的所有網站。我會怎麼做?

這是當前腳本:

# 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() 

編輯:

按照要求通過Byron,這裏是代碼,用於連接通過捻的REST API來Azure和獲取文件的片段(我有爲了清楚起見覆制並粘貼來自Kudu API模塊的功能):

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" 

t他的行,我們只是有虛擬處理代碼來顯示數據的一小部分。在這裏,我們需要找到wwwroot中的所有目錄,再次敲擊#API,在這些目錄中找到目錄,再次敲擊API等,#直到我們完成。

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 
} 

回答

1

ARM API將只允許您訪問管理平面或資源的元數據。

要列出的文件,你需要使用捻提供的VFS API的: https://github.com/projectkudu/kudu/wiki/REST-API

+0

這就是我最終做的。 – crackedcornjimmy

+0

如果你不介意,你可以發佈你最終制作的劇本,我認爲它會相當有用。 –

+0

當然可以。我會發布它的一個片段。在這一點上,Kudu API沒有遞歸的想法,所以你需要創建一個遞歸函數來查詢到最後一個子目錄。但這只是代碼,與如何連接到Azure站點無關。 – crackedcornjimmy