Power shell 脚本中的进度条可能吗?

Progress bar in Power shell script possible?

我很好奇我所做的功能是否有进度条,但是在过去的一个小时里我一直在尝试将其实现到我的代码中,但没有成功....

有没有可能,我的程序做了什么,它读取了一个 svr 名称列表,然后基本上调用了一个函数来从 svr 中获取信息并写入文件,这样我就有了原始数据日志,但是我厌倦了寻找在空白屏幕上,所以我想添加某种指标 .. :( 但运气不好 ..

真的很感激一些评论谢谢大家:)

清除主机

$serverList = Get-Content C:\RServerList.txt

$creds = Get-Credential

function Build_Directories
{
   foreach ($s in $serverList)
   {
     New-Item -ItemType directory -Path C:\Data$s
    }
 }


function xecute_eLogs
{
    $i = 1
    foreach ($s in $serverList)
    {
        Invoke-Command -ComputerName $s -ScriptBlock {Get-EventLog -LogName security -EntryType "Error","Warning"} -credential $creds | Out-File "C:\Data$s\sysElogs.txt"
        Write-Progress -Activity "Retrieving data from $s which is $i out of $($serverList.Count)" ` -percentComplete  ($i++*100/$serverList.Count)

    }
 }



Build_Directories
xecute_eLogs

您可以使用Write-Progress

这里有一个例子:

$max = 25
for ($i = 1; $i -le $max; $i++)
{
     Write-Progress -Activity "Counting $i until $max" `
        -percentComplete  ($i*100/$max)
     sleep -Seconds 1
}

编辑 考虑到有关状态的评论。

根据您的脚本改编后,它看起来像这样:

$i = 1
foreach ($s in $serverList)
{
     Invoke-Command -ComputerName $s -ScriptBlock {Get-EventLog -LogName system -EntryType "Error","Warning"} -credential $creds | Out-File "C:\Data$s\sysElogs.txt"

     Write-Progress -Activity "Retrieving data from $s which is $i out of $($serverList.Count)" -percentComplete  ($i++*100/$serverList.Count) -status Running
}