带有嵌入式开关的powershell foreach-object不锻炼

发布于 2025-02-03 19:38:24 字数 989 浏览 2 评论 0原文

试图从文本电子邮件中总计购买的时间。这是我拥有的代码,我希望它足够近,可以解释我要做什么。对我来说很有意义,但不豪华。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2025-02-10 19:38:24

感谢圣地亚哥,这现在有效。

$n = select-string -Path "$global:EMLpath\*.eml" -Pattern "[ORDER #" -SimpleMatch -CaseSensitive -Context 0,10
$ADDhrs = 0
$n.Context.PostContext|
          ForEach-Object{
            Switch -Wildcard ($Inputstring = $_) {
                "Hourly*" {$ADDhrs = $ADDhrs + (1 * (-split "$Inputstring")[4])}
                "Weekly*" {$ADDhrs = $ADDhrs + (168 * (-split "$Inputstring")[4])}
                "Monthly*" {$ADDhrs = $ADDhrs + (720 * (-split "$Inputstring")[4])}
                "Yearly*" {$ADDhrs = $ADDhrs + (8640 * (-split "$Inputstring")[4])}
             }
 }

Thanks Santiago, this works now.

$n = select-string -Path "$global:EMLpath\*.eml" -Pattern "[ORDER #" -SimpleMatch -CaseSensitive -Context 0,10
$ADDhrs = 0
$n.Context.PostContext|
          ForEach-Object{
            Switch -Wildcard ($Inputstring = $_) {
                "Hourly*" {$ADDhrs = $ADDhrs + (1 * (-split "$Inputstring")[4])}
                "Weekly*" {$ADDhrs = $ADDhrs + (168 * (-split "$Inputstring")[4])}
                "Monthly*" {$ADDhrs = $ADDhrs + (720 * (-split "$Inputstring")[4])}
                "Yearly*" {$ADDhrs = $ADDhrs + (8640 * (-split "$Inputstring")[4])}
             }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文