查找文件现在,你扔出去找-ChildItem -Recurse的超级简单的答案之前,这是我唯一的问题:如何通过目录树递归使用PowerShell
我远程连接使用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.
}
}
}
如何我写的递归函数?
Up one ..对如何在没有Get-ChildItem的情况下执行此操作感兴趣。一个自定义函数可以有额外的参数,在正确的情况下非常有用 – cet51
你已经演示了如何编写和调用函数,并且你已经写了评论,你需要做什么 - “##添加图像文件数据到一个集合*“ - 我想你知道如何做这个位? “*#我现在需要再次调用Api与Url *” - 这一点?所以你的问题真的“解释递归给我?”有各种语言的递归目录遍历示例 - https://stackoverflow.com/q/24783862/478656或https://rosettacode.org/wiki/Walk_a_directory/递归 - 哪种方法可以执行递归漫游模式或另一个 – TessellatingHeckler
,这个模式或多或少是你在你的评论中写的,例如函数递归 - 步行{将文件添加到集合;对于每个目录:Recursive-Walk $ directory}; “*我不能忘记根目录中的其他目录*” - 你不能忘记它们,它们被保存在运行代码的结构中,就像'1..10 | foreach {process-number $ _}'在函数调用时不会忘记数字 – TessellatingHeckler