# This script will resolve the NetBios Name of a Domain then add a user with the netbios name. # Don't forget to execute: set-executionpolicy RemoteSigned # in powershell prior to the use of this script # Step 1: Import The Active Directory Module import-module activedirectory # Step 2: Save the Current Directory Identity into a variable (Replace Domain.root with your domain) $ident = get-addomain -identity domain.root -ErrorVariable Err -ErrorAction SilentlyContinue # If there is an Error Stop and Report it > Else Continue if ($err) { Write-Host "ERROR! The following error occurred while obtaining NetBIOS Name: $err" } # Select the netbios name and put it into a string [string[$netbiosname = $ident.netbiosname # Setup User Variable for the add. Change Geeks to the user or Group you'd desire. $user = $netbiosname + "\Geeks" # Add the user into SQL. Be sure to change the SQL User of sa and password of my password. [string]$err = sqlcmd -Usa -Pmypassword -d 'master' -Q "CREATE LOGIN `[$user`] FROM WINDOWS WITH DEFAULT_DATABASE=`[MASTER`], DEFAULT_LANGUAGE=`[us_english`]" # This variable will become populated if an error occurred; else it will remain blank. if ($err) { write-host "ERROR! The following error occurred while creating SQL User: $err " } # The permission addition will execute if the above command was successful. Else { # Add the credential of Sysadmin to the users. This can be changed to any role. [string]$err = sqlcmd -Usa -Pmypassword -d 'master' -Q "EXEC sys.sp_addsrvrolemember @loginame = N'$user', @rolename = N'sysadmin'" if ($err) { write-host "Error Assigning Permisssions to $user: $err" } }