2016-07-25 30 views
0
Function Test{ 
    while($true){ 
    write-host "I want this to refresh every time" 
    Start-Sleep -Seconds 4 
    cls 
    } 
} 
Write-Host "I want this to be at the screen always" 
Test 

有沒有辦法讓我做到這一點,而不是把它也放在循環或功能?在實際的代碼中,我想保留一些輸出在屏幕頂部作爲參考(它不能放在函數中),而函數會提取一些數據並每4秒刷新一次。 在這裏,因爲我使用cls它清除了我不想發生的屏幕上的所有內容。我只需要清除該函數的輸出。如何清除屏幕上的某些內容?

+0

你也許能夠把[這](http://powershell-tips.blogspot.com/2011/05/moving-cursor-to-specific-location.html)類型的方法和舉措光標四周。 –

回答

1

進度條適合嗎?實際的比例可能不相關的,因爲本例中的循環是無限的,但你描述它的行爲類似於:

Function Test{ 
    while ($true) { 
     Write-Progress -Activity "I want this to be at the screen always" -Status "I want this to refresh every time" 
     Start-Sleep -Seconds 4 
    } 
} 
Write-Progress -Activity "I want this to be at the screen always" 
Test 
+0

您好我認爲這不符合我的要求。我在函數外有相當多的輸出命令。一些來自'write-host'和很多其他功能 – tuxebin

0

如果寫進展不適合再使用全局變量的函數可能會爲你工作:

Function Clear-HostCustom ($DisplayText) 
{ 
    If ($DisplayText) 
    { 
     $global:DisplayTextGlobal = $DisplayText 
    } 
    Clear-Host 
    Write-Host $global:DisplayTextGlobal 
} 
Function Test{ 
    while($true){ 
    write-host "I want this to refresh every time" 
    Start-Sleep -Seconds 4 
    Clear-HostCustom 
    Start-Sleep -Seconds 1 
    } 
} 
Clear-HostCustom "I want this to be at the screen always" 
Test