lowlydba / lowlydba.sqlserver

:spoon: A cross-platform Ansible collection using PowerShell to configure and maintain SQL Server.
https://galaxy.ansible.com/ui/repo/published/lowlydba/sqlserver
GNU General Public License v3.0
19 stars 12 forks source link

[Bug] lowlydba.sqlserver.agent_job_schedule throws error #197

Closed OsirisDBA closed 11 months ago

OsirisDBA commented 1 year ago

Describe the bug When using the enabled flag on agent_job_schedule an error is thrown.

To Reproduce Pass enabled: true to lowlydba.sqlserver.agent_job_schedule:

Expected behavior The schedule would be enabled or disabled.

This is caused by /plugins/modules/agent_job_schedule.ps1 line 105

if ($enabled -eq $true) {
  $scheduleParams.Add("Enabled", $true)
}

Since Set-DbaAgentSchedule only has a Disabled flag this needs flipped to the following.

if ($enabled -eq $true) {
    $scheduleParams.Add("Disabled", $false)
}

Expect a PR shortly.

lowlydba commented 1 year ago

It looks like the command has both: https://github.com/dataplat/dbatools/blob/master/public/Set-DbaAgentSchedule.ps1#L28-L32

what version of DbaTools are you using?

OsirisDBA commented 1 year ago

DBATools 2.0.4 I see what you mean. The underlying dbatools expects either Enabled or Disabled but just checked if each is $true. so Enabled = $false is ignored and Disabled = $false is ignored.

OsirisDBA commented 1 year ago

Yet.... in the latest version on PSGallery

PS C:\> update-module dbatools
PS C:\> get-module dbatools 

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.0.4      dbatools                            {Select-DbaObject, Set-DbatoolsConfig, Add-DbaAgDatabase, Add-DbaAgListener...}

PS C:\> get-help New-DbaAgentSchedule -detailed 

NAME
    New-DbaAgentSchedule

SYNOPSIS
    New-DbaAgentSchedule creates a new schedule in the msdb database.

SYNTAX
    New-DbaAgentSchedule [-SqlInstance] <Dataplat.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential] <Pscredential>] [[-Job] <System.Object[]>] [[-Schedule]
    <System.Object>] [-Disabled <Switch>] [[-FrequencyType] <System.Object>] [[-FrequencyInterval] <System.Object[]>] [[-FrequencySubdayType] <System.Object>]
    [[-FrequencySubdayInterval] <Int>] [[-FrequencyRelativeInterval] <System.Object>] [[-FrequencyRecurrenceFactor] <Int>] [[-StartDate] <String>] [[-EndDate] <String>] [[-StartTime]  
    <String>] [[-EndTime] <String>] [-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]

DESCRIPTION
    New-DbaAgentSchedule will help create a new schedule for a job.

    If the job parameter is not supplied the schedule will not be attached to a job.

PARAMETERS
    -Disabled [<Switch>]
        Set the schedule to disabled. Default is enabled

    -EnableException [<Switch>]
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

    -EndDate [<String>]
        The date on which execution of a job can stop.

        If force is used the end date will be '9999-12-31'

    -EndTime [<String>]
        The time on any day to end execution of a job. Format HHMMSS / 24 hour clock.
        Example: '010000' for 01:00:00 AM.
        Example: '140000' for 02:00:00 PM.

        If force is used the start time will be '23:59:59'

    -Force [<Switch>]
        The force parameter will ignore some errors in the parameters and assume defaults.
        It will also remove the any present schedules with the same name for the specific job.

    -FrequencyInterval [<System.Object[]>]
        The days that a job is executed

        Allowed values for FrequencyType 'Daily': EveryDay or a number between 1 and 365.
        Allowed values for FrequencyType 'Weekly': Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Weekdays, Weekend or EveryDay.
        Allowed values for FrequencyType 'Monthly': Numbers 1 to 31 for each day of the month.

        If "Weekdays", "Weekend" or "EveryDay" is used it over writes any other value that has been passed before.

        If force is used the default will be 1.

    -FrequencyRecurrenceFactor [<Int>]
        The number of weeks or months between the scheduled execution of a job.

        FrequencyRecurrenceFactor is used only if FrequencyType is "Weekly", "Monthly" or "MonthlyRelative".

    -FrequencyRelativeInterval [<System.Object>]
        A job's occurrence of FrequencyInterval in each month, if FrequencyInterval is 32 (monthlyrelative).

        Allowed values: First, Second, Third, Fourth or Last

    -FrequencySubdayInterval [<Int>]
        The number of subday type periods to occur between each execution of a job.

    -FrequencySubdayType [<System.Object>]
        Specifies the units for the subday FrequencyInterval.

        Allowed values: 'Once', 'Time', 'Seconds', 'Second', 'Minutes', 'Minute', 'Hours', 'Hour'

        The following synonyms provide flexibility to the allowed values for this function parameter:
        Once=Time
        Seconds=Second
        Minutes=Minute
        Hours=Hour

    -FrequencyType [<System.Object>]
        A value indicating when a job is to be executed.

        Allowed values: 'Once', 'OneTime', 'Daily', 'Weekly', 'Monthly', 'MonthlyRelative', 'AgentStart', 'AutoStart', 'IdleComputer', 'OnIdle'

        The following synonyms provide flexibility to the allowed values for this function parameter:
        Once=OneTime
        AgentStart=AutoStart
        IdleComputer=OnIdle

        If force is used the default will be "Once".

    -Job [<System.Object[]>]
        The name of the job that has the schedule.

    -Schedule [<System.Object>]
        The name of the schedule.

    -SqlCredential [<Pscredential>]
        Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).

        Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.

        For MFA support, please use Connect-DbaInstance.

    -SqlInstance [<Dataplat.Dbatools.Parameter.DbaInstanceParameter[]>]
        The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2000 or greater.

    -StartDate [<String>]
        The date on which execution of a job can begin.

        If force is used the start date will be the current day

    -StartTime [<String>]
        The time on any day to begin execution of a job. Format HHMMSS / 24 hour clock.
        Example: '010000' for 01:00:00 AM.
        Example: '140000' for 02:00:00 PM.

        If force is used the start time will be '00:00:00'

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
OsirisDBA commented 1 year ago

Okay I see the issue, dbatools has development as the default branch and they tag their releases as commits on the development branch. You can see this here: https://github.com/dataplat/dbatools/commit/cb4fec178bdc37f6a50716769dcadce517da1199

And in the development branch, there is only the Disabled switch. So this bug is valid.

lowlydba commented 12 months ago

Thanks for confirming that, looking forward to the PR!

lowlydba commented 11 months ago

This actually looks to be working still with the latest 2.1.2 version, but since its all equivalent I will still make the update to get compatibility with whatever previous version had this breakage.

2.1.2:

PS C:\Users\johnh\Documents\GitHub\ansible\ansible_collections\lowlydba\sqlserver> Get-Module dbatools

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.1.2                 dbatools                            {Select-DbaObject, Set-DbatoolsConfig, Add-DbaAgDatabase, Add-DbaAgListener…}

PS C:\Users\johnh\Documents\GitHub\ansible\ansible_collections\lowlydba\sqlserver> Get-Help Set-DbaAgentSchedule -detailed

NAME
    Set-DbaAgentSchedule

SYNOPSIS
    Set-DbaAgentSchedule updates a schedule in the msdb database.

SYNTAX
    Set-DbaAgentSchedule [-SqlInstance] <DbaInstanceParameter[]> [[-SqlCredential] <PSCredential>] [-Job] <Object[]> [-ScheduleName] <String> [[-NewName] <String>] [-Enabled] [-Disabled] [[-FrequencyType] <Object>] [[-FrequencyInterval] <Object[]>] [[-FrequencySubdayType] <Object>]
    [[-FrequencySubdayInterval] <Int32>] [[-FrequencyRelativeInterval] <Object>] [[-FrequencyRecurrenceFactor] <Int32>] [[-StartDate] <String>] [[-EndDate] <String>] [[-StartTime] <String>] [[-EndTime] <String>] [-EnableException] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
    Set-DbaAgentSchedule will help update a schedule for a job. It does not attach the schedule to a job.

PARAMETERS
    -SqlInstance <DbaInstanceParameter[]>
        The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2000 or greater.

    -SqlCredential <PSCredential>
        Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).

        Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.

        For MFA support, please use Connect-DbaInstance.

    -Job <Object[]>
        The name of the job that has the schedule.

    -ScheduleName <String>
        The name of the schedule.

    -NewName <String>
        The new name for the schedule.

    -Enabled [<SwitchParameter>]
        Set the schedule to enabled.

    -Disabled [<SwitchParameter>]
        Set the schedule to disabled.

...
lowlydba commented 11 months ago

The tests kept failing, and on closer inspection setting disabled = false is not the same as enabled = true in the command. I'm going to close this for now since it appears to be working as expected on the latest dbatools version.