Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd Windows User to local SQL Server with PowerShell
    primarykey
    data
    text
    <p>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:</p> <pre><code>$Username = "JohnDoe" [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null $SqlServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "localhost" $SqlUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $SqlServer, "$Username" $SqlUser.LoginType = 'WindowsUser' $SqlUser.Create() $SqlUser.AddToRole('sysadmin') </code></pre> <p>The user exists on localhost, it is member of the Users and the Administrators group. I got the following error message:</p> <blockquote> <p>Exception calling "Create" with "0" argument(s): "Create failed for Login 'JohnDoe'. " At C:\Users\LocalAdmin\Desktop\try.ps1:7 char:16 + $SqlUser.Create &lt;&lt;&lt;&lt; () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException</p> <p>Exception calling "AddToRole" with "1" argument(s): "Add to role failed for Login 'JohnDoe'. " At C:\Users\LocalAdmin\Desktop\try.ps1:8 char:23 + $SqlUser.AddToRole &lt;&lt;&lt;&lt; ('sysadmin') + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException</p> </blockquote> <p>Windows Server 2008 R2 with SQL Server 2008 R2 What am I doing wrong or what am I missing?</p> <p><strong>EDIT:</strong> Updated the script based on the suggessions from C.B. and mortb, but still not working. I have updated the script above to the current state, and the error message with that one what I am getting now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload