PowerShell:使用相对路径从快捷方式运行脚本 [英] PowerShell: Run script from shortcut using relative path
问题描述
编辑:简而言之,对于未来的读者来说,PowerShell 脚本不打算以这种方式使用,这就是为什么没有优雅的解决方案.
EDIT: To future readers, in short, PowerShell scripts weren't intended to be used this way which is why there is no elegant solution.
我有以下行以管理员身份从快捷方式运行脚本:
I have the following line which runs a script as an administrator from a shortcut:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile-noexit Start-Process Powershell -verb RunAs -ArgumentList "C:\Documents\WindowsPowerShell\Scripts\test.ps1"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noexit Start-Process Powershell -verb RunAs -ArgumentList "C:\Documents\WindowsPowerShell\Scripts\test.ps1"
我想改变:
C:\Documents\WindowsPowerShell\Scripts\test.ps1"
"C:\Documents\WindowsPowerShell\Scripts\test.ps1"
到一个相对路径,如:
".\test.ps1"
".\test.ps1"
但我还没有想出如何做到这一点.如何相对于快捷方式的位置运行脚本?(快捷方式和脚本在同一个文件夹中)
but I haven't figured out how I can do that. How can I run the script relative to the location of the shortcut? (The shortcut and script are in the same folder)
推荐答案
这是一个丑陋的解决方法.
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.
为什么你想要快捷方式?
这篇关于PowerShell:使用相对路径从快捷方式运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!