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

Jes*_*ood 6 powershell

编辑:简而言之,对于未来的读者来说,PowerShell脚本并不打算以这种方式使用,这就是为什么没有优雅的解决方案.

我有以下行从快捷方式以管理员身份运行脚本:

C:\ Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noexit Start-Process Powershell -verb RunAs -ArgumentList"C:\ Documents\WindowsPowerShell\Scripts\test.ps1"

我想改变:

"C:\文档\ WindowsPowerShell \脚本\ test.ps1"

到相对路径,如:

"\test.ps1"

但我还没弄明白我该怎么做.如何相对于快捷方式的位置运行脚本?(快捷方式和脚本在同一个文件夹中)

noa*_*oam 3

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

Shortcut.lnk文件的目标:(%COMSPEC% /C .\launcher.cmd和开始:(%CD%空白)。

Launcher.cmd文件内容:

Powershell -noprofile -noexit -File %CD%\PSlauncher.ps1
Run Code Online (Sandbox Code Playgroud)

PSlauncher.ps1文件内容:

Start-Process Powershell -verb RunAs -ArgumentList ($pwd.path + "\test.ps1")
Run Code Online (Sandbox Code Playgroud)

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

为什么你想要捷径?