Skip to content

Instantly share code, notes, and snippets.

@ccellar
Created September 27, 2013 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccellar/6724786 to your computer and use it in GitHub Desktop.
Save ccellar/6724786 to your computer and use it in GitHub Desktop.
Updates the MSCRM_Config with a new setup user to be able to start a redeployment
--Source: http://social.microsoft.com/Forums/en-US/crmdeployment/thread/020049fc-d4a3-4570-b70f-6e073f9afdfc
--Listing Users and with their roles
SELECT
SU.DomainName
,SU.SetupUser
,SU.IsDisabled
,SU.AccessMode
,SUR.SystemUserId
,RB.Name [Role]
,SUR.RoleId
FROM
RoleBase RB
INNER JOIN SystemUserRoles SUR on RB.RoleId = SUR.RoleId
INNER JOIN SystemUser SU ON SU.SystemUserId = SUR.SystemUserId
--WHERE RB.Name = 'System Administrator'
ORDER BY
-- Sort SysAdmins to the top
CASE RB.Name WHEN 'Systemadministrator' THEN '!' ELSE RB.Name END,
DomainName
DECLARE @NewSetupUserDomainName NVARCHAR(255),
@NewSetupUserId UniqueIdentifier,
@SystemAdminRoleId UniqueIdentifier
SET @NewSetupUserDomainName = 'CRM\Administrator'
SELECT @NewSetupUserId=SystemUserId
FROM SystemUser
WHERE DomainName=@NewSetupUserDomainName
IF (@NewSetupUserId IS NULL)
RAISERROR (N'Unable to find user %s.',
18, -- Severity,
1, -- State,
@NewSetupUserDomainName);
ELSE
BEGIN
SELECT @SystemAdminRoleId=RoleID
FROM SystemUser U
INNER JOIN [Role] R ON R.BusinessUnitId=U.BusinessUnitId
WHERE R.Name = 'Systemadministrator'
--remove all setup user(s)
UPDATE SystemUser
SET SetupUser = 0
--Mark our user as the setup user
UPDATE SystemUser
SET SetupUser =1
,IsDisabled =0
,AccessMode =0
WHERE
SystemUserID = @NewSetupUserId
--Make sure that user has the system admin role
IF (NOT EXISTS(SELECT * FROM SystemUserRoles WHERE SystemUserId=@NewSetupUserId AND RoleId=@SystemAdminRoleId))
BEGIN
INSERT SystemUserRoles (SystemUserRoleId,SystemUserId,RoleId)
SELECT NEWID()
,@NewSetupUserId
,@SystemAdminRoleId
PRINT 'Added CRM Sys Admin role to user'
END
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment