Fuzzy Compare dates on 2 objects

Hi

I have 2 system.array objects and I want to compare the lastmodified dates on them. I want to list rows where the name matches but the lastmodified date is over 1 hour out

The objects in question are the output from get-dbaagentjob, I’m comparing the agent job list on two nodes of a cluster and if the lastmodified dates are over an hour out I’d like it flagged.

Any suggestions on doing a fuzzy compare?

Cheers

Alex

AlexP01482,
Welcome to the forum. :wave:t3:

First and foremost - please post your code (formatted as code, please). :point_up:t3: … and maybe some sample data and maybe how the expected output should look like … :wink:

You can use New-TimeSpan to calculate the difference between two [DateTime] values. :man_shrugging:

After lunch and a really good cup of tea I decided I’d just write it very basically, it’s not bringing back loads of data so I don’t need hash tables or anything.

I’ve ended up with this

$PrimAgentJobs = Get-DbaAgentJob -SqlInstance NodeA -ExcludeDisabledJobs
$SecAgentJobs = Get-DbaAgentJob -SqlInstance NodeB -ExcludeDisabledJobs

foreach ($PrimAgentJob in $PrimAgentJobs)
{
foreach ($SecAgentJob in $SecAgentJobs)
{
if ($PrimAgentjob.name -eq $SecAgentJob.name)
{
$timediff = ($PrimAgentjob.DateLastModified - $SecAgentJob.DateLastModified).hours
if ($timediff -ne 0)
{
write-host $timediff $PrimAgentjob.name $PrimAgentjob.DateLastModified + $SecAgentJob.DateLastModified
}
}
}
}

The new-timespan function looks great though. I’ll be using that at some point in the future

Great you’ve found a solution yourself. :+1:t3:

Just to show you the difference between unformatted code and formatted code here in the forum …

$PrimAgentJobs = Get-DbaAgentJob -SqlInstance NodeA -ExcludeDisabledJobs
$SecAgentJobs  = Get-DbaAgentJob -SqlInstance NodeB -ExcludeDisabledJobs

foreach ($PrimAgentJob in $PrimAgentJobs) {
    foreach ($SecAgentJob in $SecAgentJobs) {
        if ($PrimAgentjob.name -eq $SecAgentJob.name) {
            $timediff = ($PrimAgentjob.DateLastModified - $SecAgentJob.DateLastModified).hours
            if ($timediff -ne 0) {
                write-host $timediff $PrimAgentjob.name $PrimAgentjob.DateLastModified + $SecAgentJob.DateLastModified
            }
        }
    }
}

So please, next time when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Yeah I looked for a code formatting button but couldn’t (and still can’t) see one.

I’d have expected to see it here

image

For new members there’s a post pinned to the top of the list explaining how to do it … even without a dedicated button. :wink: