Cant Subtract?

Welcome Forums General PowerShell Q&A Cant Subtract?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 years, 4 months ago.

  • Author
    Posts
  • #50332

    Participant
    Points: 0
    Rank: Member
    $now = Import-Csv C:\Test\Test1.csv
    $prev = Import-Csv C:\Test\Test2.csv
    
    $Compare = Compare-Object -ReferenceObject $now -DifferenceObject $prev -Property Event -IncludeEqual -PassThru
    $Compare
    
        foreach ($c in $Compare) {
    
            if($c.SideIndicator = "=="){
            $old = $prev | Select-Object -ExpandProperty Occurred
            $new = $c | Select-Object -ExpandProperty Occurred
    
            $diff = $old - $new
            $diff
            
            }
    
        }
    
    
    #test1.csv
    #TYPE Selected.System.Data.DataRow
    #"Event","Occurred","EntryType","Description","Computer"
    #"","","","Application",""
    #"102","3","Information","Music.UI (7152) {E1D728C7-3984-492F-A89E-C7CE4C191838}: The database engine (10.00.10586.0000) is starting a new instance (0).",""
    #"105","3","Information","Music.UI (7152) {E1D728C7-3984-492F-A89E-C7CE4C191838}: The database engine started a new instance (0). (Time=0 seconds)",""
    #"23","15","Information","NIC /DEVICE/{C05ABC8A-1772-4246-A2AE-A18D935B2AF6} (Friendly Name: Microsoft Network Adapter Multiplexor Driver) is now operational.",""
    
    #test2.csv
    #TYPE Selected.System.Data.DataRow
    #"Event","Occurred","EntryType","Description","Computer"
    #"23","28","Information","NIC /DEVICE/{C05ABC8A-1772-4246-A2AE-A18D935B2AF6} (Friendly Name: Microsoft Network Adapter Multiplexor Driver) is now operational.",""
    
    

    Test1 and Test2 CSV are holding just some sample data I am trying to implement in a much larger script thing.
    The error I keep getting is something about InvalidOperation: (op_Subtraction:String).

    My ideal situation is I would like to compare some event logs saved as CSV.
    If the logs have the same EventID "==" slide indicator
    Then I would like to know how many more or less times they Occurred.

    Event         : 
    Occurred      : 
    EntryType     : 
    Description   : Application
    Computer      : 
    SideIndicator : ==
    
    Event         : 23
    Occurred      : 15
    EntryType     : Information
    Description   : NIC /DEVICE/{C05ABC8A-1772-4246-A2AE-A18D935B2AF6} (Friendly Name: Microsoft Network Adapter Multiplexor Driver) is now operational.
    Computer      : LAPTOP-HTS55TQ4
    SideIndicator : ==
    
    Event         : 102
    Occurred      : 3
    EntryType     : Information
    Description   : Music.UI (7152) {E1D728C7-3984-492F-A89E-C7CE4C191838}: The database engine (10.00.10586.0000) is starting a new instance (0).
    Computer      : LAPTOP-HTS55TQ4
    SideIndicator : < =
    
    Event         : 105
    Occurred      : 3
    EntryType     : Information
    Description   : Music.UI (7152) {E1D728C7-3984-492F-A89E-C7CE4C191838}: The database engine started a new instance (0). (Time=0 seconds)
    Computer      : LAPTOP-HTS55TQ4
    SideIndicator : <=
    
    Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
    At C:\Users\seatt\Documents\Compre-Stuff-1.ps1:13 char:9
    +         $diff = $old - $new
    +         ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
    At C:\Users\seatt\Documents\Compre-Stuff-1.ps1:13 char:9
    +         $diff = $old - $new
    +         ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
    At C:\Users\seatt\Documents\Compre-Stuff-1.ps1:13 char:9
    +         $diff = $old - $new
    +         ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.
    At C:\Users\seatt\Documents\Compre-Stuff-1.ps1:13 char:9
    +         $diff = $old - $new
    +         ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    
  • #50334

    Participant
    Points: 25
    Rank: Member

    You need to subtract numbers and you're trying subtract arrays.
    Use the count property to return the number of objects in the arrays and subtract one from the other:

    $diff = $old.count - $new.count
    

The topic ‘Cant Subtract?’ is closed to new replies.