3

各位大佬,问个命令行问题

 2 years ago
source link: https://www.v2ex.com/t/822679
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Windows

各位大佬,问个命令行问题

  Gizmo · 21 小时 21 分钟前 via Android · 1231 次点击

已经打开了 powershell 或 cmd 的情况下,可以提升到管理员权限(像右键选择以管理员打开 powepowershell 或 cmd )吗?

可以的话是什么命令?

12 条回复    2021-12-17 11:33:40 +08:00

monkey110

monkey110      21 小时 12 分钟前

runas /user:administrator "cmd"

codergrowing

codergrowing      20 小时 56 分钟前

Windows 上也有一个 sudo 的工具,可以实现类似*nix 系统上的效果。
http://blog.lukesampson.com/sudo-for-windows

```
# first download scoop
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

# enable powershell, if you haven't already
set-executionpolicy unrestricted -s cu -f

scoop install sudo
```

ncepuzs

ncepuzs      20 小时 52 分钟前

flynaj

flynaj      17 小时 46 分钟前 via Android

runas /user:administrator

flynaj

flynaj      17 小时 46 分钟前 via Android

就是 1 楼这个

ysc3839

ysc3839      16 小时 49 分钟前

一楼的是用密码登录的方案,不是弹出 UAC 授权提示。

弹出 UAC 授权提示的原理之一是调用 ShellExecute ,把 Operation 参数设置成 "runas"。https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew#runas

PowerShell 可以用 Start-Process -Verb RunAs ,也可以直接调用 ShellExecute API 。

cmd 不能直接调用 Win32 API ,间接的方案可以利用 mshta.exe 通过 URL 执行 VBScript 或 JScript ,创建 "Shell.Application" 对象,然后调用其中的 ShellExecute ,传入 "runas"。

mshta vbscript:CreateObject("Shell.Application").ShellExecute("","","","runas",1)(window.close)

ysc3839

ysc3839      16 小时 47 分钟前

刚刚回复时触发关键词被拒绝访问了,最后没有发完。完整命令如下:
mshta vbscript:CreateObject("Shell.Application").ShellExecute("程序名称","","","runas",1)(window.close)

Tumblr

Tumblr      8 小时 57 分钟前

1. 在原进程上不可以提升为管理员,只能重新启一个进程。
2. 用 PowerShell 的 Start-Proces 加-Verb RunAs 参数(推荐后者)。

另外,对 @codergrowing #2 提到的 sudo 的内容好奇,怎奈没找到代码,不知道有人在用没,是否可以分享一下。

chengyiqun

chengyiqun      8 小时 32 分钟前

@Tumblr 我自己就在用 scoop 里的 sudo. 非常方便.
不过我更习惯这样
![image.png]( https://s2.loli.net/2021/12/17/zBoPpdrR7yiAkJq.png)

参考 github 上的 windowsterminal-shell

Tumblr

Tumblr      7 小时 53 分钟前

@ragnaroks #9 多谢!原来也是个变相的 start-process -verb runas ,然后隐藏窗口了。
@chengyiqun #10 这个确实也方便,不过在 Windows 11 里又是个 challenge 😌

xingchen23

xingchen23      6 小时 8 分钟前

没安 scoop 个人用的是 gsudo 不知道满不满足楼主需求

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK