为什么更改 powershell 行中的可执行文件会导致它不起作用?

为什么更改 powershell 行中的可执行文件会导致它不起作用?

毫无疑问,我犯了一个非常简单的错误,但我无法弄清楚。我不是专家,我正在写论文,并使用 .bat 文件和一系列 PowerShell 指令来转换和移动文件。现有的有效文件在所有子文件夹中执行解析器 .exe:

powershell.exe -command "& {$start_path = 'C:\folder'; Get-ChildItem $start_path -Recurse -Directory | ForEach-Object {Set-Location $_.FullName | ".\OpenWeather6to6.exe"};}

c:\folder是一个占位符,OpenWeather6to6 只是我给它起的名字。

现在问题来了!我有另一个解析器,它可以生成 SQL 脚本,我想用相同的 PowerShell 行来运行它。如果通过资源管理器或 CMD 手动启动,这两个解析器都可以正常工作。但是第二个解析器不能通过 .bat 运行??它是完全相同的脚本,但带有OpenWeatherSQL.exe

.\OpenWeatherSQL.exe : The term '.\OpenWeatherSQL.exe' 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:156
+ ...  | ForEach-Object {Set-Location $_.FullName | .\OpenWeatherSQL.exe};}
+                                                   ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\OpenWeatherSQL.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

这可能与 .exe 的类型有关吗?它们都来自同一个源文件,我用 C++ 中的 CodeBlocks 编写了它们,它们的作用几乎完全相同。我感到很困惑。

PS 我知道最好通过 .ps1 脚本来运行它们。然而,在其他地方,我将 PS 工作与其他更容易运行的函数结合起来.bat,代码是否好并不重要,它只需要做这几件事,没有人会再看它或使用它;只需要克服这一点。

答案1

我不知道为什么,但它现在起作用了。据我所知,命令是一样的吗?我添加了完整路径,我不明白为什么这会比占位符更好,但无论如何,以及不起作用的 .exe 的名称。

唯一改变的是,我编辑了解析器的代码并重新编译了它。一定是因为 PowerShell 无法以某种方式运行包含特定代码的可执行文件;尽管我所做的只是一些输出格式细节的更改。

powershell.exe -command "& {$start_path = 'C:\Users\lmche\Desktop\OpenWeather\6to6data'; Get-ChildItem $start_path -Recurse -Directory | ForEach-Object {Set-Location $_.FullName | ".\OpenWeatherSQL.exe"};}

相关内容