且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell:使用相对路径从快捷方式运行脚本

更新时间:2023-01-31 10:12:18

这是一个丑陋的解决方法.

Here is an ugly workaround.

Shortcut.lnk 文件:%COMSPEC%/C .\launcher.cmd (source) 并开始于:%CD%(或 空白).

Shortcut.lnk file with Target: %COMSPEC% /C .\launcher.cmd (source) and Start In: %CD% (or blank).

Launcher.cmd 包含内容的文件:

Powershell -noprofile -noexit -File %CD%\PSlauncher.ps1

PSlauncher.ps1 包含内容的文件:

Start-Process Powershell -verb RunAs -ArgumentList ($pwd.path + "\test.ps1")

当然有更好的解决方案.也许使用 Start-Process-WorkingDirectory 参数/a>?或者使用 Convert*-SecureString cmdlet存储凭据?算我好奇.

Surely there is a better solution. Maybe with the -WorkingDirectory parameter of Start-Process? Or storing credentials with the Convert*-SecureString cmdlets? Count me curious.

为什么你想要快捷方式?