1. 程式人生 > >托管服務賬號配置計劃任務

托管服務賬號配置計劃任務

gMSA Powershell windows 2012 托管服務

背景

豆子需要設置一個計劃任務然後用某個服務賬號運行。傳統的做法一般是創建一個服務賬號,配置對應的權限,然後設定密碼永不過期。這樣的安全隱患是密碼一直沒改變。Windows 2008的時候微軟推出了MSA(托管服務賬號),本質是繼承了計算機賬戶的特性,每次由計算機自動更換密碼,這樣完全不用管理人員的介入。但是當時MSA既不支持多臺主機,也不支持計劃任務,更別說第三方軟件,所以實用性不大。Windows 2012以後,微軟推出了新版本的gMSA,終於可以支持多臺主機和計劃任務了。gMSA每次改密碼是由DC上面的KDS服務管理的,每次通過一個root key id,時間戳以及gMSA的SID通過某個復雜的算法生成一個隨機的密碼。註意這裏的gMSA中的g代表的是group,也是說我們需要分配一個安全組給這個托管賬戶,安全組裏面的所有計算機賬戶都可以去使用這個托管賬戶

具體操作

1. 創建一個KDS Root Key

技術分享圖片

註意!創建以後,需要等待10個小時讓所有的DC同步

2.創建一個安全組,然後把需要管理的計算機加進去,重啟對應的計算機

技術分享圖片

3. 創建gMSA賬號

技術分享圖片

創建成功以後可以在Management Service Accounts這個容器下看見對應的賬號
技術分享圖片

4. 安裝gMSA到主機上

登錄到對應的主機上面,執行下面的命令

技術分享圖片

基本的安裝就完成了。下面就可以用這個gMSA賬號來配置服務或者計劃任務了

5. 配置計劃任務

一個很惡心的地方是,圖形界面不支持gMSA的配置。如果直接查找對應的賬號,他會報錯 說不存在
技術分享圖片

正確的方式是全程通過PowerShell實現

$F = "c:\scripts\syncErrornotification.ps1"

#The first command uses the New-ScheduledTaskAction cmdlet to assign the action variable $A to the executable file tskmgr.exe
$A = New-ScheduledTaskAction -Execute "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -Argument "-noexit -ExecutionPolicy Bypass -File $F"

#The second command uses the New-ScheduledTaskTrigger cmdlet to assign the trigger variable $T to the value AtLogon
$T = New-ScheduledTaskTrigger -daily -At 7am

#The third command uses the New-ScheduledTaskSettingsSet cmdlet to assign the settings variable $S to a task settings object
$S = New-ScheduledTaskSettingsSet -Compatibility Win8 -ExecutionTimeLimit 3600 -ThrottleLimit 2
#The fourth command assigns the principal variable to the New-ScheduledTaskPrincipal of the scheduled task, domainname\gMSA_account$
$P = New-ScheduledTaskPrincipal -UserId omnicom\svc-it$ -LogonType Password -RunLevel Highest

#The fifth command sets the description varible to $D for the task definition
$D = "Office365 Sync Errors"

#Register the scheduled task
Register-ScheduledTask test -Action $A -Trigger $T -Principal $P -Description $D

該主機上還需要授予執行腳本的權利,點開gpedit.msc, 添加對應logon as batch job的權限

技術分享圖片

如果需要其他權限,例如本地管理員或者域管理員等權限,請自行添加。

6. 測試

第5步腳本會創建下面的計劃任務,但是這個界面你如果用UI是無法實現的
技術分享圖片

實際的跑一下,1分鐘之後我就收到郵件了, 成功
技術分享圖片

托管服務賬號配置計劃任務