我正在运行下面列出的脚本,该脚本在blob中获取单个文件并创建其SAS URL并将数据导出到csv文件中,现在的问题是在其初始运行中,脚本会运行并创建一个csv文件,但是,当我重新运行脚本,以在文件中附加新文件SAS并给出错误“该进程无法访问该文件,因为该文件正在被另一个进程使用”。
有人可以告诉我该怎么做以避免文件锁定?
$key=(Get-AzureRmStorageAccountKey -ResourceGroupName Test -Name testdiag).Value[0]
$storageContext=(Get-AzureRmStorageAccount -ResourceGroupName Test -Name testdiag).Context
Set-AzureRmCurrentStorageAccount -Context $storageContext
$blobCon=Get-AzureStorageBlob -Container 'customscript' -Context $storage.Context
$DesktopPath = [Environment]::GetFolderPath("Desktop")
if(test-path "$DesktopPath\testfile7.csv"){
$file="$DesktopPath\testfile7.csv"
$sheetName = "testfile7"
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
$rowMax = ($sheet.UsedRange.Rows).count
$row1,$col1 = 1,1
$filename= for ($i=1; $i -le $rowMax-1; $i++) {
$sheet.Cells.Item($row1+$i,$col1).text
}
foreach ($blob in $blobCon){
if ($filename -contains $blob.Name){
echo "File Already Exists"
}
else{
$bloburi=$blob.ICloudBlob.Container.Uri.AbsoluteUri + "/" +$blob.Name
$fileSAS=$bloburi+$sas
$fileSAS | select @{n='File Name';e={$blob.Name}},
@{n='File SAS';e={$fileSAS}} |
Export-Csv "$DesktopPath\testfile7.csv" -Append -NoTypeInformation -Force
}}}
else
{
foreach ($blob in $blobCon){
$bloburi=$blob.ICloudBlob.Container.Uri.AbsoluteUri + "/" +$blob.Name\
$fileSAS=$bloburi+$sas
$fileSAS |
select @{n='File Name';e={$blob.Name}},@{n='File SAS';e={$fileSAS}} |
Export-Csv "$DesktopPath\testfile7.csv" -Append -NoTypeInformation -Force
}
}