首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从批处理文件运行power shell命令抛出异常

从批处理文件运行power shell命令抛出异常
EN

Stack Overflow用户
提问于 2020-07-15 21:32:55
回答 1查看 40关注 0票数 0

当我从powershell运行下面的powershell命令来获取应用程序版本时,我得到了正确的响应

代码语言:javascript
复制
(Get-Item "C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe").VersionInfo.FileVersion

但是,我需要从批处理文件中运行它,所以我在批处理文件中添加了以下内容。

代码语言:javascript
复制
powershell -Command " & (Get-Item 'C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe').VersionInfo.FileVersion"

执行此命令时,我看到在控制台上返回了版本,但有异常

代码语言:javascript
复制
& : 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‘是我期望它返回的版本。

另外,如果可以建议如何将其赋值给变量。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-15 22:00:41

您应该能够使用以下两种方法之一:

代码语言:javascript
复制
@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"

或者:

代码语言:javascript
复制
@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"

如果你想把它保存为一个变量,那么也许:

代码语言:javascript
复制
@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

或者:

代码语言:javascript
复制
@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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62916091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档