在powershell输出文件中列出命令

时间:2015-08-13 14:28:16

标签: powershell

非常新的脚本,我尝试使用其他人'脚本。我正在运行PowerShell脚本,该脚本使用Get-Content从文件中提取命令列表,执行命令,然后将命令结果输出到日志文件。该脚本按照这些规范工作。我的问题是我希望在日志文件中输出每个命令之前列出它们。如果可能的话,如何在Powershell中实现这一目标?

1 个答案:

答案 0 :(得分:0)

我假设您有这样的文件:

Commands.txt
Get-Date
Get-Host | Select Version
Copy-item c:\file.txt -destination C:\temp\file.txt

如果你想在运行它之前显示每个命令,你可以运行它:

$commands = Get-Content .\commands.txt 
ForEach ($command in $commands) {
write-output "Executing: $command"
Invoke-expression $command }
}

你会得到这样的输出:

Executing: Get-Date

Thursday, August 13, 2015 10:32:02 AM
Executing: Get-Host | select Version

Version : 5.0.10240.16384

Executing: Copy-item c:\file.txt -destination C:\temp\file.txt -passthru

LastWriteTime : 8/13/2015 10:26:14 AM
Length        : 0
Name          : file.txt