$envCountry = 'CH'
#import Country settings
. .\..\.env\Import-EnvironmentSettings.ps1 -envCountry $envCountry


$svcPassword = ConvertTo-SecureString -String $ADPassword -AsPlainText -Force #Muszę to zmienić ponieważ nie jest bezpiecznie trzymać te informacje w pliku płaskim
$ADCredential = New-Object System.Management.Automation.PSCredential("$ActiveDirectoryDomain\$ADAccount", $svcPassword)

function Get-Instance {

    foreach ($instance in $servers) {
        if($instance[4]) {
            [array]$sqlinstances += $instance[1] + '\' + $instance[4]
        } else {
            [array]$sqlinstances += $instance[1]
        }
    }
    if ($null -ne $sqlinstances) {
        return [array]$sqlinstances
    }
    if ($sqlinstances.Length -eq 0) {
        Write-PSFMessage -Level Warning -Message "You must specify a list of servers"
        return
    }
    else {
        [array]$sqlinstances
    }

}
@(Get-Instance).ForEach{
    $instance = $psitem
    try {
        $InstanceSMO = Connect-DbaInstance	-SqlInstance $instance -SqlCredential $ADCredential  -ErrorAction SilentlyContinue -ErrorVariable errorvar
    } catch {
        
        $NotContactable += $instance
    }
    if ($NotContactable -contains $instance) {
        Context "Checking for failed enabled jobs on $instance" {
            It "Can't Connect to $instance" {
                $false | Should -BeTrue -Because 'The instance should be available to be connected to!'
            }
        }
    }
    if ($NotContactable -notcontains $instance) {
        if ($null -eq $InstanceSMO.version) {
            $NotContactable += $instance
        } elseif (($InstanceSMO).Edition -like 'Express Edition*') { }
        else {
            BeforeAll {

            }
            Describe "Failed Jobs" -Tags FailedJob {
                $maxdays = 100
                $startdate = (Get-Date).AddDays( - $maxdays)
                $excludecancelled = $true
                Context "Checking for failed enabled jobs since $startdate on $instance -> " {
                    @(Get-DbaAgentJob -SqlInstance $instance -SqlCredential $ADCredential | Where-Object Category -eq "Database Maintenance" ).ForEach{
                        
                        if ($_.LastRunOutcome -eq 'Unknown') {
                            It -Skip "1. We chose to skip this as $psitem's last run outcome is unknown on $($_.SqlInstance)" {
                                $_.LastRunOutcome | Should -Be 'Succeeded' -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
                            }
                        } elseif (($_.LastRunOutcome -eq 'Cancelled') -and ($excludecancelled -eq $true)) {
                            It -Skip "2. We chose to skip this as $psitem's last run outcome is cancelled on $($_.SqlInstance)" {
                                $_.LastRunOutcome | Should -Be 'Succeeded' -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
                            }
                        } elseif ($_.LastRunOutcome -eq 'Succeeded'){
                            # $a = $_.LastRunOutcome
                            It "$_.LastRunOutcome's last run outcome is $($_.LastRunOutcome) on $($_.SqlInstance)" {
                                $a = 'Succeeded'
                                $a | Should -Be 'Succeeded'
                            }
                        } else {
                            It "$_.LastRunOutcome's last run outcome is $($_.LastRunOutcome) on $($_.SqlInstance)" {
                                $a = 'Failed'
                                $a | Should -Be 'Succeeded'
                            }
                        }
                    }
                }
            }
        }
    }
    $NotContactable
}
