2017-07-28 7 views
1

IISからすべてのWebサイトを取得し、それらをループし、ディレクトリツリー内のすべてのイメージを検出してデータをExcelファイルに設定し、ファイル。IISからAzureにWebサイトを取得するためのPowershellスクリプトの変更

ここで、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によって要求されたよう

、ここにファイルをクーズーレストAPIを介して紺碧に接続すると、取得するためのコードのスニペットは、(私が持っています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" 

彼の行には、データがどのように見えるかを少しだけ示すためのダミーの処理コードがあります。ここで、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のクーズーが提供する使用する必要がありますファイルを一覧表示するには:「必要な情報を抽出するために、そのリストを反復処理するには、Get-AzureRmWebAppと」 https://github.com/projectkudu/kudu/wiki/REST-API

+0

これは私がやったことです。 – crackedcornjimmy

+0

あなたが気にしないなら、あなたが作ったスクリプトを投稿できますか、それはむしろ役に立つと思います。 –

+0

確かです。私はそれのスニペットを投稿します。この時点で、Kudu APIには再帰の概念がないため、最後の子ディレクトリまでクエリを実行できるように再帰関数を作成する必要があります。しかしそれは単なるコードなので、Azureサイトに接続する方法とは関係ありません。 – crackedcornjimmy

1

これはおそらく、次のステップに分けることになります。

  1. 使用Login-AzureRmAccountすべて「サイト」を見つけるために、リソースタイプMicrosoft.Web/sitesタイプでFind-AzureRmResourceを使用
  2. 先Azureのサブスクリプションにログインするにはあなたのサブスクリプションで
  3. Get-AzureRmWebAppでそのリストを反復して必要な情報を抽出します。
+0

を - サイトから画像ファイルを取得するにはどうすればいいですか? – crackedcornjimmy

関連する問題