简体   繁体   English

Azure Runbook 不删除文件共享中的文件

[英]Azure Runbook not deleting Files in Fileshare

I am trying to to use Azure Powershell Runbooks to delete files out of the Azure Fileshare.我正在尝试使用 Azure Powershell Runbook 从 Azure 文件共享中删除文件。 There are no errors returned, but the file is not deleted.没有返回错误,但文件没有被删除。 The Automation Account has a Run As account setup that is not expired or anything and the script works if I run it from my local machine.自动化帐户有一个未过期或任何内容的运行方式帐户设置,如果我从我的本地计算机运行它,脚本就可以工作。 Looking for some advise on this.寻找一些建议。

$ctx = New-AzureStorageContext -StorageAccountName "" -StorageAccountKey "" 
$shareName = ""
$directoryPath = ".cloudconsole"
$DirIndex = 0
$day = 1
$startdate = (Get-Date).AddDays(-180)
$endDate = (Get-date).AddDays(-32)

$dirsToList = New-Object System.Collections.Generic.List[System.Object]


$shareroot = Get-AzureStorageFile -ShareName $shareName -Path $directoryPath -context $ctx 
$dirsToList += $shareroot 
While ($dirsToList.Count -gt $DirIndex)
{
 $dir = $dirsToList[$DirIndex]
 $DirIndex ++
 $fileListItems = $dir | Get-AzureStorageFile
 $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
 $dirsToList += $dirsListOut
 $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}

 foreach($file in $files)
 {
   
     $task = $file.CloudFile.FetchAttributesAsync()
     $task.Wait()

   
        if ($file.CloudFile.Properties.LastModified -ge $startdate -and $file.CloudFile.Properties.LastModified -ge $endDate  )

     {
     if ($file.CloudFile.Properties.LastModified.day -ne '01'  )
     
        {
         $file | Remove-AzureStorageFile
         }
     }
        if ($file.CloudFile.Properties.LastModified -lt $startdate)
        
     {
     
        
         $file | Remove-AzureStorageFile 
     }
   
 }


 }

BoxJumper : As kavyasaraboju-MT referred, below piece of script should help you. BoxJumper :正如kavyasaraboju-MT所提到的,下面的一段脚本应该可以帮助你。

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
$shareName = <shareName>
$startdate = (Get-Date).AddDays(-180)
$endDate = (Get-date).AddDays(-32)
    
 $DirIndex = 0
 $dirsToList = New-Object System.Collections.Generic.List[System.Object]
    
 # Get share root Dir
 $shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx 
 $dirsToList += $shareroot 
    
 # List files recursively and remove file older than 14 days 
 While ($dirsToList.Count -gt $DirIndex)
 {
     $dir = $dirsToList[$DirIndex]
     $DirIndex ++
     $fileListItems = $dir | Get-AzStorageFile
     $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
     $dirsToList += $dirsListOut
     $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
    
     foreach($file in $files)
     {
         # Fetch Attributes of each file and output
         $task = $file.CloudFile.FetchAttributesAsync()
         $task.Wait()
    
         # remove file if it's modified between after last 180 days and before last 30 Days
         if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-32) -and $file.CloudFile.Properties.LastModified -ge (Get-Date).AddDays(-180))
         {
             ## print the file LMT
             # $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } } 
    
             # remove file
             $file | Remove-AzStorageFile
         }
     }
     #Debug log
     # Write-Host  $DirIndex $dirsToList.Length  $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString() 
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azcopy:使用 Azcopy 10 将文件复制到 Azure 文件共享 - Azcopy: Copying files to an Azure Fileshare using Azcopy 10 Matillion:从 Azure Blob 存储容器和 Windows 文件共享中删除文件 - Matillion: Delete files from Azure Blob Storage Container and Windows Fileshare RabbitMQ 与 Azure 文件共享的持久性 - RabbitMQ Persistence with Azure Fileshare 从 Azure Runbook 创建 Azure AD 用户 - Creating Azure AD user from Azure Runbook 将应用服务中的数据复制到 azure 文件共享 - copy data from app service to azure fileshare Azure 存储帐户文件共享内容保留期 - Azure storage account fileshare contents retention period Azure Devops 管道下载文件共享工件任务 - Azure Devops Pipeline Download Fileshare Artifacts Task Azure Fileshare - 在没有备份服务的情况下恢复快照 - Azure Fileshare - Restore snapshot without backup service Azure 自动化运行手册 Python - webhook 数据 - Azure automation runbook Python - webhook data Azure 自动化 Runbook 工作流丢失 AzContext - Azure Automation Runbook Workflow looses AzContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM