【问题标题】:Changing Powershell Script for getting website from IIS to Azure更改用于将网站从 IIS 获取到 Azure 的 Powershell 脚本
【发布时间】:2017-07-28 19:38:23
【问题描述】:

我编写了一个简单的 Powershell 脚本,它从 IIS 中抓取所有网站,循环访问它们,发现目录树中的所有图像,然后将数据设置为 Excel 文件并保存文件。

现在,我需要更改它以连接到 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 的要求,这里是通过 Kudu 的 Rest Api 连接到 Azure 并获取文件的代码 sn-p(为了清楚起见,我复制并粘贴了 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"

在这一行之后,我们只有虚拟处理代码来显示#the data 的样子。在这里,我们需要找到 wwwroot 中的所有目录,再次点击#the API,在这些目录中找到目录,再次点击 API,等等,#until we're done。

    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
}

【问题讨论】:

    标签: powershell azure iis azure-web-app-service


    【解决方案1】:

    ARM API 只允许您访问管理平面或资源的元数据。

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

    【讨论】:

    • 这就是我最终要做的。
    • 如果你不介意的话,能不能把你最终制作的脚本贴出来,我觉得会很有用。
    • 当然可以。我会发布它的sn-p。此时,Kudu API 还没有递归的概念,所以需要创建一个递归函数,以便能够向下查询到最后一个子目录。但这只是代码,与如何连接到 Azure 站点无关。
    【解决方案2】:

    这可能会分解为以下步骤:

    1. 使用 Login-AzureRmAccount 登录目标 Azure 订阅
    2. 使用资源类型为Microsoft.Web/sitesFind-AzureRmResource 查找订阅中的所有“站点”
    3. 使用Get-AzureRmWebApp 遍历该列表以提取所需信息。

    【讨论】:

    • “使用 Get-AzureRmWebApp 遍历该列表以提取所需信息。” - 知道如何从网站获取图像文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多