簡體   English   中英

powershell命令不在.bat文件中運行?

[英]powershell command not running in .bat file?

我正在嘗試制作一個簡單的腳本來設置我的gcc變量。 作為.bat文件。

像這樣設置變量

$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin"

當我將其鍵入/粘貼到電源外殼中時,它運行得很好。

但是當我粘貼到腳本myscript.bat中並通過powershell運行它時,出現此錯誤:

C:\Users\Brett\Compilers>"$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin""
The filename, directory name, or volume label syntax is incorrect.
PS C:\Users\Scruffy\Compilers>

PowerShell是Windows命令行(cmd.exe)的獨立執行環境

如果要從批處理文件運行powershell命令,則需要保存powershell腳本(.ps1),並將其作為命令行參數傳遞到powershell.exe中。

例:

powershell.exe -noexit c:\scripts\test.ps1

可在Microsoft TechNet上獲得更多信息。

通常,將批處理內容保留到.BAT文件中,然后將PowerShell內容放入.ps1文件中。

我可以在這里復制您的結果-但這些是可以預期的。 Cmd.exe看到的是字符串然后是路徑,然后由於語法不是命令提示符可以處理的語法而變得非常混亂。 因此,它給出了該錯誤消息。

如果要在路徑中添加內容,那么為什么不將語句放入.ps1腳本文件中呢?

正如其他人提到的,您需要將代碼保存在.ps1文件中,而不是.bat中。

這行代碼(來自Setting Windows PowerShell path variable )可以解決這個問題:

$env:Path = $env:Path + ";C:\Users\Brett\Compilers\MinGW\bin"

甚至更短:

$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin" 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM