So. Frustrated. (but I'm sure it's something small that I'm overlooking!)
I have this code:
Import-Module SQLPS 3>$null
[System.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.SMO.dll") | Out-Null
$sql = " ;WITH CTE_MostRecentJobRun AS
(
-- For each job get the most recent run (this will be the one where Rnk=1)
SELECT job_id
,run_status
,run_date
,run_time
,RANK() OVER (PARTITION BY job_id ORDER BY run_date DESC,run_time DESC) AS Rnk
FROM sysjobhistory
WHERE step_id=0
)
SELECT
name AS [JobName],
run_status
,CONVERT(VARCHAR,DATEADD(S,(run_time/10000)*60*60 /* hours */
+((run_time - (run_time/10000) * 10000)/100) * 60 /* mins */
+(run_time - (run_time/100) * 100) /* secs */
,CONVERT(DATETIME,RTRIM(run_date),113)),100) AS [TimeRun]
FROM CTE_MostRecentJobRun MRJR
JOIN sysjobs SJ ON MRJR.job_id=sj.job_id
WHERE Rnk=1
/* AND run_status=0 */
AND name LIKE 'CN%'
AND [enabled] = 1
ORDER BY name"
$sqlJobs = @(Invoke-Sqlcmd -ServerInstance <MYDBINSTANCE> -Database msdb -Query $sql)
$i = 1
$successCount = 1
Foreach ($sqlJob in $sqlJobs){
Write-Host "Statistic.$($i): "$sqlJob.run_status
Write-Host "Message.$($i): "$sqlJob.JobName.tostring()
$successCount++
$i++}
If ($successCount = $i){
exit 0 }
Else {
exit 1 }
It works perfectly fine when I run it in the ISE, but SAM keeps throwing back "Not Defined". What am I doing wrong?!
Thank you,
Jack Vaughan, Jr.