将 .txt 文件传递​​给 Powershell 命令

Passing a .txt file to a Powershell command

我正在尝试通过 powershell 将带有参数的 .txt 文件传递​​给 .exe 文件。目前,这就是我所拥有的。

Write-Host "starting upgrade at $(Get-Date -format 'U')"


C:\dev\temp.exe.exe /DIR="C:\TEST" /BTPServerHost="Test" /DBInstance="testDB" /Log=C:\path\to\test\testlog.txt

这是在接受命令行输入的 InnoScript 文件中调用一个函数。

如何格式化 .txt 文件,以及如何将其传递到 .exe?任何帮助,将不胜感激!谢谢!

此脚本从 txt 文件中获取参数并将它们传递到可执行文件中,并自动填充安装向导中的字段。这就是我想要做的,但我不想为输入 txt 文件中的每个参数启动一个新进程。

Write-Host "starting upgrade at $(Get-Date -format 'U')" Get-Content -Path C:\TestInput.txt -Raw | foreach {Start-Process C:\dev\test.exe -ArgumentList $_}

传入的TestInput.txt文件如下所示:

 /DIR="C:\TEST" 
 /ServerHost="Test" 
 /DBInstance="testDB" 
 /Log=C:\testlog.txt

如果您是说,在此文本文件中,单独的行上只有这些参数行,并且您是说您已经尝试过类似下面的操作但没有成功?

消息行也不需要 Write-Host,因为默认输出到屏幕。您通常只需要 Write-Host 来为屏幕文本着色,以及其他一些格式化案例,具体取决于您在做什么。总而言之,应避免写入主机。

"starting upgrade at $(Get-Date -format 'U')"
($ConsoleCommand = Get-Content -Path 'd:\temp\input.txt' -Raw)

# Results - showing the commands in the file before process them

whoami
get-date
'hello world'

如果不使用 -Wait 开关,这将生成 3 个单独的 PowerShell 控制台并显示结果

ForEach($CmdLine in $ConsoleCommand)
{ Start-Process -FilePath powershell -ArgumentList "-NoExit","-Command  &{ $CmdLine }" }

你当然可以指向你的 .exe 与我在这里所做的。

Start-Process

通过在指定 .txt 文件路径后添加 -Raw,它会忽略换行符和 returns 一个字符串中的文件的全部内容并保留换行符。默认情况下,文件中的换行符用作分隔符,将输入分隔为字符串数组。