如何从where对象调用函数 [英] How to call function from where-object

查看:93
本文介绍了如何从where对象调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I'm executing the script nothing is logging into Test.json.

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

Can any one tell me how to call function from where-object in inline query?

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"

推荐答案

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

As Mathias R. Jessen said, you need modify getDateOfTimeStamp DateModified to getDateOfTimeStamp $_.DateModified

如何基于以上条件从表中删除行.

How can we delete the rows based on above condtions from table.

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

You could use Remove-AzureStorageTableRow to delete the rows that selected.

根据您的情况,您可以尝试以下操作:

On your scenario, you could try below:

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

我喜欢以下脚本来删除表中行之前的10天.

I like following script to delete 10 days before rows in a table.

$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

有关此的更多信息,请参阅此博客:

More information about this please refer to this blog:Working with Azure Storage Tables from PowerShell.

这篇关于如何从where对象调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆