每次我们重新安装Windows时,即使用户名与以前一样,它也会为用户创建一个新的SID 。
// example (not real SID format, just show the problem)
user SID
--------------------
liuyan S-old-501 // old SID before reinstall
liuyan S-new-501 // new SID after reinstall
重新安装后的烦人问题是NTFS文件的所有权和硬盘驱动器上的权限仍与旧用户的SID相关联。
我想保留NTFS文件的所有权和权限设置,然后让新用户使用旧用户的SID,以便我可以像以前一样访问文件而没有权限问题。
该cacls
命令行工具不能在这样的情况下使用,因为该文件不属于新用户,所以它会失败的访问被拒绝的错误。而且它不能更改所有权。
即使我可以通过SubInACL
工具更改cacls
权限,也不能删除旧用户的权限,因为新安装中不存在旧用户,也无法将旧用户的权限复制到新用户。
那么,我们可以在新安装的Windows上简单地将旧用户的SID绑定到新用户吗?
样品测试批次
@echo off
REM Additional tools used in this script
REM PsGetSid http://technet.microsoft.com/en-us/sysinternals/bb897417
REM SubInACL http://www.microsoft.com/en-us/download/details.aspx?id=23510
REM
REM make sure these tools are added into PATH
set account=MyUserAccount
set password=long-password
set dir=test
set file=test.txt
echo Creating user [%account%] with password [%password%]...
pause
net user %account% %password% /add
psgetsid %account%
echo Done !
echo Making directory [%dir%] ...
pause
mkdir %dir%
dir %dir%* /q
echo Done !
echo Changing permissions of directory [%dir%]: only [%account%] and [%UserDomain%\%UserName%] has full access permission...
pause
cacls %dir% /G %account%:F
cacls %dir% /E /G %UserDomain%\%UserName%:F
dir %dir%* /q
cacls %dir%
echo Done !
echo Changing ownership of directory [%dir%] to [%account%]...
pause
subinacl /file %dir% /setowner=%account%
dir %dir%* /q
echo Done !
echo RunAs [%account%] user to write a file [%file%] in directory [%dir%]...
pause
runas /noprofile /env /user:%account% "cmd /k echo some text %DATE% %TIME% > %dir%\%file%"
dir %dir% /q
echo Done !
echo Deleting and Recreating user [%account%] (reinstall simulation) ...
pause
net user %account% /delete
net user %account% %password% /add
psgetsid %account%
echo Done ! %account% is recreated, it has a new SID now
echo Now, use this "same" account [%account%] to access [%dir%], it will failed with "Access is denied"
pause
runas /noprofile /env /user:%account% "cmd /k cacls %dir%"
REM runas /noprofile /env /user:%account% "cmd /k type %dir%\%file%"
echo Done !
echo Changing ownership of directory [%dir%] to NEW [%account%]...
pause
subinacl /file %dir% /setowner=%account%
dir %dir%* /q
cacls %dir%
echo Done ! As you can see, "Account Domain not found" is actually the OLD [%account%] user
echo Deleting user [%account%] ...
pause
net user %account% /delete
echo Done !
echo Deleting directory [%dir%]...
pause
rmdir %dir% /s /q
echo Done !
您为什么反对仅拥有文件所有权?
—
Ramhound 2012年
如果仅拥有所有权,则某些文件将无法访问,因为该权限仍设置为旧用户的SID。
—
刘岩刘研
@LiuYan刘研但是,在您拥有所有权之后,您应该可以编辑所有权限。
—
Iszi 2012年
@IsziRoryorIsznti,如果文件很少且所有权限均从父级继承,则为true。但是当文件很多,并且几乎每个文件都有单独的权限设置(例如Cygwin下的文件)时,我不能简单地将它们替换为相同的权限。
—
刘岩刘研