gpt4 book ai didi

powershell - 如何使用 Powershell 递归通过目录树查找文件

转载 作者:行者123 更新时间:2023-12-02 23:39:42 25 4
gpt4 key购买 nike

现在,在您抛出 Get-ChildItem -Recurse 的 super 简单答案之前,这是我的独特问题:

我使用 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.
}
}
}

如何编写递归函数?

最佳答案

我会这样写。有些是伪代码,因为我不熟悉 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"
}

基本递归。

关于powershell - 如何使用 Powershell 递归通过目录树查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com