如何使用 powershell 脚本一次在 azure table 存储中部署多个 csv 文件
How to deploy multiples csv files in azure table storage at a time using powershell script
我已经打开了新线程,因为 requested.I 相信你有我在之前分享的脚本 forum.Please 考虑该脚本并找到以下要求。
function Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string] $partitionKey,
[string] $RowKey,
[string] $Label_Value,
[string] $Lable_cost
)
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("Label_Value",$Label_Value)
$entity.Properties.Add("Label_Value",$Lable_cost)
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
$tableName = "TestTable"
$subscriptionName = "Tech Enabled Solutions"
$resourceGroupName = "abc"
$storageAccountName = "defghi"
$location = "North Central US, South Central US"
# Get the storage key for the storage account
$StorageAccountKey = "12345678"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
$csv = Import-CSV "d:\a\s\DeploymentScripts\TestTable.csv"
ForEach ($line in $csv)
{
Add-Entity -Table $table -partitionKey $line.partitionkey -rowKey $line.RowKey -Label_Value $line.Label_Value Lable_cost $line.Lable_cost
}
我的假设是,如果我可以将文件作为脚本中的参数传递,它会读取该文件并可以插入和删除该特定文件的数据到 azure storage.but 事情是目前我我正在使用此命令在 power shell 脚本中插入数据...
$csv = Import-CSV "d:\a\s\DeploymentScripts\TestTable.csv" 目前根据脚本是 testtable.csv
如果我想将不同的文件比如 testtable2.csv 传递给 powershell 脚本,我无法编写第二个脚本并保留在 VSTS 存储库中,因为我需要很多 csv 文件要在脚本的 运行 时间部署到 azure storage.so 我如何使用我目前正在 运行 的一个脚本传递不同的文件。如何实现脚本以及如何传递参数。
还有一个疑惑的朋友,我如何将多个csv文件部署到table存储中,因为每个csv文件数据的行和列都不同,每个csv都会有额外的行和额外的列file.So 我怎么能 automate/implement/change 上面的脚本使用 power shell 脚本部署多个 csv 文件,因为不是每个 csv 文件都有相同的列,有些 csv 文件可能有 3 个字段,有些 csv 文件有 5,6 ,7 等等字段..我希望你理解我的 requirement.Please 帮助我。
关于将文件路径作为参数传递,您可以为该脚本定义全局参数:
param(
[string]$filepath
)
function Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string] $partitionKey,
[string] $RowKey,
[string] $Label_Value,
[string] $Lable_cost
)
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("Label_Value",$Label_Value)
$entity.Properties.Add("Label_Value",$Lable_cost)
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
$tableName = "TestTable"
$subscriptionName = "Tech Enabled Solutions"
$resourceGroupName = "abc"
$storageAccountName = "defghi"
$location = "North Central US, South Central US"
# Get the storage key for the storage account
$StorageAccountKey = "12345678"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
$csv = Import-CSV $filepath
ForEach ($line in $csv)
{
Add-Entity -Table $table -partitionKey $line.partitionkey -rowKey $line.RowKey -Label_Value $line.Label_Value Lable_cost $line.Lable_cost
}
然后就可以在Azure PowerShell任务的Script Arguments输入框中传递参数了:-filepath $(build.sourcesdirectory)\DeploymentScripts\TestTable.csv
.
对于多个不同列的文件,可以定义一个数组对象参数,然后在其属性中包含文件路径和列,然后迭代数组对象,例如:
param(
[object[]]$fileObj
)
foreach($fo in $fileObj){
Write-Host $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($c in $cArray){
Write-Host $c
#TODO add column to table
}
#TODO insert data to cloud table per to current file
}
参数:
-fileObj @(@{"filepath"="$(build.sourcesdirectory)\DeploymentScripts\TestTable.csv";"Cols"='c1,c2'},@{"filepath"="$(build.sourcesdirectory)\DeploymentScripts\TestTable2.csv";"Cols"='c3,c2'})
更新:
foreach($fo in $fileObj){
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
参数:
@(@{"filepath"="data.csv";"Cols"="Col1,Col2,Col3"},@{"filepath"="data2.csv";"Cols"="Col1,Col6,Col7,Col8"})
csv 格式的示例数据:
data.csv:
partitionkey,rowKey,Col1,Col2,Col3
p1,r1,one,two,three
p2,r2,example1,example2,example3
data2.csv:
partitionkey,rowKey,Col1,Col6,Col7,Col8
p1,r1,one,two,three,four
p2,r2,example1,example2,example3,example4
更新:
param(
[object[]]$fileObj
)
$storageAccountName = "XXX"
$tableName="XXX"
# Get the storage key for the storage account
$StorageAccountKey = "XXX"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
foreach($fo in $fileObj){
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
更新:
param(
[object[]]$fileObj
)
$storageAccountName = "XXX"
$tableName="XXX"
# Get the storage key for the storage account
$StorageAccountKey = "XXX"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
foreach($fo in $fileObj){
$table = Get-AzureStorageTable -Name $fo.tableName -Context $ctx -ErrorAction Ignore
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
参数:@(@{"filepath"="data.csv";"Cols"="Col1,Col2,Col3";"tableName"="table1"},@{"filepath"="data2.csv";"Cols"="Col1,Col6,Col7,Col8";"tableName"="table2"})
我已经打开了新线程,因为 requested.I 相信你有我在之前分享的脚本 forum.Please 考虑该脚本并找到以下要求。
function Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string] $partitionKey,
[string] $RowKey,
[string] $Label_Value,
[string] $Lable_cost
)
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("Label_Value",$Label_Value)
$entity.Properties.Add("Label_Value",$Lable_cost)
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
$tableName = "TestTable"
$subscriptionName = "Tech Enabled Solutions"
$resourceGroupName = "abc"
$storageAccountName = "defghi"
$location = "North Central US, South Central US"
# Get the storage key for the storage account
$StorageAccountKey = "12345678"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
$csv = Import-CSV "d:\a\s\DeploymentScripts\TestTable.csv"
ForEach ($line in $csv)
{
Add-Entity -Table $table -partitionKey $line.partitionkey -rowKey $line.RowKey -Label_Value $line.Label_Value Lable_cost $line.Lable_cost
}
我的假设是,如果我可以将文件作为脚本中的参数传递,它会读取该文件并可以插入和删除该特定文件的数据到 azure storage.but 事情是目前我我正在使用此命令在 power shell 脚本中插入数据...
$csv = Import-CSV "d:\a\s\DeploymentScripts\TestTable.csv" 目前根据脚本是 testtable.csv
如果我想将不同的文件比如 testtable2.csv 传递给 powershell 脚本,我无法编写第二个脚本并保留在 VSTS 存储库中,因为我需要很多 csv 文件要在脚本的 运行 时间部署到 azure storage.so 我如何使用我目前正在 运行 的一个脚本传递不同的文件。如何实现脚本以及如何传递参数。
还有一个疑惑的朋友,我如何将多个csv文件部署到table存储中,因为每个csv文件数据的行和列都不同,每个csv都会有额外的行和额外的列file.So 我怎么能 automate/implement/change 上面的脚本使用 power shell 脚本部署多个 csv 文件,因为不是每个 csv 文件都有相同的列,有些 csv 文件可能有 3 个字段,有些 csv 文件有 5,6 ,7 等等字段..我希望你理解我的 requirement.Please 帮助我。
关于将文件路径作为参数传递,您可以为该脚本定义全局参数:
param(
[string]$filepath
)
function Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string] $partitionKey,
[string] $RowKey,
[string] $Label_Value,
[string] $Lable_cost
)
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("Label_Value",$Label_Value)
$entity.Properties.Add("Label_Value",$Lable_cost)
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
$tableName = "TestTable"
$subscriptionName = "Tech Enabled Solutions"
$resourceGroupName = "abc"
$storageAccountName = "defghi"
$location = "North Central US, South Central US"
# Get the storage key for the storage account
$StorageAccountKey = "12345678"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
$csv = Import-CSV $filepath
ForEach ($line in $csv)
{
Add-Entity -Table $table -partitionKey $line.partitionkey -rowKey $line.RowKey -Label_Value $line.Label_Value Lable_cost $line.Lable_cost
}
然后就可以在Azure PowerShell任务的Script Arguments输入框中传递参数了:-filepath $(build.sourcesdirectory)\DeploymentScripts\TestTable.csv
.
对于多个不同列的文件,可以定义一个数组对象参数,然后在其属性中包含文件路径和列,然后迭代数组对象,例如:
param(
[object[]]$fileObj
)
foreach($fo in $fileObj){
Write-Host $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($c in $cArray){
Write-Host $c
#TODO add column to table
}
#TODO insert data to cloud table per to current file
}
参数:
-fileObj @(@{"filepath"="$(build.sourcesdirectory)\DeploymentScripts\TestTable.csv";"Cols"='c1,c2'},@{"filepath"="$(build.sourcesdirectory)\DeploymentScripts\TestTable2.csv";"Cols"='c3,c2'})
更新:
foreach($fo in $fileObj){
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
参数:
@(@{"filepath"="data.csv";"Cols"="Col1,Col2,Col3"},@{"filepath"="data2.csv";"Cols"="Col1,Col6,Col7,Col8"})
csv 格式的示例数据:
data.csv:
partitionkey,rowKey,Col1,Col2,Col3
p1,r1,one,two,three
p2,r2,example1,example2,example3
data2.csv:
partitionkey,rowKey,Col1,Col6,Col7,Col8
p1,r1,one,two,three,four
p2,r2,example1,example2,example3,example4
更新:
param(
[object[]]$fileObj
)
$storageAccountName = "XXX"
$tableName="XXX"
# Get the storage key for the storage account
$StorageAccountKey = "XXX"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
foreach($fo in $fileObj){
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
更新:
param(
[object[]]$fileObj
)
$storageAccountName = "XXX"
$tableName="XXX"
# Get the storage key for the storage account
$StorageAccountKey = "XXX"
# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
foreach($fo in $fileObj){
$table = Get-AzureStorageTable -Name $fo.tableName -Context $ctx -ErrorAction Ignore
Write-Host $fo.filepath
$csv = Import-CSV $fo.filepath
$cArray=$fo.Cols.split(",")
foreach($line in $csv)
{
Write-Host "$($line.partitionkey), $($line.rowKey)"
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey
foreach($c in $cArray){
Write-Host "$c,$($line.$c)"
$entity.Properties.Add($c,$line.$c)
}
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}
}
参数:@(@{"filepath"="data.csv";"Cols"="Col1,Col2,Col3";"tableName"="table1"},@{"filepath"="data2.csv";"Cols"="Col1,Col6,Col7,Col8";"tableName"="table2"})