Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将CSV文件上载到azure表存储不会上载所有项目_Powershell_Csv_Azure Table Storage_Import Csv - Fatal编程技术网

Powershell 将CSV文件上载到azure表存储不会上载所有项目

Powershell 将CSV文件上载到azure表存储不会上载所有项目,powershell,csv,azure-table-storage,import-csv,Powershell,Csv,Azure Table Storage,Import Csv,我正在尝试通过powershell脚本将CSV文件上载到Azure表存储。CSV有4776条记录,但实际上只有大约3042项迁移到Azure表存储。我做错了什么/有什么可以做得不同?这是我的密码: function Add-Entity() { [CmdletBinding()] param ( $table, [string] $partitionKey, [string] $rowKey, [string] $propOne, [stri

我正在尝试通过powershell脚本将CSV文件上载到Azure表存储。CSV有4776条记录,但实际上只有大约3042项迁移到Azure表存储。我做错了什么/有什么可以做得不同?这是我的密码:

function Add-Entity()
{
 [CmdletBinding()]

 param
 (
    $table, 
    [string] $partitionKey, 
    [string] $rowKey, 
    [string] $propOne,
    [string] $proptwo

 )
 $assemblySN = $table.CloudTable.GetType().Assembly.FullName
 $entity = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,$assemblySN" -ArgumentList $partitionKey, $rowKey
 $entity.Properties.Add("FirstProperty",$propOne)
 $entity.Properties.Add("SecondProperty",$propTwo)

 $result = $table.CloudTable.ExecuteAsync((invoke-expression "[Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::InsertOrReplace(`$entity)"))
}

Clear-Host
$subscriptionName = "mysubscription" 
$resourceGroupName = "myrg"
$storageAccountName = "mystorage" 
$location = "East US"
$tableName = "mytable"

# Log on to Azure and set the active subscription
Select-AzureRmSubscription -SubscriptionName $subscriptionName

# Get the storage key for the storage account
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]

# Get a storage context
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx 

$csv = Import-CSV <file path>

ForEach ($line in $csv)   
{
 Add-Entity -Table $table -partitionKey $line.Partitionkey -rowKey $line.Rowkey -FirstProperty $line.propOne -SecondProperty $line.proptwo 
}
函数Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string]$partitionKey,
[字符串]$rowKey,
[字符串]$propOne,
[string]$proptwo
)
$assemblySN=$table.CloudTable.GetType().Assembly.FullName
$entity=新对象-TypeName“Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,$assemblySN”-ArgumentList$partitionKey,$rowKey
$entity.Properties.Add(“FirstProperty”,$propOne)
$entity.Properties.Add(“SecondProperty”、$propTwo)
$result=$table.CloudTable.ExecuteAsync((调用表达式“[Microsoft.WindowsAzure.Storage.table.TableOperation,$assemblySN]::InsertOrReplace(`$entity)”)
}
清除主机
$subscriptionName=“mysubscription”
$resourceGroupName=“myrg”
$storageAccountName=“mystorage”
$location=“美国东部”
$tableName=“mytable”
#登录Azure并设置活动订阅
选择AzureRmSubscription-SubscriptionName$SubscriptionName
#获取存储帐户的存储密钥
$storageAccountKey=(获取AzureRmStorageAccountKey-ResourceGroupName$ResourceGroupName-Name$storageAccountName)。值[0]
#获取存储上下文
$ctx=新AzureStorageContext-StorageAccountName$StorageAccountName-StorageAccountKey$StorageAccountKey
#获取对该表的引用
$table=获取AzureStorageTable-名称$tableName-上下文$ctx
$csv=导入csv
ForEach($csv中的行)
{
添加实体-Table$Table-partitionKey$line.partitionKey-rowKey$line.rowKey-FirstProperty$line.propOne-SecondProperty$line.proptwo
}

如果您确定.csv文件中没有[Partitionkey+Rowkey]的复制,那么可能的原因是
分区键或行键中有一些
无效字符。通过使用脚本,如果表中包含无效字符,则不会将这些项添加到表中

请检查您的
分区键
行键
中是否包含以下字符

无效字符的屏幕截图:


根据我的测试,我们可以使用以下PowerShell脚本将csv文件导入Azure表存储

$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$table = Get-AzureStorageTable -Name $tableName -Context $ctx 

$CsvContents = Import-Csv -Path $Path
$CsvHeaders = ($CsvContents[0] | Get-Member -MemberType NoteProperty).Name | Where{$_ -ne "RowKey" -and $_ -ne "PartitionKey"}

Foreach($CsvContent in $CsvContents)
{
    $PartitionKey = $CsvContent.PartitionKey
    $RowKey = $CsvContent.RowKey
    $Entity = New-Object "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity" "$PartitionKey", "$RowKey"

    Foreach($CsvHeader in $CsvHeaders)
    {
        $Value = $CsvContent.$CsvHeader
        $Entity.Properties.Add($CsvHeader, $Value)
    }
    Write-Verbose "Inserting the entity into table storage."
    $result = $Table.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($Entity))
}

您的csv文件中是否有[PartitionKey+RowKey]的任何组合被重复?没有[PartitionKey+RowKey]的复制实际上您是对的……这就是发生这种情况的原因吗?删除复制后,我得到了3449条记录。尝试通过上面的脚本再次上传优化的csv,但只上传了3004条记录。您知道发生这种情况的其他原因吗?在重复[PK+RK]的情况下,“添加实体”将引发(409)冲突。我想,还有其他一些问题发生了,所以只需验证$csv是否具有所有记录计数。