【问题标题】:How to import csv replace characters and export csv that is saved in blob in powershell如何在powershell中导入csv替换字符并导出保存在blob中的csv
【发布时间】:2019-05-31 21:00:43
【问题描述】:

我对 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)"

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这样解决了: #从本地目录获取文件进行替换 $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
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多