如何通过批处理脚本测试用户的有效权限?

发布于 2024-12-11 18:14:21 字数 258 浏览 0 评论 0原文

我需要在批处理脚本中测试特定用户(即不一定是当前用户)的有效权限,并据此采取行动(提供警告)。我想要一个可以调用的子例程来检查指定文件或目录的权限,以便我可以测试只有管理员才能访问的内容(从而警告授予的权限过高)并检查数据可以访问我的应用程序路径中的目录(否则权限太低)。我希望它能在 XP、2008 和 win7 上运行。

顺便说一句,我已经弄清楚如何解析“net localgroup Administrators”,但我认为这不足以满足我的需求。

I need to test the effective permissions of a specific user (i.e. not necessarily the current user) from within a batch script and take action (provide a warning) based on that. I'd like to have a subroutine I could call to check privileges against a specified file or directory so I could test that something only an administrator should be able to access (and thus warn that too high of permissions are granted) and check that data directories in my apps path can be accessed (otherwise too low of permissions). I'd like this to work in XP, 2008, and win7.

By the way, I have figured out how to parse "net localgroup Administrators", but I don't think this is sufficient for my needs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-12-18 18:14:21

@Jared

根据您的需求,我认为通过简单的 copy%errorlevel% 您就可以获得您想要的。

copy %tempFile% %yourProtectedDir%
if %errorlevel% == 1 goto sorryYouFail
if %errorlevel% == 0 goto youAreIn
...
del %tempFile% /Q

或者,要检查任何用户,请执行相同的操作,但使用 Windows 受保护的文件夹...

copy %tempFile% %windir%\system32
if %errorlevel% == 1 goto youDontHaveAdminPrivileges
if %errorlevel% == 0 goto howdyThereAdmin
...
del %tempFile% /Q

如果您需要测试其他用户,请尝试使用 runas 选项...

@Lizz

此脚本检查 Windows 版本,并提升临时文件以使用管理员权限运行特定文件。

@echo off
title Detect and run file with Admin privileges

set yourFile=yourFileNameAsAdmin.bat
set privileges=no

VER | FINDSTR /IL "6.2." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=8
SET privileges=yes
)

VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=7
SET privileges=yes
)

VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=Vista
SET privileges=yes
)

if "%privileges%"=="no" goto SkipElevation
If "%privileges%"=="yes" goto Elevation

:SkipElevation
call %CD%\%yourFile%
goto End

:Elevation
PushD "%~dp0"
If Exist "%~0.ELEVATED" Del /f "%~0.ELEVATED"

Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('%yourFile%', '/%cd:~0,1% %CMD_Args%', '', 'runas')
Echo %ELEVATED_CMD% >> "%~0.ELEVATED"
call %ELEVATED_CMD%

Del /f "%~0.ELEVATED"
goto End

:End
Echo -------------------------------
Echo All done!
Pause
goto EOF

注意:如果 yourFileNameAsAdmin.bat 使用文件的相对路径,请记住在文件开头启用扩展名和本地目录:

@echo off
@setlocal enableextensions
@cd /d "%~dp0"
::...your code here

希望有帮助!...


作为附加相关信息...

使用命令 net 您可以获得有关用户和组的大量信息,并且您还可以与他们一起工作。

例如

NET USER

  • net users 列出所有用户。
  • net users [name] 有关特定用户的详细信息,包括他所属的所有组
  • net help users 了解更多...

NET LOCALGROUP

  • net localgroup 列出所有组。
  • net localgroup [groupName] 有关特定组的详细信息,包括属于该组的所有用户
  • net help localgroup 了解更多...

@Jared,

For your needs, I think with a simple copy and %errorlevel% you can have what you want.

copy %tempFile% %yourProtectedDir%
if %errorlevel% == 1 goto sorryYouFail
if %errorlevel% == 0 goto youAreIn
...
del %tempFile% /Q

Or, to check for any user, do the same but with a windows protected folder...

copy %tempFile% %windir%\system32
if %errorlevel% == 1 goto youDontHaveAdminPrivileges
if %errorlevel% == 0 goto howdyThereAdmin
...
del %tempFile% /Q

If you need to test other user try with the runas option...

@Lizz

This script check windows version, and elevate a temporary file to run a specific one with ADMIN RIGHTS.

@echo off
title Detect and run file with Admin privileges

set yourFile=yourFileNameAsAdmin.bat
set privileges=no

VER | FINDSTR /IL "6.2." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=8
SET privileges=yes
)

VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=7
SET privileges=yes
)

VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 (
SET winVersion=Vista
SET privileges=yes
)

if "%privileges%"=="no" goto SkipElevation
If "%privileges%"=="yes" goto Elevation

:SkipElevation
call %CD%\%yourFile%
goto End

:Elevation
PushD "%~dp0"
If Exist "%~0.ELEVATED" Del /f "%~0.ELEVATED"

Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('%yourFile%', '/%cd:~0,1% %CMD_Args%', '', 'runas')
Echo %ELEVATED_CMD% >> "%~0.ELEVATED"
call %ELEVATED_CMD%

Del /f "%~0.ELEVATED"
goto End

:End
Echo -------------------------------
Echo All done!
Pause
goto EOF

Note: If yourFileNameAsAdmin.bat use RELATIVE path to files, remember to enableextensions and the local dir at the beginning of the file:

@echo off
@setlocal enableextensions
@cd /d "%~dp0"
::...your code here

Hope that helps!...


As adition related info...

With the command net you get a lot of info about users and goups, and also can you work with them.

e.g.

NET USER

  • net users List off all users.
  • net users [name] Detail info about a specific user, including all the groups that he belongs.
  • net help users for more...

NET LOCALGROUP

  • net localgroup List all groups.
  • net localgroup [groupName] Detail info about a specific group, including all the users that belongs to that grup.
  • net help localgroup for more...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文