Board logo

标题: [原创代码] powershell 右键运行菜单 [打印本页]

作者: went    时间: 2021-5-20 15:09     标题: powershell 右键运行菜单

保存为runps1.bat文件
添加或删除ps1文件右键菜单->按指定powershell版本运行脚本
无视执行策略 - ExecutionPolicy
非右键菜单执行语法: runps1.bat "ps1脚本路径" "powershell指定版本[当前版本留空]"
无参数运行则视为 添加/删除右键菜单.
调试win7 ps2.0兼容代码使用.
  1. @echo off
  2. REM %1为空,以管理员身份运行bat,退出
  3. if "%~1"=="" start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","REM",,"runas",1)(window.close)&exit
  4. REM %~1为REM,添加或删除注册表,退出
  5. if "%~1"=="REM" (
  6. reg delete "HKCR\Microsoft.PowerShellScript.1\Shell\以powershell 2.0运行" /f >nul 2>nul && (
  7. echo 删除ps1文件右键菜单 "以powershell 2.0 版本运行" 成功
  8. reg delete "HKCR\Microsoft.PowerShellScript.1\Shell\以powershell 当前版本运行" /f >nul 2>nul
  9. echo 删除ps1文件右键菜单 "以powershell 当前版本运行" 成功
  10. ) || (
  11. reg add "HKCR\Microsoft.PowerShellScript.1\Shell\以powershell 2.0运行\command" /ve /t REG_SZ /d "\"%~0\" \"%%1\" \"2.0\"" /f >nul 2>nul
  12. echo 添加ps1文件右键菜单 "以powershell 2.0 版本运行" 成功
  13. reg add "HKCR\Microsoft.PowerShellScript.1\Shell\以powershell 当前版本运行\command" /ve /t REG_SZ /d "\"%~0\" \"%%1\"" /f >nul 2>nul
  14. echo 添加ps1文件右键菜单 "以powershell 当前版本运行" 成功
  15. )
  16. pause&exit
  17. )
  18. REM %1文件不存在,退出
  19. if not exist "%~1" (echo 脚本文件不存在! %~1 &pause&exit)
  20. REM %1文件存在,读取ps版本参数%2
  21. if not "%~2"=="" set "psVer=-version %~2"
  22. REM 运行powershell脚本
  23. cd /d "%~dp1"
  24. powershell %psVer% -c "Get-Content '%~1' | Out-String | Invoke-Expression"
  25. pause&exit
复制代码

作者: sanduo.xi    时间: 2021-5-20 17:00

貌似我用不到,不过有的代码段我可以借用。谢谢啦!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2