function Get-EventPsEngineState {
    <#
    .Synopsis
    Get serialized Windows PowerShell engine state events.
    .DESCRIPTION
    Get serialized Windows PowerShell engine state events. Query engine start, stop or all engine state events.
    .EXAMPLE
    Get-EventPsEngineState -Status Start | where {$_.hostVersion -like "*2.*"}

    Find all engine start events where the engine is for PowerShell 2.0.
    .NOTES
    This function needs to be executed with administrator priviages on the host.
    #>
    [CmdletBinding(DefaultParameterSetName = 'Local')]
    param (
        # Log name for where the events are stored.
        [Parameter(Mandatory = $false,
                    ValueFromPipelineByPropertyName = $true)]
        [string]
        $LogName = 'Windows PowerShell',

        # State of the engine, available or stopped.
        [Parameter(Mandatory = $false)]
        [ValidateSet('All','Available','Stopped')]
        [string]
        $EngineState = 'All',

        # Specifies the path to the event log files that this cmdlet get events from. Enter the paths to the log files in a comma-separated list, or use wildcard characters to create file path patterns. Function supports files with the .evtx file name extension. You can include events from different files and file types in the same command.
        [Parameter(Mandatory=$true,
                    Position=0,
                    ParameterSetName="file",
                    ValueFromPipelineByPropertyName=$true)]
        [Alias("FullName")]
        [ValidateNotNullOrEmpty()]
        [SupportsWildcards()]
        [string[]]
        $Path,


        # Gets events from the event logs on the specified computer. Type the NetBIOS name, an Internet Protocol (IP) address, or the fully qualified domain name of the computer.
        # The default value is the local computer.
        # To get events and event logs from remote computers, the firewall port for the event log service must be configured to allow remote access.
        [Parameter(Mandatory = $true,
                    ValueFromPipelineByPropertyName = $true,
                    ParameterSetName = 'Remote')]
        [string[]]
        $ComputerName,

        # Specifies a user account that has permission to perform this action.
        #
        # Type a user name, such as User01 or Domain01\User01. Or, enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will
        # be prompted for a password. If you type only the parameter name, you will be prompted for both a user name and a password.
        [Parameter(Mandatory = $false,
                    ParameterSetName = 'Remote')]
        [Management.Automation.PSCredential]
        [Management.Automation.CredentialAttribute()]
        $Credential,

        # Specifies the maximum number of events that are returned. Enter an integer. The default is to return all the events in the logs or files.
        [Parameter(Mandatory = $false,
                    ValueFromPipelineByPropertyName = $true)]
        [int64]
        $MaxEvents,

        # Stsrttime from where to pull events.
        [Parameter(Mandatory = $false)]
        [datetime]
        $StartTime,

        # Stsrttime from where to pull events.
        [Parameter(Mandatory = $false)]
        [datetime]
        $EndTime,

        # Changes the default logic for matching fields from 'and' to 'or'.
        [Parameter(Mandatory = $false)]
        [switch]
        $ChangeLogic,

        # Changes the query action from inclusion to exclusion when fields are matched.
        [Parameter(Mandatory = $false)]
        [switch]
        $Suppress
    )

    begin {
        
        $Params = $MyInvocation.BoundParameters
        $Params.Remove("EngineState") | Out-Null
    }

    process {
        [System.Collections.ArrayList]$eventIds = @(400,403)
        switch ($EngineState) {
            'Available' { $eventIds.Add(400) | Out-Null}
            'Stopped' { $eventIds.Add(403) | Out-Null}
            'All' { }
        }

        Search-EventLogEventData -EventId $eventIds -ParamHash $Params -Provider "PowerShell" -RecordType "PSEngineLifeCycle" -ReturnRecord | ForEach-Object -Process {
            [xml]$evtXml = $_.toxml()
            $evtInfo = [ordered]@{}
            $evtInfo['EventId'] = $evtXml.Event.System.EventID.'#text'
            $evtInfo['EventRecordID'] = $evtXml.Event.System.EventRecordID
            $evtInfo['TimeCreated'] = [datetime]$evtXml.Event.System.TimeCreated.SystemTime
            $evtInfo['Computer'] = $evtXml.Event.System.Computer
            $evtInfo['Provider'] = $evtXml.Event.System.Provider.Name
            $evtInfo['ProcessID'] = $evtXml.Event.System.Execution.ProcessID
            $evtInfo['ThreadID'] = $evtXml.Event.System.Execution.ThreadID
            $evtData = $evtxml.Event.EventData.Data[2] -split "`n"
            foreach($line in $evtData) {
                if ($line.trim() -ne '') {
                    $evtDataFields = $line.trim() -split '=',2
                    $evtInfo[$evtDataFields[0]] = $evtDataFields[1]
                }
            }
            New-Object psobject -Property $evtInfo
        }

    }

    end {}
}
