2017-04-07 44 views
0

問題:我想使用Microsoft PowerShell腳本在sitecore中創建用戶。我不想使用sitecore提供的powershell。使用powershell在sitecore中創建用戶

我能夠在sitecore內使用PowerShell腳本創建用戶。但要求是在安裝時使用PowerShell腳本在每個環境中創建新用戶。

回答

0

Sitecore用戶是Sitecore核心SQL數據庫中的普通ASP.NET成員資格用戶。 (默認取決於你的配置)。在Microsoft PowerShell中,您可以執行SQL查詢。

首先使用存儲過程aspnet_Membership_CreateUser創建用戶,並將其用作ApplicationName'sitecore'並使用sitecore域。

該SQL創建了一個有效的用戶Sitecore的:

declare @salt nvarchar(128) 
declare @password varbinary(256) 
declare @input varbinary(512) 
declare @hash varchar(64) 

-- Change these values (@salt should be Base64 encoded) 
set @salt = N'eyhKDP858wdrYHbBmFoQ6DXzFE1FB+RDP4ULrpoZXt6f' 
set @password = convert(varbinary(256),N'mypassword') 

set @input = hashbytes('sha1',cast('' as xml).value('xs:base64Binary(sql:variable(''@salt''))','varbinary(256)') + @password) 
set @hash = cast('' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable(''@input'')))','varchar(64)') 

-- @hash now contains a suitable password hash 
-- Now create the user using the value of @salt as the salt, and the value of @hash as the password (with the @PasswordFormat set to 1) 

DECLARE @return_value int, 
    @UserId uniqueidentifier 

EXEC @return_value = [dbo].[aspnet_Membership_CreateUser] 
    @ApplicationName = N'sitecore', 
    @UserName = N'sitecore\startup', 
    @Password = @hash, 
    @PasswordSalt = @salt, 
    @Email = N'[email protected]', 
    @PasswordQuestion = N'nope', 
    @PasswordAnswer = N'nope', 
    @IsApproved = 1, 
    @CurrentTimeUtc = '2017-03-03', 
    @CreateDate = '2017-03-03', 
    @UniqueEmail = 1, 
    @PasswordFormat = 1, 
    @UserId = @UserId OUTPUT 

SELECT @UserId as N'@UserId' 

SELECT 'Return Value' = @return_value 

並添加您需要的ROLS。 'sitecore \ Sitecore Client Users'是您需要登錄到CMS的文件。

EXECUTE [dbo].[aspnet_UsersInRoles_AddUsersToRoles] 
    @ApplicationName = N'sitecore' 
    ,@UserNames = N'sitecore\startup' 
    ,@RoleNames = N'sitecore\Sitecore Client Users' 
    ,@CurrentTimeUtc = N'2017-03-03' 
GO 

本示例使用密碼「mypassword」以最小權限創建用戶「啓動」。

+0

謝謝你的幫助。 其實我們正在尋找在Windows PowerShell中加載sitecore上下文。 \t 此外,我們不允許直接與sitecore數據庫交互 –