Login Creation, Adding Login to Server Role and extracting info, dbatools

The below powershell query will create a login it could be a windows login or SQL Server Authentication login we can create those login by using below query and add that particular login to particular role and can extract the info. If that particular login alredy exists in the environment the query will ignore or if you mention -FORCE parameter it will overwrite.
cls
$ServerList= @("Instance1","Instance2","Instance3")
$ServerList|ForEach-Object{
$indserver=$_
New-DbaLogin -SqlInstance  $indserver -Login "DomainName\WindowsLoginGroup"|
Select-Object ComputerName,InstanceName,SqlInstance,Name,LoginType,CreateDate,LastLogin,HasAccess,IsLocked,IsDisabled
Set-DbaLogin -SqlInstance $indserver -Login "DomainName\WindowsLoginGroup" -AddRole sysadmin|
Select-Object ComputerName,InstanceName,SqlInstance,LoginName,ServerRole
Get-DbaLogin -SqlInstance $indserver -Login "DomainName\WindowsLoginGroup"|
Select-Object ComputerName,InstanceName,SqlInstance,Name,LoginType,CreateDate,LastLogin,HasAccess,IsLocked,IsDisabled
}|Export-Csv -Path "C:\temp\LoginInfo.csv" -NoTypeInformation

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server