我有一个SQL脚本,该脚本进入ADP(人力资源系统)数据库并获取员工信息,然后创建一个表和命令结构来运行命令以创建活动目录帐户...问题是SQL创建了DS命令要运行,但是管理员仍然需要打开命令提示符来运行命令...我正在尝试使它成为一个自动化过程,该过程将完成数据库的插入和运行命令并创建帐户,而无需人工操作手动完成...可以完成吗?
USE [AccountManagement]
GO
/****** Object: StoredProcedure [dbo].[ProcessEmployees] Script Date: 4/17/2018 4:35:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ProcessEmployees]
AS
MERGE INTO EmployeeMaster DT
USING (
SELECT ADPID, EmployeeID, FirstName, LastName, CenterID, HireDate, Status, LastStatusChange, StatusEffectiveDate, CONVERT(BIT,0) AS WindowsLoginCreated, CONVERT(BIT,0) AS DialerLoginCreated, CONVERT(BIT,0) AS BadgeUserCreated, CONVERT(BIT,0) AS WindowsLoginDisabled, CONVERT(BIT,0) AS WindowsLoginRemoved, CONVERT(BIT,0) AS DialerLoginRemoved, CONVERT(BIT,0) AS BadgeAccessDisabled, CONVERT(BIT,0) AS BadgeUserRemoved, SupervisorID, Supervisor, OrigTrainerID, OrigTrainer, JobTitleCode, JobTitle, CONVERT(VARCHAR(200),NULL) AS ADsPath, CONVERT(DATETIME,NULL) AS LastADUpdate
FROM OPENQUERY([UIP1-Data],'SELECT ADPID, EmployeeID, FirstName, LastName, CenterID, HireDate, Status, LastStatusChange, StatusEffectiveDate, SupervisorID, Supervisor, OrigTrainerID, OrigTrainer, JobTitleCode, JobTitle FROM Staging.dbo.EmployeeMaster WITH(NOLOCK) WHERE LEFT(ADPID,3) = ''5M3''')
) ST ON DT.ADPID = ST.ADPID
WHEN MATCHED THEN
UPDATE SET
FirstName = ISNULL(ST.FirstName,DT.FirstName),
LastName = ISNULL(ST.LastName,DT.LastName),
CenterID = ISNULL(ST.CenterID,DT.CenterID),
Status = ISNULL(ST.Status,DT.Status),
SupervisorID = ISNULL(ST.SupervisorID,DT.SupervisorID),
Supervisor = ISNULL(ST.Supervisor,DT.Supervisor),
OrigTrainerID = ISNULL(ST.OrigTrainerID,DT.OrigTrainerID),
OrigTrainer = ISNULL(ST.OrigTrainer,DT.OrigTrainer),
HireDate = ISNULL(ST.HireDate,DT.HireDate),
JobTitleCode = ISNULL(ST.JobTitleCode,DT.JobTitleCode),
JobTitle = ISNULL(ST.JobTitle,DT.JobTitle),
StatusEffectiveDate = ISNULL(ST.StatusEffectiveDate,DT.StatusEffectiveDate),
LastStatusChange = CASE WHEN ISNULL(ST.Status,'') <> ISNULL(DT.Status,'') THEN GETDATE() ELSE DT.LastStatusChange END,
UpdateDt = GETDATE()
WHEN NOT MATCHED THEN
INSERT (ADPID, EmployeeID, FirstName, LastName, CenterID, Status, SupervisorID, Supervisor, OrigTrainerID, OrigTrainer, LastStatusChange, HireDate, JobTitleCode, JobTitle, StatusEffectiveDate, InsertDt, UpdateDt)
VALUES (ST.ADPID, ST.EmployeeID, ST.FirstName, ST.LastName, ST.CenterID, ST.Status, ST.SupervisorID, ST.Supervisor, ST.OrigTrainerID, ST.OrigTrainer, GETDATE(), ST.HireDate, ST.JobTitleCode, ST.JobTitle, ST.StatusEffectiveDate, GETDATE(), GETDATE());
DECLARE @BatchSize INT = 20000
DECLARE @CurrentRow INT = 1
DECLARE @SQL NVARCHAR(MAX)
DECLARE @LastADUpdate DATETIME = GETDATE()
IF (SELECT OBJECT_ID('tempdb..#ADSI')) IS NOT NULL
DROP TABLE #ADSI
CREATE TABLE #ADSI (sAMAccountname nvarchar(4000) NULL, ADsPath nvarchar(256) NULL)
DECLARE @EmployeeMaster TABLE (RowID INT IDENTITY(1,1), EmployeeID CHAR(6), ADsPath VARCHAR(200), LastADUpdate DATETIME, WindowsLoginCreated BIT DEFAULT(0), DialerLoginCreated bit DEFAULT(0))
SET ROWCOUNT @BatchSize
INSERT INTO @EmployeeMaster (EmployeeID, ADsPath, LastADUpdate)
SELECT EmployeeID, ADsPath, LastADUpdate
FROM dbo.EmployeeMaster
ORDER BY LastADUpdate ASC
INSERT INTO @EmployeeMaster (EmployeeID, ADsPath, LastADUpdate)
SELECT EmployeeID, ADsPath, LastADUpdate
FROM dbo.EmployeeMaster
ORDER BY LastStatusChange DESC
SET ROWCOUNT 0
WHILE @CurrentRow <= (SELECT MAX(RowID) FROM @EmployeeMaster)
BEGIN
UPDATE @EmployeeMaster SET LastADUpdate = @LastADUpdate WHERE RowID = @CurrentRow
SET @SQL = 'SELECT sAMAccountname, ADsPath FROM OPENQUERY(ADSI,''<LDAP://DC=domain,DC=com>;(&(objectCategory=person)(objectClass=user)(sAMAccountType=805306368)(sAMAccountName=' + (SELECT EmployeeID FROM @EmployeeMaster WHERE RowID = @CurrentRow) + '));sAMAccountname,ADsPath;subtree'')'
-- SELECT @SQL
INSERT INTO #ADSI (sAMAccountname, ADsPath)
EXEC (@SQL)
SET @CurrentRow = @CurrentRow + 1
END
UPDATE #ADSI SET ADsPath = REPLACE(ADsPath,'LDAP://','')
UPDATE EMT SET
WindowsLoginCreated = CASE WHEN ADSI.ADsPath IS NULL THEN 0 ELSE 1 END,
ADsPath = ADSI.ADsPath
FROM @EmployeeMaster EMT
JOIN #ADSI ADSI ON EMT.EmployeeID = ADSI.sAMAccountname
UPDATE EM SET
WindowsLoginCreated = EMT.WindowsLoginCreated,
DialerLoginCreated = EMT.DialerLoginCreated,
ADsPath = EMT.ADsPath,
LastADUpdate = EMT.LastADUpdate
FROM @EmployeeMaster EMT
JOIN dbo.EmployeeMaster EM ON EM.EmployeeID = EMT.EmployeeID
WHERE EMT.LastADUpdate = @LastADUpdate
IF (SELECT OBJECT_ID('tempdb..#ADSI')) IS NOT NULL
DROP TABLE #ADSI
--SELECT LastADUpdate, COUNT(*) AS Records FROM dbo.EmployeeMaster WHERE WindowsLoginCreated = 1 GROUP BY LastADUpdate ORDER BY LastADUpdate ASC
DECLARE @CmdLine varchar(200)
DECLARE @SQLString varchar(4000)
DECLARE @AgentID char(6)
DECLARE @FirstName varchar(60)
DECLARE @LastName varchar(60)
DECLARE @Status VARCHAR(20)
--SELECT * FROM EmployeeMaster WHERE DialerLoginCreated = 1 AND WindowsLoginCreated = 0
SELECT 'dsadd user "CN=' + EmployeeID + ' ' + REPLACE(FirstName,'"','') + ' ' + REPLACE(LastName,'"','') + ',OU=User Imports,DC=domain,DC=com" -upn "' + EmployeeID + '@domain.com" -samid "' + EmployeeID + '" -fn "' + REPLACE(FirstName,'"','') + '" -ln "' + REPLACE(LastName,'"','') + '" -display "' + REPLACE(FirstName,'"','') + ' ' + REPLACE(LastName,'"','') + '" -empid "' + EmployeeID + '" -email "' + EmployeeID + '@domain.com" -pwd "Pa$$w0rd" -mustchpwd yes'
FROM EmployeeMaster
WHERE Status = 'Active' AND WindowsLoginCreated = 0
UNION
SELECT 'for /f "Tokens=*" %a in (''dsquery user domainroot -samid "' + EmployeeID + '"'') do dsmod user %a -disabled yes' AS Command
FROM dbo.EmployeeMaster WHERE Status = 'Terminated' AND WindowsLoginCreated = 1 AND StatusEffectiveDate >= DATEADD(DAY,-20,GETDATE())
UNION
SELECT 'for /f "Tokens=*" %a in (''dsquery user domainroot -samid "' + EmployeeID + '"'') do dsrm -noprompt %a' AS Command
FROM dbo.EmployeeMaster WHERE Status = 'Terminated' AND WindowsLoginCreated = 1 AND ISNULL(StatusEffectiveDate,'2000-01-01') < DATEADD(DAY,-20,GETDATE())
UPDATE EmployeeMaster SET WindowsLoginCreated = 1 WHERE Status = 'Active' AND WindowsLoginCreated = 0
GO