Add Windows User to local SQL Server with PowerShell

后端 未结 4 1946
清歌不尽
清歌不尽 2021-01-24 18:08

I would like to add an existing local user to the SQL Server as a sysadmin, with PowerShell. fter some research I have the following script so far:

$Username = \         


        
4条回答
  •  野性不改
    2021-01-24 18:25

    I did not try your code. But, the following one worked for me on my SQL Express instance.

        $conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList $env:ComputerName
    $conn.applicationName = "PowerShell SMO"
    $conn.ServerInstance = ".\SQLEXPRESS"
    $conn.StatementTimeout = 0
    $conn.Connect()
    $smo = New-Object Microsoft.SqlServer.Management.Smo.Server -ArgumentList $conn
    $SqlUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $smo,"${env:ComputerName}\JohnDoe"
    $SqlUser.LoginType = 'WindowsUser'
    $sqlUser.PasswordPolicyEnforced = $false
    $SqlUser.Create()
    

提交回复
热议问题