繁体   English   中英

从批处理文件运行电源 shell 命令引发异常

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM