Powershell foreach-object with embedded switch not working out

Viewed 39

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


==========

1 Answers

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])}
             }
 }
Related