带有嵌入式开关的powershell foreach-object不锻炼
试图从文本电子邮件中总计购买的时间。这是我拥有的代码,我希望它足够近,可以解释我要做什么。对我来说很有意义,但不豪华。
$n = select-string -Path "$global:EMLpath\*.eml" -Pattern "[ORDER #" -SimpleMatch -CaseSensitive -Context 0,10
$n.Context.PostContext|
ForEach-Object{
Switch -Wildcard ($Inputstring = $_) {
$Inputstring -like "Hourly*" {$ADDhrs = 1 * (-split "$Inputstring")[4]}
$Inputstring -like "Weekly*" {$ADDhrs = 168 * (-split "$Inputstring")[4]}
$Inputstring -like "Monthly*" {$ADDhrs = 720 * (-split "$Inputstring")[4]}
$Inputstring -like "Yearly*" {$ADDhrs = 8640 * (-split "$Inputstring")[4]}
}
}
文件中的相关文本是:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
New Order: #1667
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
You’ve received the following order from Joe Shmo:
[ORDER #1667] (2022-04-28)
Monthly Server Rental X 1 = $26.56
Weekly Server Rental X 2 = $26.56
==========
Trying to total up the purchased time from text email. Here is the code that I have and I'm hoping it's close enough to explain what I'm trying to do. Makes sense to me but not posh.
$n = select-string -Path "$global:EMLpath\*.eml" -Pattern "[ORDER #" -SimpleMatch -CaseSensitive -Context 0,10
$n.Context.PostContext|
ForEach-Object{
Switch -Wildcard ($Inputstring = $_) {
$Inputstring -like "Hourly*" {$ADDhrs = 1 * (-split "$Inputstring")[4]}
$Inputstring -like "Weekly*" {$ADDhrs = 168 * (-split "$Inputstring")[4]}
$Inputstring -like "Monthly*" {$ADDhrs = 720 * (-split "$Inputstring")[4]}
$Inputstring -like "Yearly*" {$ADDhrs = 8640 * (-split "$Inputstring")[4]}
}
}
The pertinent text in the file is:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
New Order: #1667
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
You’ve received the following order from Joe Shmo:
[ORDER #1667] (2022-04-28)
Monthly Server Rental X 1 = $26.56
Weekly Server Rental X 2 = $26.56
==========
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢圣地亚哥,这现在有效。
Thanks Santiago, this works now.