none
Invoke-command get-eventlog test-connection uniqe message text

    Question

  • Hello there,

    can you please solve me a problem with this code? I am trying to make a universal script for collecting warning and error eventlog for previous months from many servers into one file (could be HTML) grouped by count with unique message text. Also, I would like to use parallel collection for better performance. Problem was, that when I use foreach for testing the result is in one file, but separated into many tabs for each server (not grouped into one html). If I comment the foreach the result is error "Test-Connection : Cannot validate argument on parameter 'ComputerName'."

    $server = Get-Content $resdir\servers.txt
    $resdir = "C:\Script\Eventlog\"
    $resfile = "events.html"
    $outfile = $resdir + $resfile
    $starttime = (get-date -day 1).AddMonths(-1).ToString('MM/dd/yyyy')
    
    
    # Start HTML Output file style 
    some style
    # End HTML Output file style 
    
    
    $code = {
    Get-EventLog application -After $args[0] -EntryType Error, Warning -Newest 10 |
        Group-Object Message |
        % { $_.Group[0] | Add-Member -PassThru -NotePropertyName Count -NotePropertyValue $_.Count } |
        Sort-Object -Descending Count | 
        Select-Object Count, EventID, PSComputerName, Source, EntryType, Message
      
             }
        
    
    If (Test-Connection -ComputerName $server -Quiet)
        {
        $inv =  Invoke-Command -ScriptBlock $code -ComputerName $server -ArgumentList $starttime,$outfile
        $inv | Sort-Object -Descending Count| Select-Object Count, EventID, PSComputerName, Source, EntryType, Message -ExcludeProperty RunspaceId, PSShowComputerName | 
        ConvertTo-Html -head $style | Out-File $outfile -Append
    
        }

    Tuesday, December 4, 2018 1:19 PM

All replies

  • You have to run the script in a loop for each system and run the "Invoke" as a job using the "-AsJob" parameter.

    Note that we do not rewrite or redesign code you have found.  This is not a free consulting forum.


    \_(ツ)_/

    Tuesday, December 4, 2018 2:21 PM