调用批处理文件并跟踪进度

时间:2015-03-09 20:55:22

标签: powershell batch-file

所以这就是我想要做的事情。

我有一个Powershell脚本,可以调用一堆安装软件的批处理文件。反正有没有进度条(GUI将是我的选择)来跟踪正在调用的批处理文件的状态?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我在TechNet找到了这个;这篇文章是由Ed Wilson撰写的。

  

使用Write-Progress cmdlet时,需要两个参数。第一个是活动参数。为此参数提供的字符串显示在进度对话框的第一行。第二个必需参数是status参数。它出现在活动行下。

答案 1 :(得分:0)

我可以从PowerShell脚本中提供一个示例。使用This作为计时器。

下面的代码有一个FOR循环来循环$variable.Count

中的项目
$time = 7
$percentage = $i / $time
$remaining = New-TimeSpan -Seconds ($time - $i)
$message = "{0:p0} complete" -f $percentage, $remaining    
Write-Progress -Activity "Working" -status $message -PercentComplete ($percentage * 100)

此示例略有不同,使用ForEach迭代集合中的项目并更新进度条。下面将运行并更新进度条超过60秒。

$time = 60 # seconds
foreach($i in (1..$time)) {
$percentage = $i / $time
$remaining = New-TimeSpan -Seconds ($time - $i)
$message = "{0:p0} complete, remaining time {1}" -f $percentage, $remaining
Write-Progress -Activity "Wait for SCCM scan" -status $message -PercentComplete ($percentage * 100)
Start-Sleep 1

- 编辑:

这是我提出的代码,它成功启动了5个批处理文件,每个文件包含ping 1.1.1.1 -n 2 >NUL,并且-n计数增加以模拟已用时间。 *注意PercentComplete选项是行为不端的,我的经验很少,因为我不确定它在这个例子中会起作用。 -edit,忘了归功this postWrite-Progress工作。

$commands = gc C:\test4\l.txt
$i = 0
foreach ($bat in $commands){
Start-Process cmd -ArgumentList "/c $bat" -Wait -WindowStyle Minimized
$i++
Write-Progress -Activity "Batch File Test" -Status "Completed: $i of $($commands.Count)" -PercentComplete (($i / $commands.Count) * 100)
}#END FOREACH
Write-Host "Batch files finished running!"