通过CMD的PS脚本

时间:2019-04-02 14:52:41

标签: powershell cmd

我正在尝试通过CMD运行PS表达式,但出现错误

powershell -command "Get-ChildItem <FOLDERPATH> -Recurse -file | % { "{0}  {1}" -f $_.FullName, [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName).FileVersion }

2 个答案:

答案 0 :(得分:0)

我已经确认,如安东尼·斯金格(Anthony Stringer)所建议的,将单引号与双引号一起使用

powershell -command "Get-ChildItem <FOLDERPATH> -Recurse -file | % { '{0}  {1}' -f $_.FullName, [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName).FileVersion }

答案 1 :(得分:0)

问题是'%'字符和引号内的引号。请改用ForEach-Object。别名不应在脚本中使用。在格式字符串上使用撇号代替引号。

@ECHO OFF
SET "FOLDERPATH=C:/src"
powershell -NoLogo -NoProfile -Command ^
    "Get-ChildItem '%FOLDERPATH%' -Recurse -File |" ^
        "ForEach-Object { '{0}  {1}' -f $_.FullName, [Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName).FileVersion }"
相关问题