如何通过目录树递归来查找带有Powershell的文件

时间:2017-08-07 16:18:37

标签: powershell azure recursion kudu

现在,在你抛出Get-ChildItem -Recurse的超级简单答案之前,这是我唯一的问题:

我使用Powershell远程连接Azure,抓取站点(插槽)列表,然后使用Kudu API循环遍历站点并查找目录结构中的所有图像。由于Kudu没有递归的概念,我必须建立自己的递归函数来从根目录中获取所有图像,然后递归所有子项和孩子等,以便在这些目录中找到图像文件。

以下是我连接Azure并获取根目录的代码:

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 [Subscription Name]

#Get the resource group name
$resourceGroupName = [Resource Group Name]

#Get the WebApp name
$Resources = Find-AzureRmResource -ResourceType Microsoft.Web/sites -ResourceGroupNameContains $resourceGroupName

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)       
        {
            #Add image file data to a collection
        }

        If ($Item.mime -eq "inode/directory")
        {
            #So, here is where I need to recurse...
            #The mime type of inode/directory means it's a directory ;)
            #I now need to call the Api again with the Url and get the contents of the current directory and rinse and repeat until done
            #But I cannot forget about the other directories in the root directory and their children.
        }
    }
}

如何编写递归函数?

1 个答案:

答案 0 :(得分:2)

我会写如下。有些是伪代码,因为我不熟悉PS语法或关键字:

    function Collect-Files($apiUrl, $creds, $currentDir)
{
    $list = Invoke-RestMethod -Uri $apiUrl/$currentDir/ -Headers @{Authorization=$creds} -Method GET -ContentType "application/json"

    If($MATCHLIST -eq $null)
    {
        $MATCHLIST = @() #initialize array
    }


    ForEach($Item in $list)
    {
        If($MimeTypes -Contains $Item.mime)       
        {
            #Add image file data to a collection
            $MATCHLIST += $Item #add to array
        }

        If ($Item.mime -eq "inode/directory")
        {
            $nextDir = $Item.name
            $MATCHLIST = Collect-Files $apiUrl $creds $currentDir/$nextDir
        }
    }

    return ($MATCHLIST)
} 

然后,您之前的代码将调用此函数,如下所示:

    #Get the WebApp name
$Resources = Find-AzureRmResource -ResourceType Microsoft.Web/sites -ResourceGroupNameContains "Nav-Inventory"

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/"

    #Now get the list of files in the wwwroot directory
    $InitialList = Invoke-RestMethod -Uri $ApiUrl -Headers @{Authorization=$publishingCredentialsHeader} -Method GET -ContentType "application/json"

    $MATCHES += Collect-Files $ApiUrl $publishingCredentialsHeader "wwwroot"
}

基本递归。