Skip to content

Commit

Permalink
Changes to SqlServerLogin
Browse files Browse the repository at this point in the history
- BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed
    to ServerName and InstanceName respectivly (issue dsccommunity#308).
  • Loading branch information
johlju committed Dec 1, 2017
1 parent 4f3b010 commit 5efdcc3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 64 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
- Made the resource cluster aware. When ProcessOnlyOnActiveNode is specified,
the resource will only determine if a change is needed if the target node
is the active host of the SQL Server instance ([issue #882](https://github.com/PowerShell/xSQLServer/issues/882)).
- Changes to SqlServerLogin
- BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed
to ServerName and InstanceName respectivly ([issue #308](https://github.com/PowerShell/xSQLServer/issues/)).

## 8.2.0.0

Expand Down
74 changes: 37 additions & 37 deletions DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Pare
.PARAMETER Name
The name of the login to retrieve.
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server to retrieve the login from.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance to retrieve the login from.
#>
function Get-TargetResource
Expand All @@ -27,17 +27,17 @@ function Get-TargetResource

[Parameter(Mandatory = $true)]
[System.String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[System.String]
$SQLInstanceName
$InstanceName
)

$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
$serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

Write-Verbose 'Getting SQL logins'
New-VerboseMessage -Message "Getting the login '$Name' from '$SQLServer\$SQLInstanceName'"
New-VerboseMessage -Message "Getting the login '$Name' from '$ServerName\$InstanceName'"

$login = $serverObject.Logins[$Name]

Expand All @@ -50,15 +50,15 @@ function Get-TargetResource
$Ensure = 'Absent'
}

New-VerboseMessage -Message "The login '$Name' is $ensure from the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "The login '$Name' is $ensure from the '$ServerName\$InstanceName' instance."

$returnValue = @{
Ensure = $Ensure
Name = $Name
LoginType = $login.LoginType
SQLServer = $SQLServer
SQLInstanceName = $SQLInstanceName
Disabled = $login.IsDisabled
Ensure = $Ensure
Name = $Name
LoginType = $login.LoginType
ServerName = $ServerName
InstanceName = $InstanceName
Disabled = $login.IsDisabled
}

if ( $login.LoginType -eq 'SqlLogin' )
Expand All @@ -84,10 +84,10 @@ function Get-TargetResource
.PARAMETER LoginType
The type of login to create. Default is 'WindowsUser'
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server to create the login on.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance to create the login on.
.PARAMETER LoginCredential
Expand Down Expand Up @@ -134,11 +134,11 @@ function Set-TargetResource

[Parameter(Mandatory = $true)]
[System.String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[System.String]
$SQLInstanceName,
$InstanceName,

[Parameter()]
[System.Management.Automation.PSCredential]
Expand All @@ -161,7 +161,7 @@ function Set-TargetResource
$Disabled
)

$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
$serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

switch ( $Ensure )
{
Expand All @@ -175,14 +175,14 @@ function Set-TargetResource
{
if ( $login.PasswordExpirationEnabled -ne $LoginPasswordExpirationEnabled )
{
New-VerboseMessage -Message "Setting PasswordExpirationEnabled to '$LoginPasswordExpirationEnabled' for the login '$Name' on the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "Setting PasswordExpirationEnabled to '$LoginPasswordExpirationEnabled' for the login '$Name' on the '$ServerName\$InstanceName' instance."
$login.PasswordExpirationEnabled = $LoginPasswordExpirationEnabled
Update-SQLServerLogin -Login $login
}

if ( $login.PasswordPolicyEnforced -ne $LoginPasswordPolicyEnforced )
{
New-VerboseMessage -Message "Setting PasswordPolicyEnforced to '$LoginPasswordPolicyEnforced' for the login '$Name' on the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "Setting PasswordPolicyEnforced to '$LoginPasswordPolicyEnforced' for the login '$Name' on the '$ServerName\$InstanceName' instance."
$login.PasswordPolicyEnforced = $LoginPasswordPolicyEnforced
Update-SQLServerLogin -Login $login
}
Expand All @@ -196,7 +196,7 @@ function Set-TargetResource

if ( $PSBoundParameters.ContainsKey('Disabled') -and ($login.IsDisabled -ne $Disabled) )
{
New-VerboseMessage -Message "Setting IsDisabled to '$Disabled' for the login '$Name' on the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "Setting IsDisabled to '$Disabled' for the login '$Name' on the '$ServerName\$InstanceName' instance."
if ( $Disabled )
{
$login.Disable()
Expand All @@ -220,7 +220,7 @@ function Set-TargetResource
throw New-TerminatingError -ErrorType LoginCredentialNotFound -FormatArgs $Name -ErrorCategory ObjectNotFound
}

New-VerboseMessage -Message "Adding the login '$Name' to the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "Adding the login '$Name' to the '$ServerName\$InstanceName' instance."

$login = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $serverObject, $Name
$login.LoginType = $LoginType
Expand All @@ -232,7 +232,7 @@ function Set-TargetResource
# Verify the instance is in Mixed authentication mode
if ( $serverObject.LoginMode -notmatch 'Mixed|Integrated' )
{
throw New-TerminatingError -ErrorType IncorrectLoginMode -FormatArgs $SQLServer, $SQLInstanceName, $serverObject.LoginMode -ErrorCategory NotImplemented
throw New-TerminatingError -ErrorType IncorrectLoginMode -FormatArgs $ServerName, $InstanceName, $serverObject.LoginMode -ErrorCategory NotImplemented
}

$login.PasswordPolicyEnforced = $LoginPasswordPolicyEnforced
Expand Down Expand Up @@ -267,7 +267,7 @@ function Set-TargetResource
{
if ( $serverObject.Logins[$Name] )
{
New-VerboseMessage -Message "Dropping the login '$Name' from the '$SQLServer\$SQLInstanceName' instance."
New-VerboseMessage -Message "Dropping the login '$Name' from the '$ServerName\$InstanceName' instance."
Remove-SQLServerLogin -Login $serverObject.Logins[$Name]
}
}
Expand All @@ -287,10 +287,10 @@ function Set-TargetResource
.PARAMETER LoginType
The type of login. Default is 'WindowsUser'
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance.
.PARAMETER LoginCredential
Expand Down Expand Up @@ -337,11 +337,11 @@ function Test-TargetResource

[Parameter(Mandatory = $true)]
[System.String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[System.String]
$SQLInstanceName,
$InstanceName,

[Parameter()]
[System.Management.Automation.PSCredential]
Expand All @@ -368,44 +368,44 @@ function Test-TargetResource
$testPassed = $true

$getParams = @{
Name = $Name
SQLServer = $SQLServer
SQLInstanceName = $SQLInstanceName
Name = $Name
ServerName = $ServerName
InstanceName = $InstanceName
}

$loginInfo = Get-TargetResource @getParams

if ( $Ensure -ne $loginInfo.Ensure )
{
New-VerboseMessage -Message "The login '$Name' on the instance '$SQLServer\$SQLInstanceName' is $($loginInfo.Ensure) rather than $Ensure"
New-VerboseMessage -Message "The login '$Name' on the instance '$ServerName\$InstanceName' is $($loginInfo.Ensure) rather than $Ensure"
$testPassed = $false
}

if ( $Ensure -eq 'Present' )
{
if ( $LoginType -ne $loginInfo.LoginType )
{
New-VerboseMessage -Message "The login '$Name' on the instance '$SQLServer\$SQLInstanceName' is a $($loginInfo.LoginType) rather than $LoginType"
New-VerboseMessage -Message "The login '$Name' on the instance '$ServerName\$InstanceName' is a $($loginInfo.LoginType) rather than $LoginType"
$testPassed = $false
}

if ( $PSBoundParameters.ContainsKey('Disabled') -and ($loginInfo.Disabled -ne $Disabled) )
{
New-VerboseMessage -Message "The login '$Name' on the instance '$SQLServer\$SQLInstanceName' has IsDisabled set to $($loginInfo.Disabled) rather than $Disabled"
New-VerboseMessage -Message "The login '$Name' on the instance '$ServerName\$InstanceName' has IsDisabled set to $($loginInfo.Disabled) rather than $Disabled"
$testPassed = $false
}

if ( $LoginType -eq 'SqlLogin' )
{
if ( $LoginPasswordExpirationEnabled -ne $loginInfo.LoginPasswordExpirationEnabled )
{
New-VerboseMessage -Message "The login '$Name' on the instance '$SQLServer\$SQLInstanceName' has PasswordExpirationEnabled set to $($loginInfo.LoginPasswordExpirationEnabled) rather than $LoginPasswordExpirationEnabled"
New-VerboseMessage -Message "The login '$Name' on the instance '$ServerName\$InstanceName' has PasswordExpirationEnabled set to $($loginInfo.LoginPasswordExpirationEnabled) rather than $LoginPasswordExpirationEnabled"
$testPassed = $false
}

if ( $LoginPasswordPolicyEnforced -ne $loginInfo.LoginPasswordPolicyEnforced )
{
New-VerboseMessage -Message "The login '$Name' on the instance '$SQLServer\$SQLInstanceName' has PasswordPolicyEnforced set to $($loginInfo.LoginPasswordPolicyEnforced) rather than $LoginPasswordPolicyEnforced"
New-VerboseMessage -Message "The login '$Name' on the instance '$ServerName\$InstanceName' has PasswordPolicyEnforced set to $($loginInfo.LoginPasswordPolicyEnforced) rather than $LoginPasswordPolicyEnforced"
$testPassed = $false
}

Expand All @@ -416,7 +416,7 @@ function Test-TargetResource

try
{
$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -SetupCredential $userCred
$serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName -SetupCredential $userCred
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class MSFT_SqlServerLogin : OMI_BaseResource
[Write, Description("The type of login to be created. If LoginType is 'WindowsUser' or 'WindowsGroup' then provide the name in the format DOMAIN\name. Default is WindowsUser. Unsupported login types are Certificate, AsymmetricKey, ExternalUser, and ExternalGroup."),
ValueMap{"WindowsUser","WindowsGroup","SqlLogin","Certificate","AsymmetricKey","ExternalUser","ExternalGroup"},
Values{"WindowsUser","WindowsGroup","SqlLogin","Certificate","AsymmetricKey","ExternalUser","ExternalGroup"}] String LoginType;
[Key, Description("The hostname of the SQL Server to be configured.")] String SQLServer;
[Key, Description("Name of the SQL instance to be configured.")] String SQLInstanceName;
[Key, Description("The hostname of the SQL Server to be configured.")] String ServerName;
[Key, Description("Name of the SQL instance to be configured.")] String InstanceName;
[Write, EmbeddedInstance("MSFT_Credential"), Description("If LoginType is 'SqlLogin' then a PSCredential is needed for the password to the login.")] String LoginCredential;
[Write, Description("Specifies if the login is required to have its password change on the next login. Only applies to SQL Logins. Default is $true.")] Boolean LoginMustChangePassword;
[Write, Description("Specifies if the login password is required to expire in accordance to the operating system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordExpirationEnabled;
Expand Down
16 changes: 8 additions & 8 deletions Examples/Resources/SqlServerLogin/1-AddLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Configuration Example
Ensure = 'Present'
Name = 'CONTOSO\WindowsUser'
LoginType = 'WindowsUser'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SysAdminAccount
}

Expand All @@ -39,8 +39,8 @@ Configuration Example
Ensure = 'Present'
Name = 'CONTOSO\WindowsUser2'
LoginType = 'WindowsUser'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SysAdminAccount
Disabled = $true
}
Expand All @@ -50,8 +50,8 @@ Configuration Example
Ensure = 'Present'
Name = 'CONTOSO\WindowsGroup'
LoginType = 'WindowsGroup'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SysAdminAccount
}

Expand All @@ -60,8 +60,8 @@ Configuration Example
Ensure = 'Present'
Name = 'SqlLogin'
LoginType = 'SqlLogin'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
LoginCredential = $LoginCredential
LoginMustChangePassword = $false
LoginPasswordExpirationEnabled = $true
Expand Down
30 changes: 15 additions & 15 deletions Examples/Resources/SqlServerLogin/2-RemoveLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ Configuration Example
node localhost {
SqlServerLogin Remove_WindowsUser
{
Ensure = 'Absent'
Name = 'CONTOSO\WindowsUser'
LoginType = 'WindowsUser'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
Ensure = 'Absent'
Name = 'CONTOSO\WindowsUser'
LoginType = 'WindowsUser'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
}

SqlServerLogin Remove_WindowsGroup
{
Ensure = 'Absent'
Name = 'CONTOSO\WindowsGroup'
LoginType = 'WindowsGroup'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
Ensure = 'Absent'
Name = 'CONTOSO\WindowsGroup'
LoginType = 'WindowsGroup'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
}

SqlServerLogin Remove_SqlLogin
{
Ensure = 'Absent'
Name = 'SqlLogin'
LoginType = 'SqlLogin'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
Ensure = 'Absent'
Name = 'SqlLogin'
LoginType = 'SqlLogin'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
}
}
}
4 changes: 2 additions & 2 deletions Tests/Unit/MSFT_SqlServerLogin.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ try
$mockSqlLoginCredentialOtherPassword = New-Object System.Management.Automation.PSCredential( $mockSqlLoginUser, $mockSqlLoginOtherPassword )

$instanceParameters = @{
SQLInstanceName = 'MSSQLSERVER'
SQLServer = 'Server1'
InstanceName = 'MSSQLSERVER'
ServerName = 'Server1'
}

$getTargetResource_UnknownSqlLogin = $instanceParameters.Clone()
Expand Down

0 comments on commit 5efdcc3

Please sign in to comment.