[英]How to import csv replace characters and export csv that is saved in blob in powershell
I am very new to powershell. 我对powershell很新。 I am trying to replace characters in a csv that is saved in blob storage. 我正在尝试替换blob存储中保存的csv中的字符。 Take the file from one folder, do the replace and save in another. 从一个文件夹中取出文件,进行替换并保存到另一个文件夹中。
I have 2 sets of code, one for replacing the characters and one for creating blob context. 我有两组代码,一组用于替换字符,另一组用于创建blob上下文。 However, I am unsure how to put them together to perform the task. 但是,我不确定如何将它们组合在一起来执行任务。 Meaning what do I use instead of 'import-csv' and 'set-content' and how do I tell it to do this for each file in the folder? 这意味着我使用什么而不是'import-csv'和'set-content'?如何告诉它为文件夹中的每个文件执行此操作? Any advice would be appreciated. 任何意见,将不胜感激。
$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
}
get context: 得到上下文:
$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)"
solved it this way: #Getting files from local directory to perform replace $ent = Get-ChildItem -Path $afLocalPath2 -Recurse -include aircoach .csv 这样解决了:#Getting文件从本地目录执行替换$ 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.