powershellmonitorevent-viewer

Script won't run in Switch menu


function Show-Menu { #Create the Show-Menu function
    param ([string]$Title = 'Functions') #Sets title
    Clear-Host
    Write-Host "`t6: Reboot History." -foregroundcolor white
    Write-Host "`tQ: Enter 'Q' to quit."
} #close of create show menu function

#Begin Main Menu
do
 {
    Show-Menu #Displays created menu above
    
    $Selection = $(Write-Host "`tMake your selection: " -foregroundcolor Red -nonewline; Read-Host)
    
    
    switch ($selection) #Begin switch selection
    {

#===Reboot History===
    '6' {
                $Workstation = $(Write-Host "Workstation\IP Address" -nonewline -foregroundcolor DarkGreen) + $(Write-Host "(Use IP for remote users)?: " -NoNewline; Read-Host)
                $DaysFromToday = Read-Host "How many days would you like to go back?"
                $MaxEvents = Read-Host "How many events would you like to view?"
                
                $EventList = Get-WinEvent -ComputerName $Workstation -FilterHashtable @{
                    Logname = 'system'
                    Id = '41', '1074', '1076', '6005', '6006', '6008', '6009', '6013'
                    StartTime = (Get-Date).AddDays(-$DaysFromToday)
                } -MaxEvents $MaxEvents -ErrorAction Stop
 
 
                foreach ($Event in $EventList) {

                    if ($Event.Id -eq 1074) {
                        [PSCustomObject]@{
                            TimeStamp    = $Event.TimeCreated
                            Event        = $Event.Id
                            ShutdownType = 'Restart'
                            UserName     = $Event.Properties.value[6]
                        }
                    }

                    if ($Event.Id -eq 41) {
                        [PSCustomObject]@{
                            TimeStamp    = $Event.TimeCreated
                            Event        = $Event.Id
                            ShutdownType = 'Unexpected'
                            UserName     = ' '
                        }
                    }
                }
                pause
        }
    }
    }
 until ($selection -eq 'q') #End of main menu

Works perfectly fine if I remove the script from the switch and run it separately, but as soon as I call it from the switch it still asks for the workstation/IP, how many days, and max events, but just outputs nothing.

Here is what it looks like when it works:

How many days would you like to go back?: 90
How many events would you like to view?: 999

TimeStamp              Event ShutdownType UserName
---------              ----- ------------ --------
12/23/2022 12:20:55 AM  1074 Restart      Username
12/20/2022 1:00:01 AM   1074 Restart      Username
12/17/2022 12:21:54 AM  1074 Restart      Username
12/13/2022 8:57:40 AM   1074 Restart      Username

This is what I get when I run it within the switch menu

Workstation\IP Address(Use IP for remote users)?: IP Address
How many days would you like to go back?: 90
How many events would you like to view?: 999

Press Enter to continue...: 

I have tried just doing 1 day and 1 event, but same results. No errors or anything indicating a failure, so not sure how to troubleshoot this. I have had similar issues with switches in the past that were resolved with some researching into scopes, but I don't think this is the same case as it is all self contained within the switch itself.

I am at a loss, any ideas? As always, any insight into my script is greatly appreciated, even if it doesn't resolve the problem at hand.


Solution

  • JosefZ has provided the crucial pointer:

    Here's a simplified example:

    switch ('foo') {
    
      default {
    
        # Wrap the `foreach` statement in . { ... }, 
        # so its output can be piped to Out-Host. 
        . { 
          foreach ($i in 1..3) {
            [pscustomobject] @{ prop = $i } 
          }
        } |
          Out-Host # Without this, "pause" will run FIRST.
        pause
    
      }
    } 
    

    Note: