简体   繁体   English

使用 PowerShell 获取 SQL 作业计划的值

[英]Using PowerShell to get the values of a SQL job schedule

Currently, I am getting the job schedule for an existing SQL job and I want to get the values of how frequently it runs ie Daily, Weekly, Monthly, etc then I want to get when it should run next, and if the job runs on the weekends.目前,我正在获取现有 SQL 作业的作业计划,我想获取它运行频率的值,即每天、每周、每月等,然后我想知道它应该何时运行,以及作业是否继续运行周末。 I understand how to get all that information by doing我了解如何通过执行获取所有信息

Get-SqlAgent -ServerInstance "$SERVER" | Get-SqlAgentJob $job | Get-SqlAgentJobSchedule | Format-List -Property *

This shows me all the relative information I need这显示了我需要的所有相关信息

Parent                     : Test_Job
ActiveEndDate              : 12/31/9999 12:00:00 AM
ActiveEndTimeOfDay         : 23:59:59
ActiveStartDate            : 3/4/2020 12:00:00 AM
ActiveStartTimeOfDay       : 00:00:00
DateCreated                : 3/4/2020 2:08:00 PM
FrequencyInterval          : 1
FrequencyRecurrenceFactor  : 0
FrequencyRelativeIntervals : First
FrequencySubDayInterval    : 2
FrequencySubDayTypes       : Hour
FrequencyTypes             : Daily
IsEnabled                  : True
JobCount                   : 1

I am looking at microsofts page on how to understand all the frequency information, but so far it seems like the only option is to have a bunch of nested IF statements that determine how often it runs.我正在查看有关如何理解所有频率信息的 microsofts 页面,但到目前为止,似乎唯一的选择是使用一堆嵌套的 IF 语句来确定它的运行频率。 I can do it this way, but I figured there has to be a cleaner way to get the information I need.我可以这样做,但我认为必须有一种更简洁的方式来获取我需要的信息。 This is how I am currently parsing the information这就是我目前解析信息的方式

if($frequency -eq "DAILY")
    {
        $MINUTES_LATEST_RUN_SUCCESS = "1500"
        #Code here to see how often it runs in a day

    }
    elseif($frequency -eq "WEEKLY")
    {
        $MINUTES_LATEST_RUN_SUCCESS = "11520"
        #Code here to see how how many days a week it runs

    }
    elseif($frequency -eq "MONTHLY")
    {
        $MINUTES_LATEST_RUN_SUCCESS = "50400"
       #Code here to see how which day it runs a month
    }
    else
    {
        $MINUTES_LATEST_RUN_SUCCESS = "1500"
    }

I figured this can't be the best approach.我认为这不是最好的方法。

Anytime you get into many if/then statements, it's time to use a switch.每当您遇到许多 if/then 语句时,就该使用 switch 了。 There are sample code blocks in the PowerShell ISE (CRTL+J) and VSCode (CRTl+ALT+J) for this idea. PowerShell ISE (CRTL+J) 和 VSCode (CRTl+ALT+J) 中有针对此想法的示例代码块。

$a = 5
switch ($a) 
{ 
    1 {"The color is red."} 
    2 {"The color is blue."} 
    3 {"The color is green."} 
    4 {"The color is yellow."} 
    5 {"The color is orange."} 
    6 {"The color is purple."} 
    7 {"The color is pink."}
    8 {"The color is brown."}
    default {"The color could not be determined."}
}

Yours, but of course add your other code as needed in the block.您的,但当然可以根据需要在块中添加其他代码。

$frequency = 'DAILY'
switch ($frequency) 
{ 
    DAILY {"The job is run $frequency"} 
    WEEKLY {"The job is run $frequency"} 
    MONTHLY {"The job is run $frequency"} 
    default {$MINUTES_LATEST_RUN_SUCCESS = "1500"}
}

我参加聚会有点晚了,但我也在寻找有关频率间隔的文档,因为 Powershell 页面上没有适当的文档,但遇到了这个https://docs.microsoft.com/en-us /sql/relational-databases/system-tables/dbo-sysschedules-transact-sql?view=sql-server-ver15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM