Skip to content

Conversation

SylvainDuran
Copy link
Contributor

Type of Change

  • Bug fix (non-breaking change )
  • New feature (adds functionality)

Purpose

This PR addresses two issues in Set-DbaAgentJobStep:

Bug: RetryAttempts and RetryInterval parameters were incorrectly being set to 0 when not explicitly provided by the user, overwriting existing job step values.

Missing feature: There was no way to remove a proxy from a job step once it was assigned. Users could only add or change proxies, but not remove them.

Approach

The fix uses $PSBoundParameters.ContainsKey() to properly distinguish between:

  • A parameter that was not provided (should leave existing value unchanged)
  • A parameter that was explicitly provided with a value (including 0, $null, or empty string)

For ProxyName:

  • Now accepts $null or empty string to remove the proxy from a job step
  • Uses [string]::IsNullOrEmpty($ProxyName) to handle both $null and empty string cases
  • Sets $jobStep.ProxyName = '' to clear the proxy
  • Adds verbose message "Removing proxy from job step" when proxy is removed

For RetryAttempts and RetryInterval:

  • Changed from if ($null -ne $RetryAttempts) to if ($PSBoundParameters.ContainsKey('RetryAttempts'))
  • This prevents the unintended reset to 0 when these parameters are not specified
  • Now only updates these values when explicitly provided by the user

Commands to test

Test 1: Remove proxy from a job step

# Setup: Create a job step with a proxy
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -ProxyName "MyProxy"

# Remove the proxy by passing $null
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -ProxyName $null -Verbose

# Or remove by passing empty string
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -ProxyName "" -Verbose

# Verify proxy is removed
Get-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" | Select-Object Name, ProxyName

Test 2: Verify RetryAttempts and RetryInterval are not reset

# Setup: Create a job step with retry values
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -RetryAttempts 5 -RetryInterval 10

# Update another property WITHOUT specifying retry parameters
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -Command "SELECT 1" -Verbose

# Verify RetryAttempts and RetryInterval remain at 5 and 10 (not reset to 0)
Get-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" | Select-Object Name, RetryAttempts, RetryInterval

Test 3: Explicitly set retry values to 0

# This should still work - explicitly setting to 0
Set-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" -RetryAttempts 0 -RetryInterval 0 -Verbose

# Verify values are set to 0
Get-DbaAgentJobStep -SqlInstance localhost -Job "TestJob" -StepName "Step1" | Select-Object Name, RetryAttempts, RetryInterval

…ts, and RetryInterval

- Add support for removing proxy by passing $null or empty string to -ProxyName parameter
- Fix RetryAttempts and RetryInterval being incorrectly set to 0 when parameters are not provided
- Use $PSBoundParameters.ContainsKey() to distinguish between "parameter not provided" and "parameter provided with value 0 or $null"
@niphlod
Copy link
Contributor

niphlod commented Oct 14, 2025

Hi @SylvainDuran , not saying this doesn't work but we usually rely on Test-Bound for these kind of checks

… checks to follow dbatools project conventions
@SylvainDuran
Copy link
Contributor Author

Hi @niphlod, thanks for pointing that out! I searched the codebase for PSBoundParameters usage and saw it was being used in some places, but I wasn't aware of the Test-Bound helper function convention in dbatools since I'm not familiar with the project's standards yet.

I've updated the code to use Test-Bound with the -ParameterName parameter for all three checks (ProxyName, RetryAttempts, and RetryInterval). I've tested the changes and everything works perfectly.

The modifications have been pushed to the PR. Thanks again for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants