我是Powershell的新手,我想知道爲什麼這個功能不能按照我的需要工作。以下函數在指定的計算機的hosts文件中取消註釋行。它很棒!但我希望能夠使用Write-Output將每行輸出到屏幕並獲取變量並將其存儲在另一個數組中。這個寫輸出不做任何事情,我不能將它添加到數組中。爲什麼?上面的寫輸出工作得很好。寫入輸出失敗內部Foreach循環 - Powershell
function UnCommentHostsFile($ip){
Write-Output "This Write-Output Works!"
$hosts = $hosts | Foreach {
#This part of the function works great!
if ($_ -match $regex + $ip){
$_.replace("#", "")
#This is where I want to add it to an array and write-output but I can't.
}
else {
$_
}
#This does not output!
Write-Output $_
}
}
任何幫助將不勝感激!謝謝! :)
這沒有奏效。我嘗試了寫主機,寫輸出和寫詳細。沒有任何內容正在輸出到屏幕上,但它仍然正確地寫入文件。 –
而結果是? – mjolinor
沒什麼。這三種情況中的任何一種都不會寫入屏幕。如果將Write語句放在if語句中,它不會運行。如果我在這裏放置寫主機,我的原始寫輸出是打印出文件中的每一行。我只想要改變的線條。 –