我编写了一个脚本,该脚本在本地工作站上作为具有管理权限的计算机策略运行。它会检查AD中最后一个登录用户的描述,该域管理员可以从“ Active Directory用户和计算机”中设置域管理员,如果其中包含工作站名称,则脚本会将用户添加到本地管理组中(如果工作站名称不在本地管理员组中)。用户的描述,它将用户从本地管理员组中删除。描述可以包含多个计算机名称,如下所示:
用户的描述:“ WKST-E445R和WKST-VM398上的本地管理员”
因此,要使某人仅在一台计算机上成为本地管理员,我只需要将此计算机的名称添加到AD中的用户说明中,并要求用户重新启动,删除计算机名称将删除本地管理员权限。
那不是最整洁的解决方案吗?:-)
这是脚本:
@if "%debug%" neq "%username%" echo off
set ver=MakeLocalAdmin.cmd henrik@c o m m o r e.se 20150423
:: Adds last logged on domain user to local Administrators group if run by computer GPO with Administrative rights and the user's Comment contains Computername
set log=nul
:: or set log=c:\logs\MakeLocalAdmins.txt
:: Check who was last logged on user
FOR /F "tokens=3 delims= " %%G in ('reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" /v LastLoggedOnUser') DO (
set DomainAndUserName=%%G)
:: Remove the spaces
set DomainAndUserName=%DomainAndUserName: =%
:: Get only username part
set LastLoggedOnUserName=%DomainAndUserName:*\=%
:: Check OS language, so we can adapt to localized name of Admin group and Comment
FOR /F "tokens=3 delims= " %%G in ('reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage') DO (
set Language=%%G)
echo %date% %Time% ; %0 ; %computername%; %LastLoggedOnUserName%; %DomainAndUserName%, %Language% >> %log%
goto %Language%
:: Add any langauage specific part below, but if an unknown install language is found,
:: an error 'label not found' should mean script terminates, but anyway make sure it terminates.
goto end
:0409
:: English
net user /domain %LastLoggedOnUserName% | find "Comment " | find "%computername%" >> %log%
set NoLocalAdmin=%errorlevel%
if %NoLocalAdmin% equ 0 net localgroup Administrators /add "%DomainAndUserName%" >> %log%
if %NoLocalAdmin% equ 1 net localgroup Administrators /del "%DomainAndUserName%" >> %log%
goto end
:041D
:: Swedish
:: †„” åäö (Swedish char's)
net user /domain %LastLoggedOnUserName% | find "Kommentar " | find "%computername%" >> %log%
set NoLocalAdmin=%errorlevel%
if %NoLocalAdmin% equ 0 net localgroup Administrat”rer /add "%DomainAndUserName%" >> %log%
if %NoLocalAdmin% equ 1 net localgroup Administrat”rer /del "%DomainAndUserName%" >> %log%
goto end
:end