使用 powershell 过滤并通过共享点列表项目
我在下面尝试过但没有得到任何结果 不确定我是否做得很好。 我可以在 foreach 或 if 语句中进行过滤吗 提前致谢
[DateTime] $CreatedDate = $item["Created"]
$convertedCreatedDate = $CreatedDate.ToString("yyyy-MM-dd")
$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
foreach ($item in $list.items | where {$convertedCreatedDate -eq $today}) {
if ($list.items | where {$convertedCreatedDate -eq $today})
{
Write-Host $item["Created"]
}
Write-Host $item["Created"]
}
I have tried below but not getting any result back
Not sure if i'm doing this well.
Can i filter in the foreach or in my if statement
Thanks in advance
[DateTime] $CreatedDate = $item["Created"]
$convertedCreatedDate = $CreatedDate.ToString("yyyy-MM-dd")
$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
foreach ($item in $list.items | where {$convertedCreatedDate -eq $today}) {
if ($list.items | where {$convertedCreatedDate -eq $today})
{
Write-Host $item["Created"]
}
Write-Host $item["Created"]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像上面那样在
foreach
中使用复杂的表达式。我会将其包装在@()
中,以使代码更具可读性并确保结果是一个数组(长度为 0、1 或 n),例如:您还可以简化您的操作使用 DateTime 上的
Date
属性进行日期测试,例如:您还可以在
if
语句条件中放置复杂的表达式,但 PowerShell 只会评估该语句是否为$true
或$false
。 PowerShell 执行大量强制操作来尝试执行以下操作:并将其转换为布尔值。本质上,如果管道计算结果为非空结果,则结果为
$true
,否则为$false
。这可能不是您想要的。You can use a complex expression in your
foreach
as you're doing above. I would wrap it in a@()
to make the code a bit more readable and to ensure the result is an array (either length 0, 1 or n) e.g.:You can also simplify you're date testing by using the
Date
property on a DateTime e.g.:You can also put a complex expression within an
if
statement condition but PowerShell will only evaluate whether the statement is$true
or$false
. PowerShell does lots of coercion to try to take something like:And convert that to a boolean. Essentially if the pipeline evaluates to a non-empty result, the result is
$true
otherwise it is$false
. This is probably not what you intended.试试这个:
Try this: