Add User to Administrators Group using SQL Lookup Table Summary This utility is intended to aid customers in the ability to make the user of a desktop a local administrator on the desktop. In order to script this there must be a way for the script to know which user should be defined as the administrator of the desktop. This can be done in many ways but for this process a SQL server lookup table is used to match the user to the desktop. Then a PowerShell script is added to the startup files of the desktop that will look up the desktop name in the lookup table and add the appropriate user to the local administrators group. Included in this package are a utility to manage the database table and the PowerShell script to add to the desktops. The script can be added to the OS layer for Unidesk desktops but it could also be run from a startup script on any desktop for those that do not use Unidesk. Assumption/Requirements 1. PowerShell 2.0 or later on desktop 2. Microsoft SQL Server Installing and Setting up the Utility Create a folder on your desktop and unzip the files. Create a sql database using the script found later in this document or to similar specification. Then run the management utility to define the database settinsg and create the configuration fiel used by the script. Check Script for Blocking Before unpacking check the properties of the zip file to see if they may be blocked as shown on the right. If they are click unblock. This will unblock all of the files as long as you unblock the zip before you extract the files. Version 1.0 April 15, 2014 Page 1
Create the SQL Database The requirements for the database are very minimal. There need to be a database with one table and three fields. The names are configurable. In our example we called the Database AssignDesktop and the table DesktopAssignments. We then created three columns User, Desktop and Domain. If desired this SQL script can be used to create the table. First create the Database in SQL Manager. Then run this query. USE [AssignDesktop] /****** Object: Table [dbo].[desktopassignment] Script Date: 04/15/2014 12:44:09 ******/ SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON SET ANSI_PADDING ON CREATE TABLE [dbo].[desktopassignment]( [User] [varchar](100) NULL, [Desktop] [varchar](15) NULL, [Domain] [varchar](100) NULL ) ON [PRIMARY] SET ANSI_PADDING OFF Database Permission In your database you will need two accounts. One that has only db_datareader permissions and one that has read and write permissions. I used db_owner for the later. The first is used by the script to perform lookups in the database to match the user of a desktop to the desktop name. The second is used by the management utility to view, add and edit entries in the database. Version 2.0 April 30, 2014 Page 2
Setup Utility Once unpacked there is an application called AddToAdminGroupSQL.exe. Double click this application and click on Settings. The following screen will open. Enter the information that matches the database you created. Version 1.0 April 30, 2014 Page 3
Listing and Searching The utility provides the ability to list all the assignment entries and to search for a particular entry or a wildcard entry. The wildcard character for SQL server is the % character. Therefore to find all desktops starting with mrk enter mrk% in the desktop field and click the search button. You can also add single entries form this interface. To add a single desktop fill in the Desktop Name, User Name, Domain Name and click add. If you have set a default domain it will be used. If you have not set a default then you can support multiple domains by adding the appropriate domain here. Version 2.0 April 30, 2014 Page 4
Use the Utility to Bulk Load Data After creating the database and configuring the utility you can add user/desktop entries to the table. This can be performed in bulk from a csv file or the Unidesk MA Database from the UI. To Add entries in bulk create a csv file using excel. The file should have three columns. The order of the columns must be Domain, User, Desktop and the file must be named bulkadd.csv and it must be stored in the directory with the utility. Click on the Get CSV button to import the desktop assignments into the list. Then use the Upload to SQL to add the desired records to the assignment table. You can select and deselect records to include before uploading. Version 1.0 April 30, 2014 Page 5
In order to load records to the UI from the Unidesk MA Database click Get Unidesk after entering the Management Appliance FQDN or IP address and password. If the appliance root password has not been changed leave the password field blank and the default Password will be used. Again after retrieving the list, records can be selected or deselected. When ready use the Upload To SQL function to load the selected records into the SQL table. Configuring the PowerShell Script on the Desktop After the database is configured and data entered you can deploy the script to the desktops. For a Unidesk desktop the best place to add the script is in the OS layer by modifying the kmssetup.cmd script. This script rusn during every boot. Add a version to the OS layer then open c:\windows\setup\scripts\kmsdir and copy the PowerShell script and the configuration file into the kmsdir folder. Then at the bottom of the kmssetup.cmd where it runs commands during every boot add the following. The script first checks to see if the user is in the group and if not adds them in. The script will create a log file in the kmsdir folder for troubleshooting issues. Version 2.0 April 30, 2014 Page 6