我面临以下脚本的问题。这是我想用来监视应用程序的某些作业的脚本。我使用调用 sqlcmd 来加载数据集。如果选择查询以 atlast 1 行返回,所有这些都可以正常工作。
如果没有结果(通常是这种情况),那么我会收到错误消息:
>Cannot index into a null array.
At P:\DeploymentTESTING\PowerShell\EVENING_CHECK_JOBS_FL_WITH_DETAILS.ps1:40 char:7
+ IF ($JOBERRORS_INT.Tables[0].Rows.Count -eq 0 -and $LONGRUNNINGJOBS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
当涉及到第一个 if 语句时,它失败了。我对 powershell 比较陌生,我尝试结合 Invoke Sqlcmd 找到这个问题并获得结果 -AS 数据集,但是我找不到,或者无法理解如何处理这个问题。
有人有想法吗?将不胜感激。
[string]$RESULT = "test"
[STRING]$JOB_SERVER_ACC = "SERVERNAME"
[STRING]$JOB_DB_INT = "DATABASE"
[STRING]$QUERY_1 = "SELECT *
FROM [JOB]
WHERE [JOB_STATUS] = 'ERR';"
[STRING]$QUERY_2 = "SELECT *
FROM [JOB]
WHERE JOB_STATUS = 'RUN'
AND DATEDIFF(MINUTE,JOB_LOG_START_DATETIME,sysdatetime()) > 120; "
[STRING]$QUERY_3 = "SELECT COUNT([JOB_NAME])
FROM [JOB]
WHERE JOB_STATUS = 'RUN';"
$JOBERRORS_INT = Invoke-Sqlcmd -ServerInstance $JOB_SERVER_ACC -Database $JOB_DB_INT -Query $QUERY_1 -AS DataSet
$LONGRUNNINGJOBS_INT = Invoke-Sqlcmd -ServerInstance $JOB_SERVER_ACC -Database $JOB_DB_INT -Query $QUERY_2 -AS DataSet
$RUNNINGJOBS_INT = Invoke-Sqlcmd -ServerInstance $JOB_SERVER_ACC -Database $JOB_DB_INT -Query $QUERY_3
IF ($JOBERRORS_INT.Tables[0].Rows.Count -eq 0 -and $LONGRUNNINGJOBS_INT.Tables[0].Rows.Count -eq 0 -and $RUNNINGJOBS_INT.ItemArray[0] -NE 0 )
{
$RESULT = $RESULT + "OK"
}
Else {
$RESULT = $RESULT + "NOT OK - PLEASE LOGON AND CHECK!"
IF ($JOBERRORS_INT.Tables[0].Rows.Count -NE 0) {
$RESULT = $RESULT + "
$($JOBERRORS_INT.TableS[0].Rows.Count) JOBS IN ERROR! "
}
IF ($LONGRUNNINGJOBS_INT.Tables[0].Rows.Count -ne $null) {
$RESULT = $RESULT + "
$($LONGRUNNINGJOBS_INT.Tables[0].Rows.Count) LONG RUNNING JOBS!"
}
IF ($RUNNINGJOBS_INT.ItemArray[0] -EQ 0) {
$RESULT = $RESULT + "
NO JOBS RUNNING AT ALL!"}
}
WRITE-HOST -F DarkYellow $RESULT