如何导入csv替换字符并导出保存在powershell中的blob中的csv

时间:2019-05-08 12:26:58

标签: powershell

我对Powershell非常陌生。我正在尝试替换保存在Blob存储中的csv中的字符。从一个文件夹中取出文件,进行替换,然后保存到另一个文件夹中。

我有2组代码,一组用于替换字符,一组用于创建blob上下文。但是,我不确定如何将它们放在一起执行任务。这意味着我用什么代替“ import-csv”和“ set-content”,以及如何告诉它为文件夹中的每个文件执行此操作?任何建议将不胜感激。

$dest = "C:\test\Test - Copy\"

Function RemoveStripScrape ($CsvFileName, $csvLoc)
{
    $CsvFile = $csvLoc + $CsvFileName + ".csv"
    $ExportLoc = $csvLoc + $CsvFileName + ".csv"

    $Csv = Import-Csv $CsvFile 

    $regex = '(?<!"),|,(?!")'
    $regex1 = '(?<!\r)\n'

    $Csv | Select-Object * |ConvertTo-Csv -NoTypeInformation | %{$_ -replace $regex,' '} |%{$_ -replace $regex1, ' '} | Set-Content $ExportLoc 
    }

$ens = Get-ChildItem $dest -Recurse -include Let*.csv
foreach($e in $ens)
{
    $e.Basename
    RemoveStripScrape -CsvFileName $e.BaseName -csvLoc $dest
} 

获取上下文:

$azurepw = ConvertTo-SecureString "xyz" -AsplainText -Force
$cred = New-Object -typename pscredential -argumentlist "xyz@xyz.com",$azurepw
Login-AzureRmAccount -credential $cred | Out-Null

$resourcegroup = "xyz"
$storageaccountname = "xyz"

$storageaccountkey = `
    (Get-AzureRmStorageAccountKey `
    -ResourceGroupName $resourcegroup `
    -Name $storageaccountname).Value[0]

$sourceContext = New-AzureStorageContext $storageaccountname $storageaccountkey
$container = "xyz"

$FileDate = $($(Get-Date).ToString('yyyyMMdd'))
$SourceFileName = "*activity*.csv"
$SinkFileName = "processed_$($FileDate).csv"
$BlobSourceFileNamePath = "Data/Revenue/Archive/CorethreeNew/$($SourceFileName)"
$BlobSinkFileNamePath = "Data/Revenue/Archive/CorethreeNew/Processed/$($SinkFileName)"
#$BlobArchiveSinkFileNamePath = "Data/Revenue/Archive/CorethreeNew/Archive/$($SinkFileName)"

1 个答案:

答案 0 :(得分:0)

通过以下方式解决了它:     #从本地目录获取文件以执行替换         $ ent = Get-ChildItem -Path $ afLocalPath2 -Recurse -include aircoach .csv

#Importing csv and replacing regex for each file in the local store and saving it in 
local Processed folder
foreach($e in $ent)
{
     $FileName = $e.Name
     $CsvFile = "$($afLocalPath2)$($FileName)"
     $Csv = Import-Csv $CsvFile 

     $Csv | Select-Object * |ConvertTo-Csv -NoTypeInformation | %{$_ -replace $regex,' '} |%{$_ -replace $regex1, ' '} |%{$_ -replace $regex2, ' '} | %{$_ -replace $regex3, ''}| Set-Content "$($ExportLoc)$($FileName)" -Force



   #Uploading the updated files from local Processed directory to blob
   Set-AzureStorageBlobContent -Context $sourceContext -Container $container -File "$($ExportLoc)$($FileName)" -Blob "$($BlobSinkFileNamePath)$($FileName)" -Force


}