Powershell SQL JobServer开始运行日期或上次运行结果

时间:2014-01-28 11:27:16

标签: sql-server powershell powershell-v3.0

伙计们,希望你们一切顺利。

我正在尝试找到一个PowerShell示例,它可以帮助我找出备份作业是否无法启动或无法启动,即启动。

到目前为止,我可以通过使用Name方法找出作业名称,是否通过IsEnabled函数启用作业,以及通过LastRUnOutcome函数查看作业的上次运行结果(if = 1即if它成功打印“成功”或其他1“失败”。)

问题在于,由于上述原因,我们不知道该工作是否已执行。因此,例如,当工作没有开始时,我们仍然会获得成功(即启用)的结果,但成功启用的工作并不意味着它是一个成功的开始工作和完成的工作。

在代码方面,我失败的地方(并且缺乏powershell oomphh)是当我尝试拉入作业的开始运行日期时,即“作业开始”上方的步骤2或与上次运行密切相关的任何其他功能日期或上次运行日期的时间。

环境设置 - 我的计算机本地: PowerShell V3.0 SQL 2005 .Net 2 - 在服务下禁用 .Net 4 - 未运行,在服务下自动

在服务器本地服务上: .Net 2.0& 3.0 MSSQLSERVER SQL SERVER AGENT(MSQLSERVER)

在服务器上正确 - 服务器管理工​​具: 4.0

对象库:

# Create an SMO Server object
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $sqlserver;
# Create jobs variable from object
$jobsHistory=$srv.JobServer.Jobs
# Create an SMO connection to the instance for job history methods
$jobHistoryFilter = New-Object "Microsoft.SqlServer.Management.Smo.Agent.JobHistoryFilter" $sqlserver;

想法尝试:

$jobName = $job.Name; #jobs name
$jobEnabled = $job.IsEnabled; #step 1 job is enabled
$jobLastRunDate = $jobsHistoryFilter.StartRunDate  #step 2 job started date (result of executing backup i.e. if date was today it successfully started)
#$executionResult #step 3 job result(post execution result/did it backup or not, i.e. successful back or not)
#$jobLastRunOutcome = $job.LastRunOutcome; #step 4 jobs last run result

从上面我错了,我需要做什么和/或多少步骤来完成1.工作启用,2。工作开始结果(开始执行)和3.工作结果 - 成功或失败(开始执行后)。三个步骤够了吗?或者我需要4?

我感觉这与我的环境有关,也许SQL2005中的函数与PowerShell 3.0不兼容。

请解释为什么你给我的功能,即他们在形成我的答案时的用处。

所有的帮助都非常感激,因为这让我烦恼!

整个代码如下所示,除此之外的任何事情,你太爱管闲事了;):

# Load SMO extension
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;

# Get List of sql servers to check
$sqlservers = Get-Content $sqlserverlistpath;

""
"---------DAILY CHECKLIST AUTOMATION----------"
"---------------SAM v1.0.5---------------"
"----------------15th Jan 2013---------------"
""

"----------SQL SERVER & BACKUP CHECK----------"
""

#output some information onto our excel sheet

$intRow = 0
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1) = "Daily Checklist for system:"
$Sheet.Cells.Item($intRow,2) =  $systemname
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 37
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
            $intRow = $intRow + 1

$Sheet.Cells.Item($intRow,1) = "Date:"
$Sheet.Cells.Item($intRow,2) = $date
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 37
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
            $intRow = $intRow + 1
            $intRow = $intRow + 1

$Sheet.Cells.Item($intRow,1) = "SQL JOBS"
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 37
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1) = "Servername"
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,2) = "Job Name"
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
$Sheet.Cells.Item($intRow,3) = "Enabled"
$Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,3).Font.Bold = $True

$Sheet.Cells.Item($intRow,4) = "Last Ran Date"
$Sheet.Cells.Item($intRow,4).Font.Bold = $True
$Sheet.Cells.Item($intRow,4).Interior.ColorIndex = 19


$Sheet.Cells.Item($intRow,5) = "Status"
$Sheet.Cells.Item($intRow,5).Font.Bold = $True
$Sheet.Cells.Item($intRow,5).Interior.ColorIndex = 19

#added columns to record check person, date, actions
$Sheet.Cells.Item($intRow,6).Font.Bold = $True
$Sheet.Cells.Item($intRow,6).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,6) = "Checked By"
$Sheet.Cells.Item($intRow,7).Font.Bold = $True
$Sheet.Cells.Item($intRow,7).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,7) = "Date"
$Sheet.Cells.Item($intRow,8).Font.Bold = $True
$Sheet.Cells.Item($intRow,8).Interior.ColorIndex = 19
$Sheet.Cells.Item($intRow,8) = "Actions (if any)"



$WorkBook = $Sheet.UsedRange

$intRow = $intRow + 1


# Loop through each sql server from sqlservers.txt
foreach($sqlserver in $sqlservers)
{
      # Create an SMO Server object
      #$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $sqlserver;


      $srv = new-object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $sqlserver
      $srv.JobServer.Jobs | where {$_.IsEnabled} | Select Name, HasSchedule, LastRunDate, LastRunOutcome



      # Jobs counts
      $totalJobCount = $srv.JobServer.Jobs.Count;
      $failedCount = 0;
      $successCount = 0;
      $notRunOrNotExistCount = 0;

      Write-Host -ForegroundColor white "=========================================================================================";
      Write-Host -ForegroundColor white "$sqlserver | $sqlserver | $sqlserver | $sqlserver | $sqlserver | $sqlserver ";
      Write-Host -ForegroundColor white "=========================================================================================";  
      ""


# For each jobs on the server
foreach($job in $srv.JobServer.Jobs)
{
            # Default write colour
            $colour = "Green";
            $jobName = $job.Name;
            $jobEnabled = $job.IsEnabled;                      #step 1 job is enabled
           # $jobLastRunDate = $srv.JobServer.Jobs.LastRunDate; #step 2 job started date (result of executing backup i.e. if date was today it successfully started)
            #$executionResult =                                #step 3 job result  (post execution result/did it backup or not, i.e. successful back or not)
            #$jobLastRunOutcome = $job.LastRunOutcome;         #step 4 job result  what this produce?






            # Set write text to red for Failed jobs
          # if($jobLastRunOutcome -eq "Failed")
          # {
               #    $colour = "Red";
               #    $failedCount += 1;

                   #write status
                #   $Sheet.Cells.Item($intRow,2) = $jobname
               #    $Sheet.Cells.Item($intRow,6) = "Failed"
               #    $Sheet.Cells.Item($intRow,6).Interior.ColorIndex = 3

               #    $Sheet.Cells.Item($intRow,3) = $jobEnabled

           # }#otherwise it is successful
          # if ($jobLastRunOutcome -eq "Succeeded")
           # {
               #   $successCount += 1;

                  #write status
                #  $Sheet.Cells.Item($intRow,2) = $jobname
                #  $Sheet.Cells.Item($intRow,5) = "Succeeded"
                #  $Sheet.Cells.Item($intRow,5).Interior.ColorIndex = 4

                #  $Sheet.Cells.Item($intRow,3) = $jobEnabled

           # }#otherwise it has failed to start running
           # elseif($jobEnabled -eq $FALSE) #iffalse
           # {


             #     $notRunOrNotExistCount += 1;

                  #write status
              #    $Sheet.Cells.Item($intRow,2) = $jobname
              #    $Sheet.Cells.Item($intRow,3) = "Not Started"
              #    $Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 46

              #    $colour = "Red";

              #    $failedCount += 1;
               #   $Sheet.Cells.Item($intRow,5) = "Failed"
              #    $Sheet.Cells.Item($intRow,5).Interior.ColorIndex = 3


           if($job.IsEnabled -and $job.HasSchedule)  # }elseif($job.IsEnabled -and $job.HasSchedule) #$jobEnabled = 1) #true)
            {

                  $Name = $job.Name
                  $LastRunOutcome = $job.LastRunOutcome
                  $LastRunDate = $job.LastRunDate

                  #$notRunOrNotExistCount += 1;


                  #write status
                 # $Sheet.Cells.Item($intRow,2) = $jobname

                  #Enabled
                  #$Sheet.Cells.Item($intRow,3) = "Started" #$jobEnabled
                 # $Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 4

                  #LastRunDate
                  $Sheet.Cells.Item($intRow,4) = "$LastRunDate"
                  $Sheet.Cells.Item($intRow,4).Interior.ColorIndex = 4



                 # $successCount += 1;

                  #status
                 # $Sheet.Cells.Item($intRow,5) = "Succeeded"
                #  $Sheet.Cells.Item($intRow,5).Interior.ColorIndex = 4

                  #checked by
                  #$Sheet.Cells.Item($intRow,6) = "SAM"
                #  $Sheet.Cells.Item($intRow,6).Interior.ColorIndex = 20

                  #$Sheet.Cells.Item($intRow,3) = $jobEnabled 

            }


            Write-Host -ForegroundColor $colour "SERVER = $sqlserver JOB = $jobName ENABLED = $jobEnabled LASTRUNDATE = $LastRunDate LASTRUN = $jobLastRunOutcome";
            $Sheet.Cells.Item($intRow,1) = $sqlserver

            #give the cells we type into some colour

            Write-Output "$Name,   $LastRunOutcome,   $LastRunDate"

            $Sheet.Cells.Item($intRow,7).Interior.ColorIndex = 20
            $Sheet.Cells.Item($intRow,8).Interior.ColorIndex = 20

            $intRow = $intRow + 1

      }

      # Writes a summary for each SQL server
      Write-Host -ForegroundColor white "=========================================================================================";
      Write-Host -ForegroundColor white "$sqlserver total jobs = $totalJobCOunt, success count $successCount, failed jobs = $failedCount.";
      Write-Host -ForegroundColor white "=========================================================================================";     
}

2 个答案:

答案 0 :(得分:0)

不确定我是否正在追踪你所追求的目标。 LastRunDate是最后一次执行作业,LastRunOutcome显示它是否在上次运行时成功。如果您只想快速报告已启用的作业,则会列出它们。我添加了HasSchedule,因为可以启用一项工作但是没有计划。

$srv = new-object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $sqlserver
$srv.JobServer.Jobs | where {$_.IsEnabled} | Select Name, HasSchedule, LastRunDate, LastRunOutcome

阅读完评论后,我想您可能希望使用foreach一次循环一次。

$srv = new-object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $sqlserver
$srv.JobServer.Jobs | where {$_.IsEnabled} | Select Name, HasSchedule, LastRunDate, LastRunOutcome | `
foreach { $Name = $_.Name; $LastRunOutcome = $_.LastRunOutcome; $LastRunDate = $_.LastRunDate; `
          Write-Output "$Name,   $LastRunOutcome,   $LastRunDate" }

或者是一个更具可读性的共同体。

foreach ($job in $srv.JobServer.Jobs) {
   if ($job.IsEnabled -and $job.HasSchedule) {
      $Name = $job.Name
      $LastRunOutcome = $job.LastRunOutcome
      $LastRunDate = $job.LastRunDate
      Write-Output "$Name,   $LastRunOutcome,   $LastRunDate"
   }
}

答案 1 :(得分:0)

我可以在24小时后自己回答。

我所做的只是使用相同的对象类和同一类可用的方法然后添加一个新变量并调用它:

 #Create an SMO Server object
 $srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $sqlserver;

添加一个新变量:

 $jobLastRunDate = $job.LastRunDate;

称之为:

#write status
$Sheet.Cells.Item($intRow,4) = "$jobLastRunDate"

我现在也可以,如果我愿意的话,可以将结果状态与它并排:

 #write status
    $Sheet.Cells.Item($intRow,4) = "$jobLastRunDate"
    $Sheet.Cells.Item($intRow,5) = "Succeeded"

或者更进一步,将上次运行日期的时间与检查的设定运行时间相匹配,如果它同时运行,则表示成功。

#find the execution time of check in SQL Server
$setExecutionTimeofCheck = $someServerObjectofExecutionSetTime


#write status
$Sheet.Cells.Item($intRow,4) = "$jobLastRunDate"

if($jobLastRunDate => $setExecutionTimeofCheck)
{
    $Sheet.Cells.Item($intRow,5) = "Succeeded"
}

但看起来我正试图过度复杂化,因为我得到的当前结果低于它们并且它们在不同的设定时间运行!

DB backup           1 01/30/2014 23:00:00
DB backup           2 01/26/2014 06:00:00
History             1 01/31/2014 02:00:00
Transaction backup    1 01/31/2014 13:00:00

那是另一天我把它带走了吗? ;)

非常感谢布鲁斯。还有其他人!