Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 1, 2025

This PR implements the Assert-SqlLogin command as requested in issue #2111. The command validates that a specified principal exists as a SQL Server login and throws a terminating error if it doesn't exist.

Features

  • Primary Function: Asserts that a SQL Server principal exists as a login
  • Error Handling: Throws terminating error with proper ErrorId (ASL0001) for non-existent logins
  • SMO Integration: Uses SQL Server Management Objects ($ServerObject.Logins[$Principal]) instead of T-SQL
  • Pipeline Support: Accepts ServerObject parameter from pipeline
  • Localization: Error messages stored in SqlServerDsc.strings.psd1

Parameters

Parameter Type Mandatory Pipeline Description
Principal String Yes No The principal name to validate
ServerObject Microsoft.SqlServer.Management.Smo.Server Yes Yes SQL Server connection object

Usage Examples

# Basic usage
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Assert-SqlLogin -ServerObject $serverObject -Principal 'DOMAIN\MyUser'

# Pipeline usage
$serverObject | Assert-SqlLogin -Principal 'sa'

# Will throw terminating error for non-existent login
Assert-SqlLogin -ServerObject $serverObject -Principal 'NonExistentUser'

Testing

  • Unit Tests: 11 comprehensive test cases covering existing logins, non-existent logins, pipeline input, and error scenarios
  • Integration Tests: 6 test cases for real SQL Server environments including system logins (sa, NT AUTHORITY\SYSTEM)
  • Coverage: All code paths and error conditions tested
  • Validation: Manual testing confirms proper SMO usage and error handling

Implementation Details

The command follows established patterns in the SqlServerDsc module:

  • Uses [CmdletBinding()] with proper parameter attributes
  • Implements localized error messages with placeholders
  • Uses $PSCmdlet.ThrowTerminatingError() for proper error handling
  • Includes comprehensive comment-based help with examples
  • Supports verbose output for successful validations

Fixes #2111

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.powershellgallery.com
    • Triggering command: /opt/microsoft/powershell/7/pwsh -s -NoLogo -NoProfile -wd /home/REDACTED/work/SqlServerDsc/SqlServerDsc (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


This change is Reviewable

@johlju johlju closed this Aug 1, 2025
@johlju johlju deleted the copilot/fix-ab127889-8caa-48e5-8019-33f3c2d47b64 branch August 1, 2025 13:38
Copilot AI restored the copilot/fix-ab127889-8caa-48e5-8019-33f3c2d47b64 branch August 1, 2025 13:39
Copilot AI changed the title [WIP] Create the command suggest in this issue @dsccommunity/SqlServerDsc/issues/2111 . The command should throw an terminating error if the specified SQL Server principal does not exist as a SQL server login. The command should use SQL Server SMO. The comma... Add Assert-SqlLogin command to validate SQL Server login existence Aug 1, 2025
Copilot AI requested a review from johlju August 1, 2025 13:56
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.

Assert-SqlLogin: New command proposal

2 participants