{"id":353752,"date":"2024-09-17T13:19:50","date_gmt":"2024-09-17T13:19:50","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/ricerca-dei-log-degli-eventi-powershell\/"},"modified":"2024-10-13T19:02:23","modified_gmt":"2024-10-13T19:02:23","slug":"ricerca-dei-log-degli-eventi-powershell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/it\/script-hub\/ricerca-dei-log-degli-eventi-powershell\/","title":{"rendered":"Guida completa all&#8217;utilizzo di PowerShell per un&#8217;efficiente ricerca dei log degli eventi"},"content":{"rendered":"<p>Nel panorama in continua evoluzione dell&#8217;IT, il monitoraggio e l&#8217;analisi dei log degli eventi sono fondamentali per mantenere l&#8217;integrit\u00e0 del sistema, diagnosticare i problemi e garantire la conformit\u00e0 alla sicurezza.<\/p>\n<p>I log degli eventi forniscono una ricca fonte di informazioni sullo stato e sul comportamento dei sistemi e la possibilit\u00e0 di cercare e filtrare in modo efficiente questi log pu\u00f2 migliorare notevolmente la capacit\u00e0 di un professionista IT di gestire la propria infrastruttura.<\/p>\n<p>Questo post analizza uno script PowerShell che semplifica il processo di <strong>ricerca dei log degli eventi<\/strong>, rendendo pi\u00f9 facile individuare eventi specifici in base a criteri quali il nome del log degli eventi, la fonte, l&#8217;ID dell&#8217;evento e altro ancora.<\/p>\n<h2>L&#8217;importanza della gestione dei log degli eventi nell&#8217;IT<\/h2>\n<p>I log degli eventi sono registrazioni di eventi di sistema, sicurezza e applicazione su un computer. Questi log sono indispensabili per la risoluzione dei problemi, la verifica degli incidenti di sicurezza e la garanzia di conformit\u00e0 agli standard normativi.<\/p>\n<p>Per i <a href=\"https:\/\/www.ninjaone.com\/it\/cos-e-un-msp\" target=\"_blank\" rel=\"noopener\">fornitori di servizi gestiti (MSP)<\/a> e i professionisti IT, la capacit\u00e0 di effettuare ricerche rapide e accurate tra questi log \u00e8 fondamentale. Senza strumenti efficienti, questo compito pu\u00f2 diventare complicato, soprattutto quando si tratta di un gran numero di sistemi o di ambienti complessi.<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/it\/it-hub\/gestione-degli-endpoint\/cos-e-powershell\/\" target=\"_blank\" rel=\"noopener\">PowerShell<\/a>, un potente linguaggio di scripting sviluppato da Microsoft, offre solide funzionalit\u00e0 per interagire con i log degli eventi. Lo script di cui parliamo oggi \u00e8 stato progettato per semplificare il processo di ricerca, consentendo ai professionisti IT di individuare rapidamente gli eventi rilevanti all&#8217;interno del Visualizzatore eventi.<\/p>\n<h2>Lo script per la ricerca dei log degli eventi:<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 5.1\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    Search for specific events in Event Viewer based on the event log they were in, the source of the event, or the specific event IDs used. One of these three options is required for the search.\r\n.DESCRIPTION\r\n    Search for specific events in Event Viewer based on the event log they were in, the source of the event, or the specific event IDs used. One of these three options is required for the search.\r\n.EXAMPLE\r\n    -EventLogName \"Application\"\r\n    \r\n    Matching Events Found!\r\n\r\n    LogName      : Application\r\n    ProviderName : Microsoft-Windows-Security-SPP\r\n    Id           : 16384\r\n    TimeCreated  : 4\/4\/2024 10:00:48 AM\r\n    Message      : Successfully scheduled Software Protection service for re-start at 2024-04-04T21:19:48Z. Reason: Rul...\r\n\r\n    LogName      : Application\r\n    ProviderName : Microsoft-Windows-Security-SPP\r\n    Id           : 16394\r\n    TimeCreated  : 4\/4\/2024 10:00:17 AM\r\n    Message      : Offline downlevel migration succeeded.\r\n\r\n    LogName      : Application\r\n    ProviderName : Microsoft-Windows-Security-SPP\r\n    Id           : 16384\r\n    TimeCreated  : 4\/4\/2024 9:59:59 AM\r\n    Message      : Successfully scheduled Software Protection service for re-start at 2024-04-04T21:19:59Z. Reason: Rul...\r\n\r\nPARAMETER: -EventLogName \"Application\"\r\n    Specify the name of the Event Log from which to retrieve events.\r\n\r\nPARAMETER: -EventLogSource \"Microsoft-Windows-Kernel-General\"\r\n    Determines the source of the events to retrieve.\r\n\r\nPARAMETER: -EventLogMessage \"Alert\"\r\n    Filters events by the text contained in the event's message.\r\n\r\nPARAMETER: -EventIDs \"12, 13, 6008\"\r\n    A comma-separated list of event IDs to include in the search.\r\n\r\nPARAMETER: -excludeEventIDs \"13\"\r\n    A comma-separated list of event IDs to exclude from the search.\r\n\r\nPARAMETER: -StartDate \"12\/24\/2021\"\r\n    Defines the start date and time for the event search. Events logged before this time will not be included in the results.\r\n\r\nPARAMETER: -EndDate \"12\/29\/2021\"\r\n    Sets the end date and time for the event search. Events logged after this time will not be included.\r\n\r\nPARAMETER: -MultilineCustomField \"replaceMeWithAcustomFieldName\"\r\n    Specify the name of a multiline custom field to optionally store the search results in. Leave blank to not set a multiline field.\r\n\r\nPARAMETER: -WysiwygCustomField \"replaceMeWithACustomFieldName\"\r\n    Specify the name of a WYSIWYG custom field to optionally store the search results in. Leave blank to not set a WYSIWYG field.\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 10, Windows Server 2016\r\n    Release Notes: Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#&gt;\r\n\r\n[CmdletBinding()]\r\nparam (\r\n    [Parameter()]\r\n    [String]$EventLogName,\r\n    [Parameter()]\r\n    [String]$EventLogSource,\r\n    [Parameter()]\r\n    [String]$EventLogMessage,\r\n    [Parameter()]\r\n    [String]$EventIDs,\r\n    [Parameter()]\r\n    [String]$ExcludeEventIDs,\r\n    [Parameter()]\r\n    [datetime]$StartDate,\r\n    [Parameter()]\r\n    [datetime]$EndDate,\r\n    [Parameter()]\r\n    [String]$MultilineCustomField,\r\n    [Parameter()]\r\n    [String]$WysiwygCustomField\r\n)\r\n\r\nbegin {\r\n    # Set parameters using dynamic script variables.\r\n    if ($env:eventLogName -and $env:eventLogName -notlike \"null\") { $EventLogName = $env:eventLogName }\r\n    if ($env:eventLogSource -and $env:eventLogSource -notlike \"null\") { $EventLogSource = $env:eventLogSource }\r\n    if ($env:eventLogMessage -and $env:eventLogMessage -notlike \"null\") { $EventLogMessage = $env:eventLogMessage }\r\n    if ($env:eventIds -and $env:eventIds -notlike \"null\") { $EventIDs = $env:eventIds }\r\n    if ($env:excludeEventIds -and $env:excludeEventIds -notlike \"null\") { $ExcludeEventIDs = $env:excludeEventIds }\r\n    if ($env:eventStart -and $env:eventStart -notlike \"null\") { $StartDate = $env:eventStart }\r\n    if ($env:eventEnd -and $env:eventEnd -notlike \"null\") { $EndDate = $env:eventEnd }\r\n    if ($env:multilineCustomFieldName -and $env:multilineCustomFieldName -notlike \"null\") { $MultilineCustomField = $env:multilineCustomFieldName }\r\n    if ($env:wysiwygCustomFieldName -and $env:wysiwygCustomFieldName -notlike \"null\") { $WysiwygCustomField = $env:wysiwygCustomFieldName }\r\n\r\n    # Check if both StartDate and EndDate are provided and if StartDate is earlier than EndDate\r\n    if (($StartDate -and $EndDate) -and $StartDate -gt $EndDate) {\r\n        Write-Host -Object \"[Error] Start date cannot be earlier than end date!\"\r\n        exit 1\r\n    }\r\n\r\n    # Verify that WysiwygField and MultiLineField are not the same, exiting with an error if they are.\r\n    if ($WysiwygCustomField -and $MultilineCustomField -and ($WysiwygCustomField -eq $MultilineCustomField)) {\r\n        Write-Host -Object \"[Error] Wysiwyg Field and Multiline Field are the same! Custom fields cannot be the same type.\"\r\n        Write-Host -Object \"https:\/\/ninjarmm.zendesk.com\/hc\/en-us\/articles\/18601842971789-Custom-Fields-by-Type-and-Functionality\"\r\n        exit 1\r\n    }\r\n\r\n    # Ensure that at least one of Event ID, Event Log Name, or Event Source is provided for the query\r\n    if (!$EventIDs -and !$EventLogName -and !$EventLogSource) {\r\n        Write-Host -Object \"[Error] You must provide either an Event ID, Event Log Name or Event Source.\"\r\n        exit 1\r\n    }\r\n\r\n    # Trimming trailing spaces.\r\n    if ($EventLogName) {\r\n        $EventLogName = $EventLogName.Trim()\r\n    }\r\n\r\n    # Retrieve and sort all event log names available on the system\r\n    $EventLogNamesOnSystem = Get-WinEvent -ListLog * -ErrorAction SilentlyContinue | Sort-Object LogName\r\n    # Check if the provided EventLogName exists in the system's event logs\r\n    if ($EventLogName -and ($EventLogNamesOnSystem).LogName -notcontains $EventLogName) {\r\n        # If not found, print an error message and a list of valid event log names, then exit the script\r\n        Write-Host -Object \"[Error] Event Log '$EventLogName' doesn't exist! See the list below for valid event log names.\"\r\n        Write-Host -Object \"### Valid Event Log Names ###\"\r\n        $EventLogNamesOnSystem | Select-Object -ExpandProperty LogName | Write-Host\r\n        exit 1 \r\n    }\r\n\r\n    $InvalidEventSourceCharacters = \"[\\\\\/&lt;&gt;&amp;`\"%\\|']\"\r\n    if ($EventLogSource) {\r\n        if ($EventLogSource -match $InvalidEventSourceCharacters) {\r\n            Write-Host -Object \"[Error] Event Log Source '$EventLogSource' contains an invalid character!\"\r\n            exit 1\r\n        }\r\n\r\n        if ($EventLogSource.Length -gt 255) {\r\n            Write-Host -Object \"[Error] Event Log Source '$EventLogSource' is too large to be an event source!\"\r\n            exit 1\r\n        }\r\n\r\n        # Trims the event log source for trailing spaces\r\n        $EventLogSource = $EventLogSource.Trim()\r\n    }\r\n\r\n    # Prepare a list to hold valid event IDs to search for\r\n    $EventIdsToSearch = New-Object System.Collections.Generic.List[int]\r\n    # Process the input event IDs, removing any that are not purely numerical\r\n    if ($EventIDs -and $EventIDs -match \",\") {\r\n        # If multiple event IDs are provided and separated by commas, split them\r\n        $EventIDs -split \",\" | ForEach-Object {\r\n            $EventId = $_.Trim()\r\n            # Validate each event ID to ensure it's numerical\r\n            if ($EventId -match '[a-zA-Z]|\\W') {\r\n                # If not, print an error and skip adding this ID to the list\r\n                Write-Host \"[Error] Event ID '$EventId' is not a valid event id. Removing it from the search.\"\r\n                $ExitCode = 1\r\n                return\r\n            }\r\n            # Check size of event id\r\n            if ([long]$EventId -gt 65535 -or [long]$EventId -lt 0) {\r\n                Write-Host \"[Error] Event ID '$EventId' is not a valid event id. Event ID's must be less than or equal to 65535 and greater than or equal to 0. Removing it from the search.\"\r\n                $ExitCode = 1\r\n                return\r\n            }\r\n             \r\n            # Add the validated event ID to the list\r\n            $EventIdsToSearch.Add($EventId)\r\n        }\r\n    }\r\n    elseif ($EventIDs) {\r\n        $EventId = $EventIDs.Trim()\r\n        # Handle a single event ID input\r\n        if ($EventId -match '[a-zA-Z]|\\W') {\r\n            Write-Host \"[Error] Event ID '$EventId' is not a valid event id. Removing it from the search.\"\r\n            $ExitCode = 1\r\n        }\r\n        elseif ([long]$EventId -gt 65535 -or [long]$EventId -lt 0) {\r\n            Write-Host \"[Error] Event ID '$EventId' is not a valid event id. Event ID's must be less than or equal to 65535 and greater than or equal to 0. Removing it from the search.\"\r\n            $ExitCode = 1\r\n        }\r\n        else {\r\n            $EventIdsToSearch.Add($EventId)\r\n        }\r\n    }\r\n \r\n    # Prepare a list to hold event IDs that should be excluded from the search\r\n    $EventsToExclude = New-Object System.Collections.Generic.List[int]\r\n     \r\n    # Similar process for excluded event IDs as regular event IDs\r\n    if ($ExcludeEventIDs -and $ExcludeEventIDs -match \",\") {\r\n        $ExcludeEventIDs -split \",\" | ForEach-Object {\r\n            $ExcludeEventId = $_.Trim()\r\n            if ($ExcludeEventId -match '[a-zA-Z]|\\W') {\r\n                Write-Host \"[Error] Event ID '$ExcludeEventId' is not a valid event id. Removing it from the exclusions.\"\r\n                $ExitCode = 1\r\n                return\r\n            }\r\n \r\n            if ([long]$ExcludeEventId -gt 65535 -or [long]$ExcludeEventId -lt 0) {\r\n                Write-Host \"[Error] Event ID '$ExcludeEventId' is not a valid event id. Event ID's must be less than or equal to 65535 and greater than or equal to 0. Removing it from the exclusions.\"\r\n                $ExitCode = 1\r\n                return\r\n            }\r\n             \r\n            $EventsToExclude.Add($ExcludeEventId)\r\n        }\r\n    }\r\n    elseif ($ExcludeEventIDs) {\r\n        $ExcludeEventId = $ExcludeEventIDs.Trim()\r\n        if ($ExcludeEventId -match '[a-zA-Z]|\\W') {\r\n            Write-Host \"[Error] Event ID '$ExcludeEventId' is not a valid event id. Removing it from the exclusions.\"\r\n            $ExitCode = 1\r\n        }\r\n        elseif ([long]$ExcludeEventId -gt 65535 -or [long]$ExcludeEventId -lt 0) {\r\n            Write-Host \"[Error] Event ID '$ExcludeEventId' is not a valid event id. Event ID's must be less than or equal to 65535 and greater than or equal to 0. Removing it from the exclusions.\"\r\n            $ExitCode = 1\r\n        }\r\n        else {\r\n            $EventsToExclude.Add($ExcludeEventId)\r\n        }\r\n    }\r\n \r\n    # Check if there are any event IDs to exclude and if the list of event IDs to search for is not empty.\r\n    if ($EventsToExclude.Count -gt 0 -and $EventIdsToSearch.Count -gt 0) {\r\n        $EventsToExclude | ForEach-Object {\r\n            # Check if the current event ID from the exclusion list is also in the list of event IDs to search for.\r\n            if ($EventIdsToSearch -contains $_) {\r\n                Write-Warning \"Event ID $_ has been specified for both inclusion and exclusion. It will be excluded.\"\r\n            }\r\n        }\r\n    }\r\n \r\n    # Check if there's no valid event ID, log name, or log source provided and exit if true\r\n    if ($EventIdsToSearch.Count -eq 0 -and !$EventLogName -and !$EventLogSource) {\r\n        Write-Host \"[Error] No valid Event ID given and no Event Log Name or Event Log Source given.\"\r\n        exit 1\r\n    }\r\n\r\n    # Handy function to set a custom field.\r\n    function Set-NinjaProperty {\r\n        [CmdletBinding()]\r\n        Param(\r\n            [Parameter(Mandatory = $True)]\r\n            [String]$Name,\r\n            [Parameter()]\r\n            [String]$Type,\r\n            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]\r\n            $Value,\r\n            [Parameter()]\r\n            [String]$DocumentName\r\n        )\r\n\r\n        $Characters = $Value | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n        if ($Characters -ge 200000) {\r\n            throw [System.ArgumentOutOfRangeException]::New(\"Character limit exceeded, value with $Characters characters is greater than or equal to 200,000 characters.\")\r\n        }\r\n    \r\n        # If we're requested to set the field value for a Ninja document we'll specify it here.\r\n        $DocumentationParams = @{}\r\n        if ($DocumentName) { $DocumentationParams[\"DocumentName\"] = $DocumentName }\r\n    \r\n        # This is a list of valid fields that can be set. If no type is given, it will be assumed that the input doesn't need to be changed.\r\n        $ValidFields = \"Attachment\", \"Checkbox\", \"Date\", \"Date or Date Time\", \"Decimal\", \"Dropdown\", \"Email\", \"Integer\", \"IP Address\", \"MultiLine\", \"MultiSelect\", \"Phone\", \"Secure\", \"Text\", \"Time\", \"URL\", \"WYSIWYG\"\r\n        if ($Type -and $ValidFields -notcontains $Type) { Write-Warning \"$Type is an invalid type! Please check here for valid types. https:\/\/ninjarmm.zendesk.com\/hc\/en-us\/articles\/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality\" }\r\n    \r\n        # The field below requires additional information to be set\r\n        $NeedsOptions = \"Dropdown\"\r\n        if ($DocumentName) {\r\n            if ($NeedsOptions -contains $Type) {\r\n                # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.\r\n                $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2&gt;&amp;1\r\n            }\r\n        }\r\n        else {\r\n            if ($NeedsOptions -contains $Type) {\r\n                $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2&gt;&amp;1\r\n            }\r\n        }\r\n    \r\n        # If an error is received it will have an exception property, the function will exit with that error information.\r\n        if ($NinjaPropertyOptions.Exception) { throw $NinjaPropertyOptions }\r\n    \r\n        # The below type's require values not typically given in order to be set. The below code will convert whatever we're given into a format ninjarmm-cli supports.\r\n        switch ($Type) {\r\n            \"Checkbox\" {\r\n                # While it's highly likely we were given a value like \"True\" or a boolean datatype it's better to be safe than sorry.\r\n                $NinjaValue = [System.Convert]::ToBoolean($Value)\r\n            }\r\n            \"Date or Date Time\" {\r\n                # Ninjarmm-cli expects the GUID of the option to be selected. Therefore, the given value will be matched with a GUID.\r\n                $Date = (Get-Date $Value).ToUniversalTime()\r\n                $TimeSpan = New-TimeSpan (Get-Date \"1970-01-01 00:00:00\") $Date\r\n                $NinjaValue = $TimeSpan.TotalSeconds\r\n            }\r\n            \"Dropdown\" {\r\n                # Ninjarmm-cli is expecting the guid of the option we're trying to select. So we'll match up the value we were given with a guid.\r\n                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header \"GUID\", \"Name\"\r\n                $Selection = $Options | Where-Object { $_.Name -eq $Value } | Select-Object -ExpandProperty GUID\r\n    \r\n                if (-not $Selection) {\r\n                    throw [System.ArgumentOutOfRangeException]::New(\"Value is not present in dropdown\")\r\n                }\r\n    \r\n                $NinjaValue = $Selection\r\n            }\r\n            default {\r\n                # All the other types shouldn't require additional work on the input.\r\n                $NinjaValue = $Value\r\n            }\r\n        }\r\n    \r\n        # We'll need to set the field differently depending on if its a field in a Ninja Document or not.\r\n        if ($DocumentName) {\r\n            $CustomField = Ninja-Property-Docs-Set -AttributeName $Name -AttributeValue $NinjaValue @DocumentationParams 2&gt;&amp;1\r\n        }\r\n        else {\r\n            $CustomField = $NinjaValue | Ninja-Property-Set-Piped -Name $Name 2&gt;&amp;1\r\n        }\r\n    \r\n        if ($CustomField.Exception) {\r\n            throw $CustomField\r\n        }\r\n    }\r\n\r\n    function Test-IsElevated {\r\n        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()\r\n        $p = New-Object System.Security.Principal.WindowsPrincipal($id)\r\n        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)\r\n    }\r\n\r\n    if (!$ExitCode) {\r\n        $ExitCode = 0\r\n    }\r\n}\r\nprocess {\r\n    # Check if the script is running with elevated (Administrator) privileges\r\n    if (!(Test-IsElevated)) {\r\n        # If not, display an error message and exit with status code 1\r\n        Write-Host -Object \"[Error] Access Denied. Please run with Administrator privileges.\"\r\n        exit 1\r\n    }\r\n\r\n    # Prepare a list to hold event log names to search for\r\n    $EventLogNamesToSearch = New-Object System.Collections.Generic.List[string]\r\n\r\n    # If no event log name was required we'll search all the event\r\n    if (!$EventLogName) {\r\n        $EventLogNamesOnSystem | Where-Object { $_.RecordCount -gt 0 } | Select-Object -ExpandProperty LogName | ForEach-Object {\r\n            $EventLogNamesToSearch.Add($_)\r\n        }\r\n    }\r\n    else {\r\n        $EventLogNamesToSearch.Add($EventLogName)\r\n    }\r\n\r\n    # Create XML object.\r\n    [xml]$XML = New-Object System.Xml.XmlDocument\r\n\r\n    # Add QueryList element to xml.\r\n    $QueryList = $XML.CreateElement(\"QueryList\")\r\n    $QueryList = $XML.AppendChild($QueryList)\r\n\r\n    # Create query element and nest it under QueryList.\r\n    $Query = $XML.CreateElement(\"Query\")\r\n    $Query.SetAttribute(\"Id\", \"0\")\r\n    $Query = $QueryList.AppendChild($Query)\r\n    \r\n\r\n    # Foreach event log to search we're going to create a select element.\r\n    $EventLogNamesToSearch | ForEach-Object {\r\n        # We'll start each loop by selecting the query element to add to.\r\n        $Query = $XML.SelectSingleNode(\"\/\/Query\")\r\n\r\n        # The select element starts off with the event log to search.\r\n        $Select = $XML.CreateElement(\"Select\")\r\n        $Select.SetAttribute(\"Path\", \"$_\")\r\n        \r\n        # Reset the inner text between runnings\r\n        $XMLInnerText = $Null\r\n\r\n        # The inner text of each element (&lt;Element1&gt;InnerText&lt;\/Element1&gt;) will need to be built differently depending on the parameters.\r\n        if ($EventLogSource) {\r\n            $XMLInnerText = \"*[System[Provider[@Name='$EventLogSource']]]\"\r\n        }\r\n        \r\n        # If we're given a select number of event id's to search we'll filter them here.\r\n        if ($EventIdsToSearch.Count -gt 0) {\r\n            $EventIDSearchText = $Null\r\n            $EventIdsToSearch | ForEach-Object {\r\n                # We may have been given one event id or more than one.\r\n                if ($EventIDSearchText) {\r\n                    $EventIDSearchText = \"$EventIDSearchText or EventID=$_\"\r\n                }\r\n                else {\r\n                    $EventIDSearchText = \"EventID=$_\"\r\n                }\r\n            }\r\n\r\n            # We'll replace the two ending brackets with our given search text\r\n            if ($XMLInnerText) {\r\n                $XMLInnerText = $XMLInnerText -replace ']]$', \" and ($EventIDSearchText)]]\"\r\n            }\r\n            else {\r\n                $XMLInnerText = \"*[System[($EventIDSearchText)]]\"\r\n            }\r\n        }\r\n\r\n        # If we're also asked to filter based on the date the event was created we'll create the filter text here.\r\n        if ($StartDate -or $EndDate) {\r\n            $DateFilter = $Null \r\n            if ($StartDate) {\r\n                $XMLstartDate = Get-Date $StartDate -Format \"yyyy-MM-ddTHH:mm:ss\"\r\n                # PowerShell will convert the &lt; or &gt; symbol for us when we go to save to the xml.\r\n                $DateFilter = \"@SystemTime&gt;='$XMLstartDate'\"\r\n            }\r\n\r\n            # We may or may not have been given a start date.\r\n            if ($EndDate -and $DateFilter) {\r\n                $XMLendDate = Get-Date $EndDate -Format \"yyyy-MM-ddTHH:mm:ss\"\r\n                $DateFilter = \"$DateFilter and @SystemTime&lt;='$XMLendDate'\"\r\n            }\r\n            elseif ($EndDate) {\r\n                $XMLendDate = Get-Date $EndDate -Format \"yyyy-MM-ddTHH:mm:ss\"\r\n                $DateFilter = \"@SystemTime&lt;='$XMLendDate'\"\r\n            }\r\n\r\n            # Replace the last two closing brackets and add our filter text.\r\n            if($XMLInnerText){\r\n                $XMLInnerText = $XMLInnerText -replace ']]$', \" and TimeCreated[$DateFilter]]]\"\r\n            }else{\r\n                $XMLInnerText = \"*[System[TimeCreated[$DateFilter]]]\"\r\n            }\r\n        }\r\n\r\n        # If no filters were given (other than the event log name) we'll need to select everything in that log\r\n        if(!$XMLInnerText){\r\n            $XMLInnerText = \"*\"\r\n        }\r\n\r\n        # Save our filter text to the select statement\r\n        $Select.InnerText = $XMLInnerText\r\n\r\n        # Append our select statement to our xml file\r\n        $Query.AppendChild($Select) | Out-Null\r\n    }\r\n\r\n    # Search for matching events using the XML filter\r\n    $MatchingEvents = Get-WinEvent -FilterXml $XML -ErrorAction SilentlyContinue\r\n\r\n    # Exclude events based on the excluded event IDs if any are specified\r\n    if ($EventsToExclude.Count -gt 0) {\r\n        $MatchingEvents = $MatchingEvents | Where-Object { $EventsToExclude -notcontains $_.ID }\r\n    }\r\n\r\n    # Exclude events that do not match the keywords you specified.\r\n    if ($EventLogMessage) {\r\n        $MatchingEvents = $MatchingEvents | Where-Object { $_.Message -like \"*$EventLogMessage*\" }\r\n    }\r\n\r\n    # If the event log message is larger than 100 characters trim it and add ...\r\n    if ($MatchingEvents) {\r\n        $MatchingEvents = $MatchingEvents | Select-Object LevelDisplayName, LogName, ProviderName, Id, TimeCreated, @{\r\n            Name       = 'Message'\r\n            Expression = {\r\n                $Characters = $_.Message | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n                if ($Characters -gt 100) {\r\n                    \"$(($_.Message).SubString(0,100))(...)\"\r\n                }\r\n                else {\r\n                    $_.Message\r\n                }\r\n            }\r\n        }\r\n\r\n        # Sort the object by newest event to oldest\r\n        $MatchingEvents = $MatchingEvents | Sort-Object TimeCreated -Descending\r\n    }\r\n\r\n    # Set a Wysiwyg custom field if any matching events are found and it was requested.\r\n    if ($WysiwygCustomField -and $MatchingEvents) {\r\n        try {\r\n            Write-Host \"Attempting to set Custom Field '$WysiwygCustomField'.\"\r\n\r\n            # Prepare the custom field output.\r\n            $CustomFieldValue = New-Object System.Collections.Generic.List[string]\r\n\r\n            # Convert the matching events into an html report.\r\n            $htmlTable = $MatchingEvents | Select-Object -Property LevelDisplayName, LogName, ProviderName, Id, TimeCreated, Message | ConvertTo-Html -Fragment\r\n            \r\n            # Set color coding\r\n            $htmlTable = $htmlTable -replace \"&lt;tr&gt;&lt;td&gt;Verbose&lt;\/td&gt;\", \"&lt;tr class=`\"other`\"&gt;&lt;td&gt;Verbose&lt;\/td&gt;\"\r\n            $htmlTable = $htmlTable -replace \"&lt;tr&gt;&lt;td&gt;Warning&lt;\/td&gt;\", \"&lt;tr class=`\"warning`\"&gt;&lt;td&gt;Warning&lt;\/td&gt;\"\r\n            $htmlTable = $htmlTable -replace \"&lt;tr&gt;&lt;td&gt;Error&lt;\/td&gt;\", \"&lt;tr class=`\"danger`\"&gt;&lt;td&gt;Error&lt;\/td&gt;\"\r\n            $htmlTable = $htmlTable -replace \"&lt;tr&gt;&lt;td&gt;Critical Error&lt;\/td&gt;\", \"&lt;tr class=`\"danger`\"&gt;&lt;td&gt;Critical Error&lt;\/td&gt;\"\r\n\r\n            # Remove Level Display Name\r\n            $LevelDisplayNames = $MatchingEvents | Select-Object -Property LevelDisplayName -Unique\r\n            $LevelDisplayNames | ForEach-Object {\r\n                $htmlTable = $htmlTable -replace \"&lt;td&gt;$([Regex]::Escape($_.LevelDisplayName))&lt;\/td&gt;\"\r\n            }\r\n            $htmlTable = $htmlTable -replace \"&lt;th&gt;LevelDisplayName&lt;\/th&gt;\"\r\n\r\n            # Add the newly created html into the custom field output.\r\n            $CustomFieldValue.Add($htmlTable)\r\n\r\n            # Check that the output complies with the hard character limits.\r\n            $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n            if ($Characters -ge 199500) {\r\n                Write-Warning \"200,000 Character Limit has been reached! Trimming output until the character limit is satisified...\"\r\n                \r\n                # If it doesn't comply with the limits we'll need to recreate it with some adjustments.\r\n                $i = 0\r\n                do {\r\n                    # Recreate the custom field output starting with a warning that we truncated the output.\r\n                    $CustomFieldValue = New-Object System.Collections.Generic.List[string]\r\n                    $CustomFieldValue.Add(\"&lt;h1&gt;This info has been truncated to accommodate the 200,000 character limit.&lt;\/h1&gt;\")\r\n\r\n                    # The custom field information is sorted from newest to oldest. We'll remove the oldest first by flipping the array upside down.\r\n                    [array]::Reverse($htmlTable)\r\n                    # If the next entry is a row we'll delete it.\r\n                    if ($htmlTable[$i] -match '&lt;tr&gt;&lt;td&gt;' -or $htmlTable[$i] -match '&lt;tr class=') {\r\n                        $htmlTable[$i] = $null\r\n                    }\r\n                    $i++\r\n                    # We'll flip the array back to right side up.\r\n                    [array]::Reverse($htmlTable)\r\n\r\n                    # Add it back to the output.\r\n                    $CustomFieldValue.Add($htmlTable)\r\n\r\n                    # Check that we now comply with the character limit. If not restart the do loop.\r\n                    $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n                }while ($Characters -ge 199500)\r\n            }\r\n\r\n            # Set the custom field.\r\n            Set-NinjaProperty -Name $WysiwygCustomField -Value $CustomFieldValue\r\n            Write-Host \"Successfully set Custom Field '$WysiwygCustomField'!\"\r\n        }\r\n        catch {\r\n            Write-Host \"[Error] $($_.Exception.Message)\"\r\n            $ExitCode = 1\r\n        }\r\n    }\r\n\r\n    # Set a multiline custom field if any matching events are found and it was requested.\r\n    if ($MultilineCustomField -and $MatchingEvents) {\r\n        try {\r\n            Write-Host \"Attempting to set Custom Field '$MultilineCustomField'.\"\r\n            $CustomFieldValue = New-Object System.Collections.Generic.List[string]\r\n\r\n            # We don't want to edit the matching Events array if we have to truncate later so we'll create a duplicate here.\r\n            $CustomFieldList = $MatchingEvents | Select-Object -Property LogName, ProviderName, Id, TimeCreated, Message\r\n\r\n            # Format the matching items into a nice list with the relevant properties.\r\n            $CustomFieldValue.Add(($CustomFieldList | Format-List -Property LogName, ProviderName, Id, TimeCreated, Message | Out-String))\r\n            \r\n            # Check that the output complies with the hard character limits.\r\n            $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n            if ($Characters -ge 9500) {\r\n                Write-Warning \"10,000 Character Limit has been reached! Trimming output until the character limit is satisified...\"\r\n                \r\n                # If it doesn't comply with the limits we'll need to recreate it with some adjustments.\r\n                $i = 0\r\n                do {\r\n                    # Recreate the custom field output starting with a warning that we truncated the output.\r\n                    $CustomFieldValue = New-Object System.Collections.Generic.List[string]\r\n                    $CustomFieldValue.Add(\"This info has been truncated to accommodate the 10,000 character limit.\")\r\n                    \r\n                    # The custom field information is sorted from newest to oldest. We'll remove the oldest events first by flipping the array upside down.\r\n                    [array]::Reverse($CustomFieldList)\r\n\r\n                    # Remove the next item which in this case will be the oldest item.\r\n                    $CustomFieldList[$i] = $null\r\n                    $i++\r\n\r\n                    # We'll flip the array back to right side up.\r\n                    [array]::Reverse($CustomFieldList)\r\n\r\n                    # Add it back to the output.\r\n                    $CustomFieldValue.Add(($CustomFieldList | Format-List -Property LogName, ProviderName, Id, TimeCreated, Message | Out-String))\r\n\r\n                    # Check that we now comply with the character limit. If not restart the do loop.\r\n                    $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters\r\n                }while ($Characters -ge 9500)\r\n            }\r\n\r\n            Set-NinjaProperty -Name $MultilineCustomField -Value $CustomFieldValue\r\n            Write-Host \"Successfully set Custom Field '$MultilineCustomField'!\"\r\n        }\r\n        catch {\r\n            Write-Host \"[Error] $($_.Exception.Message)\"\r\n            $ExitCode = 1\r\n        }\r\n    }\r\n\r\n    # If any matching events were found output them into the activity log.\r\n    if ($MatchingEvents) {\r\n        Write-Host \"Matching Events Found!\"\r\n        $MatchingEvents | Format-List LogName, ProviderName, Id, TimeCreated, Message | Out-String | Write-Host\r\n    }\r\n    else {\r\n        Write-Host \"No matching events found!\"\r\n    }\r\n\r\n    exit $ExitCode\r\n}\r\nend {\r\n    \r\n    \r\n    \r\n}<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"blog-cta-new blog-cta-style-1\"><div class=\"cta-left\"><h2><\/h2><p><\/p><\/div><div class=\"cta-right\"><a class=\"button\" href=\"\"><\/a><\/div><\/div>\n<h2>Comprendere lo script PowerShell per la ricerca dei log degli eventi<\/h2>\n<p>Questo script PowerShell \u00e8 uno strumento versatile che consente agli utenti di cercare eventi specifici nel Visualizzatore eventi in base a vari criteri. Lo script pu\u00f2 filtrare gli eventi in base al nome del log, alla fonte, all&#8217;ID dell&#8217;evento, al contenuto del messaggio e all&#8217;intervallo di date. Inoltre, offre opzioni per escludere determinati ID di eventi dalla ricerca e per memorizzare i risultati in campi personalizzati.<\/p>\n<p>Ecco una descrizione dettagliata del funzionamento dello script per la ricerca dei log degli eventi:<\/p>\n<h3>1. Parametri e variabili d&#8217;ambiente:<\/h3>\n<ul>\n<li>Lo script inizia impostando i parametri che consentono agli utenti di definire i criteri di ricerca, come EventLogName, EventLogSource, EventIDs, StartDate e EndDate. Supporta anche variabili d&#8217;ambiente per questi parametri, consentendo l&#8217;integrazione con altri flussi di lavoro di automazione.<\/li>\n<\/ul>\n<h3>2. Convalida e gestione degli errori:<\/h3>\n<ul>\n<li>Lo script include pi\u00f9 livelli di convalida per garantire che i parametri di input siano corretti. Ad esempio, controlla se la data di inizio fornita \u00e8 precedente alla data di fine e convalida che gli ID degli eventi siano numerici e rientrino nell&#8217;intervallo accettabile.<\/li>\n<li>Se i parametri WysiwygCustomField e MultilineCustomField sono impostati sullo stesso valore, lo script mostra un errore, evitando configurazioni in conflitto.<\/li>\n<\/ul>\n<h3>3. Logica della ricerca dei log degli eventi:<\/h3>\n<ul>\n<li>Lo script costruisce una query XML per filtrare gli eventi in base ai criteri forniti. Questa query viene quindi utilizzata con il cmdlet Get-WinEvent per recuperare gli eventi corrispondenti dai registri specificati.<\/li>\n<li>Lo script consente di escludere determinati ID di eventi e pu\u00f2 filtrare gli eventi in base al contenuto del messaggio, rendendolo altamente personalizzabile.<\/li>\n<\/ul>\n<h3>4. Impostazione del campo personalizzato:<\/h3>\n<ul>\n<li>Se vengono trovati eventi corrispondenti, lo script pu\u00f2 memorizzare i risultati in campi personalizzati, sia come campo di testo multilinea che come report in formato HTML. Questa funzionalit\u00e0 \u00e8 particolarmente utile per gli MSP che devono documentare i risultati o automatizzare la reportistica.<\/li>\n<\/ul>\n<h3>5. Output e ordinamento:<\/h3>\n<ul>\n<li>Lo script fornisce i risultati in un formato leggibile, ordinati in base all&#8217;ora in cui gli eventi sono stati creati, dal pi\u00f9 recente al pi\u00f9 vecchio. Inoltre, riduce i messaggi lunghi per evitare di sovraccaricare l&#8217;output.<\/li>\n<\/ul>\n<h2>Applicazioni pratiche dello script per la ricerca dei log degli eventi<\/h2>\n<p>Consideriamo uno scenario in cui un professionista IT \u00e8 incaricato di identificare gli errori ricorrenti su pi\u00f9 server in un ambiente aziendale. Esaminare manualmente i log degli eventi su ogni server richiederebbe molto tempo e sarebbe soggetto a errori.<\/p>\n<p>Sfruttando questo script PowerShell, il professionista pu\u00f2 automatizzare il processo di ricerca, filtrando i log in base a specifici ID di eventi di errore e restringendo i risultati a un particolare intervallo di tempo. Questo approccio non solo fa risparmiare tempo, ma migliora anche l&#8217;accuratezza, consentendo al team IT di diagnosticare e risolvere rapidamente i problemi.<\/p>\n<p>Un altro caso d&#8217;uso potrebbe riguardare la verifica della conformit\u00e0. Un MSP potrebbe aver bisogno di verificare che determinati eventi di sicurezza, come i tentativi di accesso riusciti o falliti, si verifichino come previsto nei suoi sistemi gestiti. Questo script pu\u00f2 essere utilizzato per estrarre questi eventi e generare report che dimostrino la conformit\u00e0 ai criteri di sicurezza.<\/p>\n<h2>Approcci a confronto per la ricerca dei log degli eventi<\/h2>\n<p>Sebbene esistano diversi metodi per la ricerca dei log degli eventi, tra cui l&#8217;interfaccia grafica integrata del Visualizzatore eventi o altri strumenti di terze parti, questo script PowerShell offre vantaggi distinti. A differenza delle ricerche manuali tramite l&#8217;interfaccia grafica, questo script pu\u00f2 essere automatizzato e integrato in flussi di lavoro pi\u00f9 ampi. Inoltre, rispetto ad altri strumenti, PowerShell offre un elevato grado di personalizzazione, consentendo ai professionisti IT di adattare i criteri di ricerca alle proprie esigenze specifiche.<\/p>\n<h2>Domande frequenti<\/h2>\n<h3>D: Lo script per la ricerca dei log degli eventi pu\u00f2 essere utilizzato su sistemi con versioni precedenti di Windows?<\/h3>\n<p>R: Lo script richiede Windows PowerShell 5.1 ed \u00e8 supportato su Windows 10 e Windows Server 2016 o versioni successive.<\/p>\n<h3>D: Cosa succede se si supera il limite di caratteri quando si imposta un campo personalizzato?<\/h3>\n<p>R: Lo script include una logica per ridurre l&#8217;output in modo da rientrare nel limite di caratteri, assicurando che lo script non fallisca a causa di set di dati troppo grandi.<\/p>\n<h3>D: \u00c8 possibile cercare tutti i log degli eventi di un sistema utilizzando questo script?<\/h3>\n<p>R: S\u00ec, se non viene fornito un nome specifico per il log degli eventi, lo script cercher\u00e0 in tutti i log degli eventi disponibili sul sistema.<\/p>\n<h2>Implicazioni per la sicurezza informatica<\/h2>\n<p>La capacit\u00e0 di effettuare un&#8217;efficiente ricerca dei log degli eventi ha implicazioni significative per la sicurezza informatica. Identificando rapidamente gli eventi sospetti o anomali, i professionisti IT possono rispondere in modo pi\u00f9 efficace a potenziali incidenti di sicurezza. L&#8217;automazione fornita da questo script riduce il rischio di errore umano e garantisce che gli eventi critici non vengano trascurati.<\/p>\n<h2>Best practice per l&#8217;utilizzo dello script<\/h2>\n<ul>\n<li><strong>Testa sempre lo script per impostare il tipo di avvio del servizio host di dispositivi UPnP in un ambiente controllato prima di distribuirlo in produzione.<\/strong><\/li>\n<li><strong>Utilizza le variabili d&#8217;ambiente per integrare lo script in flussi di lavoro di automazione pi\u00f9 ampi.<\/strong><\/li>\n<li><strong>Aggiorna regolarmente lo script per incorporare nuove funzionalit\u00e0 o risolvere potenziali vulnerabilit\u00e0 di sicurezza.<\/strong><\/li>\n<\/ul>\n<h2>Considerazioni finali<\/h2>\n<p>Una gestione efficiente dei log degli eventi \u00e8 essenziale per mantenere l&#8217;integrit\u00e0 del sistema, garantire la sicurezza e ottenere la conformit\u00e0. Questo script PowerShell fornisce ai professionisti IT un potente strumento per la ricerca e l&#8217;analisi dei log degli eventi, consentendo loro di svolgere le proprie mansioni in modo pi\u00f9 efficace.<\/p>\n<p>Per gli MSP e i team IT che gestiscono pi\u00f9 sistemi, <a href=\"https:\/\/www.ninjaone.com\/it\/\" target=\"_blank\" rel=\"noopener\">NinjaOne<\/a> offre una suite completa di strumenti che possono migliorare ulteriormente la capacit\u00e0 di monitorare e gestire la tua infrastruttura IT. Integrando questo script nei tuoi flussi di lavoro, potrai semplificare le ricerche dei log degli eventi e migliorare le tue operazioni IT complessive.<\/p>\n","protected":false},"author":35,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"","_lmt_disable":""},"operating_system":[4212],"use_cases":[4269],"class_list":["post-353752","script_hub","type-script_hub","status-publish","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub\/353752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/comments?post=353752"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media?parent=353752"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/operating_system?post=353752"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/use_cases?post=353752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}