如何使用正确的工作目录从BAT文件启动PowerShell脚本? [英] How to start PowerShell script from BAT file with proper Working Directory?
本文介绍了如何使用正确的工作目录从BAT文件启动PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建bat脚本,该脚本可以在适当的工作目录下启动与bat文件相同名称的PowerShell脚本.
I'm trying to create bat script that can start PowerShell script named the same as bat file in proper working directotry.
这就是我得到的:
@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -WorkingDirectory '%~dp0' -Verb RunAs}"
PAUSE
以这种方式传递工作目录无效.
Passing working directory this way does not work.
如何制作能够通过正常工作目录以及命令行参数的脚本?
How to make script that will pass proper working directroy and also command line arguments?
推荐答案
一种解决方法是使用以下命令让PowerShell脚本将目录更改为其自身的来源:
A workaround is to let the PowerShell script change the directory to it's own origin with:
Set-Location (Split-Path $MyInvocation.MyCommand.Path)
作为第一个命令.
按照 mklement0 的提示:在PSv3 +中,使用更简单的方法:
As per mklement0s hint: In PSv3+ use the simpler:
Set-Location -LiteralPath $PSScriptRoot
或使用此目录打开相邻文件.
Or use this directory to open adjacent files.
$MyDir = Split-Path $MyInvocation.MyCommand.Path
$Content = Get-Content (Join-Path $MyDir OtherFile.txt)
这篇关于如何使用正确的工作目录从BAT文件启动PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文