<commit_before>﻿Function Repair-SqlOrphanUser
{
<#
.SYNOPSIS
Find orphan users with existing login and remap.

.DESCRIPTION
An orphan user is defined by a user that does not have their matching login. (Login property = "")
	
If the matching login exists it must be:
    .Enabled
    .Not a system object
    .Not locked
    .Have the same name that user

You can drop users that does not have their matching login by especifing the parameter -RemoveNotExisting This will be made by calling Remove-SqlOrphanUser function.
	
.PARAMETER SqlServer
The SQL Server instance.

.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:

$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. 

Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.

.PARAMETER Users
List of users to repair

.PARAMETER RemoveNotExisting
If passed, all users that not have their matching login will be dropped from database

.EXAMPLE
Repair-SqlOrphanUser -SqlServer sql2005 

Will find and repair all orphan users of all databases present on server 'sql2005'

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -SqlCredential $cred
	
Will find and repair all orphan users of all databases present on server 'sqlserver2014a'. Will be verified using SQL credentials. 
	
.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Databases db1, db2

Will find and repair all orphan users on both db1 and db2 databases

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Databases db1 -Users OrphanUser

Will find and repair user 'OrphanUser' on 'db1' database

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Users OrphanUser

Will find and repair user 'OrphanUser' on all databases

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -RemoveNotExisting

Will find all orphan users of all databases present on server 'sqlserver2014a'
Will also remove all users that does not have their matching login by calling Remove-SqlOrphanUser function
	
.NOTES 
Original Author: <NAME> (@ClaudioESSilva)
dbatools PowerShell module (https://dbatools.io, <EMAIL>)
Copyright (C) 2016 Ch<NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

.LINK
https://dbatools.io/Repair-SqlOrphanUser

#>
	[CmdletBinding(SupportsShouldProcess = $true)]
	Param (
		[parameter(Mandatory = $true, ValueFromPipeline = $true)]
		[Alias("ServerInstance", "SqlInstance")]
		[object[]]$SqlServer,
        [object]$SqlCredential,
        [parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [object[]]$Users,
        [switch]$RemoveNotExisting
	)

    DynamicParam { if ($SqlServer) { return Get-ParamSqlDatabases -SqlServer $SqlServer -SqlCredential $SqlCredential } }
	
	BEGIN
	{
        Write-Output "Attempting to connect to Sql Server.."
		$server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
	}
	
	PROCESS
	{
        # Convert from RuntimeDefinedParameter object to regular array
		$databases = $psboundparameters.Databases
		
        if ($databases.Count -eq 0)
        {
            $databases = $server.Databases | Where-Object {$_.IsSystemObject -eq $false -and $_.IsAccessible -eq $true}
        }
        else
        {<commit_msg>Finally go thte help to pass the pester tests!!!<commit_after>﻿Function Repair-SqlOrphanUser
{
<#
.SYNOPSIS
Find orphan users with existing login and remap.

.DESCRIPTION
An orphan user is defined by a user that does not have their matching login. (Login property = "")
	
If the matching login exists it must be:
    Enabled
    Not a system object
    Not locked
    Have the same name that user

You can drop users that does not have their matching login by especifing the parameter -RemoveNotExisting This will be made by calling Remove-SqlOrphanUser function.

	
.PARAMETER SqlServer
The SQL Server instance.

.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:

$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. 

Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.

.PARAMETER Users
List of users to repair

.PARAMETER RemoveNotExisting
If passed, all users that not have their matching login will be dropped from database

.EXAMPLE
Repair-SqlOrphanUser -SqlServer sql2005 

Will find and repair all orphan users of all databases present on server 'sql2005'

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -SqlCredential $cred
	
Will find and repair all orphan users of all databases present on server 'sqlserver2014a'. Will be verified using SQL credentials. 
	
.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Databases db1, db2

Will find and repair all orphan users on both db1 and db2 databases

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Databases db1 -Users OrphanUser

Will find and repair user 'OrphanUser' on 'db1' database

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -Users OrphanUser

Will find and repair user 'OrphanUser' on all databases

.EXAMPLE   
Repair-SqlOrphanUser -SqlServer sqlserver2014a -RemoveNotExisting

Will find all orphan users of all databases present on server 'sqlserver2014a'
Will also remove all users that does not have their matching login by calling Remove-SqlOrphanUser function
	
.NOTES 
Original Author: <NAME> (@ClaudioESSilva)
dbatools PowerShell module (https://dbatools.io, <EMAIL>)
Copyright (C) 2016 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

.LINK
https://dbatools.io/Repair-SqlOrphanUser
#>
	[CmdletBinding(SupportsShouldProcess = $true)]
	Param (
		[parameter(Mandatory = $true, ValueFromPipeline = $true)]
		[Alias("ServerInstance", "SqlInstance")]
		[object[]]$SqlServer,
        [object]$SqlCredential,
        [parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [object[]]$Users,
        [switch]$RemoveNotExisting
	)

    DynamicParam { if ($SqlServer) { return Get-ParamSqlDatabases -SqlServer $SqlServer -SqlCredential $SqlCredential } }
	
	BEGIN
	{
        Write-Output "Attempting to connect to Sql Server.."
		$server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
	}
	
	PROCESS
	{
        # Convert from RuntimeDefinedParameter object to regular array
		$databases = $psboundparameters.Databases
		
        if ($databases.Count -eq 0)
        {
            $databases = $server.Databases | Where-Object {$_.IsSystemObject -eq $false -and $_.IsAccessible -eq $true}
        }
        else
        {
{'fraction_non_alphanumeric': 0.06241400875547217, 'fraction_numerical': 0.01200750469043152, 'mean_word_length': 4.019460138104206, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3498871', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2177, 'n_words': 956}