水铝英石

win_add_Administrator_permission_in_right_click

承接上文win_add_powershell_prompt_in_right_click – 水铝英石 (allophane.com)

Win+R regedit

进\HKEY_CLASSES_ROOT\Directory\Background\shell\里添加runas项,再在里面添加command

  • \HKEY_CLASSES_ROOT\Directory\Background\shell\
    • runas
      • commad

然后在runas项里新建字符串值和DWORD值:

名称类型数据
默认REG_SZps_prompt_admin
IconREG_SZD:\ProgramData\Anaconda3\Menu\Iconleak-Atrous-PSConsole.ico
ShowBasedOnVelocityIdREG_DWORD639bc8

command里添加字符串值

名称类型数据
默认REG_SZpowershell.exe -ExecutionPolicy ByPass -NoExit -Command “& ‘D:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1’ ; conda activate ‘D:\ProgramData\Anaconda3’ ; Set-Location -literalPath ‘%V'”

ShowBasedOnVelocityId 这个值用来更改显示状态与隐藏状态(HideBasedOnVelocityId),其实不添加也可以。

其中 runas的 Icon 和 command 的默认值 可以从快捷方式中的 目标图标 中获取,并且需要修改 目标 中的代码

icon没什么好说的,主要解释一下脚本:

这个是powershell prompt原脚本的内容

powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'D:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'D:\ProgramData\Anaconda3' "
powershell.exe调用
-ExecutionPolicy ByPasspowershell对于脚本的执行有着严格的安全限制,
策略 Policies状态分类:Restricted / AllSigned / RemoteSigned / Unrestricted / Bypass / Undefined
设置成Bypass相当于临时使用模式,执行策略权限较低
-NoExit相当于打开命令行后不退出窗口
-Command相当于直接执行后续的脚本内容,
&”运算符可以定义为调用 PowerShell 中的命令、脚本文件、函数和可操作程序的运算符
相当于:
1. 执行conda-hook.ps1的脚本,配置anaconda环境变量
2. conda activate 这句相当于启动默认的base环境
其中conda-hook.ps1可以认为是配置anaconda环境,很类似之前mmcv硬件编译前的配置,这里就不细讲了

而一般来说如果管理员调用的话,会直接蹦到system32目录,这也是难以避免的,但是我们其实更希望打开就在目录下
于是在此基础上添加Set-Location,将当前PowerShell的工作位置设置为指定位置
-LiteralPath参数用于指定一个或多个位置的路径。

Set-Location -literalPath '%V'

最后得到上面表格里的形式

powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'D:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'D:\ProgramData\Anaconda3' ; Set-Location -literalPath '%V'"
滚动到顶部