[英]Running a power shell command from batch files throws exception
當我從 powershell 運行以下 powershell 命令以獲取應用程序版本時,我得到了正確的響應
(Get-Item "C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe").VersionInfo.FileVersion
但是我需要從批處理文件中運行它,所以我在我的批處理文件中添加了以下內容。
powershell -Command " & (Get-Item 'C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe').VersionInfo.FileVersion"
執行此操作時,我看到控制台上返回的版本但有異常
& : The term '1.2.0.33' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ & (Get-Item 'C:\\Program Files\\CompanyName\\AppName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (1.2.0.33:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
上述錯誤中的“1.2.0.33”是我希望它返回的版本。
此外,是否可以建議如何將其分配給變量。
您應該可以使用:
@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
或者:
@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
如果您想將其保存為變量,那么也許:
@For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do @Set VerInfo=%%G
或者:
@For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do @Set VerInfo=%%G
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.