<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    Test-MyTestFunction -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Invoke-ITDSqlCmdJob {
    [CmdletBinding()]
    param(
        [string]
        $ServerInstance,

        [string]
        $Database,

        [string]
        $Query,

        [PSCredential]
        $Credential
    ) 
    
    begin {
        
    }
    
    process {
        
        $ScriptBlock = {
            param($ServerInstance, $Database, $Query) 
            $ErrorActionPreference = 'Stop';

            return [PSCustomObject]@{
                Result = (Invoke-Sqlcmd -ServerInstance $ServerInstance -Query $Query -Database $Database)
            }
        }

        $Job = Start-Job -Credential $Credential -ScriptBlock $ScriptBlock -ArgumentList $ServerInstance, $Database, $SqlQuery
        (Wait-Job -Job $Job | Receive-Job -Keep).Result
        #$Result = $Job | Receive-Job -Keep
    }
    
    end {
        #Write-Output $Result
    }
}



<#$ScriptBlock = {
    param($SqlServer, $Database, $Query) 
    $ErrorActionPreference = 'Stop';

    return @{ Result = Invoke-Sqlcmd -ServerInstance $SqlServer -Query $Query -Database $Database }
}

$Job = Start-Job -Credential $Credential -ScriptBlock $ScriptBlock -ArgumentList $SqlServer,$Database,$SqlQuery#>