如何从where-object调用函数

时间:2017-11-09 10:47:11

标签: powershell azure-powershell

当我执行脚本时,没有任何内容登录Test.json

任何人都可以告诉我如何在内联查询中调用where-object中的函数吗?

function getDateOfTimeStamp($dateString) {
    [string[]] $format = @("MM/d/yyyy hh:mm:ss tt", "M/d/yyyy hh:mm:ss tt", "MM/dd/yyyy hh:mm:ss tt", "M/dd/yyyy hh:mm:ss tt")
    $result = $null
    $format.ForEach( { [DateTime] $dt = New-Object DateTime; if ([datetime]::TryParseExact($dateString, $_, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref] $dt)) { $result = $dt } })
    $result
}
$storageaccount = Get-AzureRmStorageAccount -StorageAccountName test123 -ResourceGroupName testinggroup
$table = Get-AzureStorageTable -Name testtable123
$query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery
$data = $table.CloudTable.ExecuteQuery($query)       
$data | % { EntityToObject $_ } | Select-Object PartitionKey, RowKey, DateModified| Where-Object {getDateOfTimeStamp DateModified -lt ((Get-Date).ToUniversalTime().AddDays(-10))} | ConvertTo-Json | Out-File "C:\\Test.json"

1 个答案:

答案 0 :(得分:0)

正如Mathias R. Jessen所说,您需要将getDateOfTimeStamp DateModified修改为getDateOfTimeStamp $_.DateModified

  

我们如何根据表中的上述条件删除行。

您可以使用Remove-AzureStorageTableRow删除所选的行。

在您的方案中,您可以尝试以下方式:

$removedata=$data | % { EntityToObject $_ } | Select-Object PartitionKey, RowKey, DateModified| Where-Object {getDateOfTimeStamp DateModified -lt ((Get-Date).ToUniversalTime().AddDays(-10))}
$removedata|Remove-AzureStorageTableRow -table $table

我喜欢按照脚本删除表格中的行前10天。

$rgname=""
$storagename=""
$tablename=""
$saContext = (Get-AzureRmStorageAccount -StorageAccountName $storagename -ResourceGroupName $rgname).Context
$table = Get-AzureStorageTable -Name $tablename -Context $saContext
$query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery
$data = $table.CloudTable.ExecuteQuery($query)    
$remove=$data  | Select-Object PartitionKey, RowKey, TimeStamp| Where-Object {$_.TimeStamp -lt ((Get-Date).ToUniversalTime().AddDays(-10))} 
$removedata|Remove-AzureStorageTableRow -table $table

有关此内容的详情,请参阅此博客:Working with Azure Storage Tables from PowerShell