apiVersion: batch/v1
kind: CronJob
metadata:
  name: connectsqlfailures
spec:
  schedule: "*/6000 * * * *"
  jobTemplate:
    spec:
      activeDeadlineSeconds: 1200
      template:
        spec:
          containers:
            - name: dbatoolspowershellcontainer
              image: sqldbawithabeard/pwshdbatools:v1
              env:
                - name: HOME
                  value: /tmp
              command:
              - pwsh
              - -Command
              - |
                $Date = Get-Date -Format yyyyMMdd-HHmmsss
                Write-Host "$Date - I am a script running in container"
        
                $Query = "SELECT @@SERVERNAME"
                $Server = '10.244.0.157'

                $Date = Get-Date -Format yyyyMMdd-HHmmsss
                Write-Host "$Date - I am importing dbatools"

                Import-Module /root/.local/share/powershell/Modules/dbatools/1.1.0/dbatools.psd1
                $Date = Get-Date -Format yyyyMMdd-HHmmsss
                Write-Host "$Date - I will try to connect to the sql instance $server"
                $secStringPassword = ConvertTo-SecureString 'wrongpassword' -AsPlainText -Force
                $Username = 'TryingToStealYourData-{0}' -f (Get-Random -Minimum 1000 -Maximum 10000)
                [pscredential]$sqllogin = New-Object System.Management.Automation.PSCredential ($Username, $secStringPassword)
                
                $Results = Invoke-DBaQuery -SQlINstance $server -SqlCredential $$sqllogin -Query $Query -Verbose
                $Date = Get-Date -Format yyyyMMdd-HHmmsss
                $Message =  "{0}- I got {1}" -f $date, ($Results| Out-String)
                Write-Output $Message
          restartPolicy: Never