此輸出不適合視窗
get-netFirewallRule | where { $_.Action -eq 'Block' -and $_.Enabled -eq 'True' }
read-host -prompt "Press Enter to continue..."
當我嘗試
Remove-Item -Path ($env:userprofile "\Desktop\enabledBlockRules_WindowsFilteringPlatform.txt")
get-netFirewallRule | where { $_.Action -eq 'Block' -and $_.Enabled -eq 'True' } | forEach {
$_ | Add-Content -Path ($env:userprofile "\Desktop\enabledBlockRules_WindowsFilteringPlatform.txt") -Encoding "utf8"
}
read-host -prompt "Exported to desktop ."
我得到了無意義的結果。我怎么能這樣做?
uj5u.com熱心網友回復:
要簡化您的答案并添加一些背景關系:
Get-NetFirewallRule |
Where-Object { $_.Action -eq 'Block' -and $_.Enabled } |
Out-File -Encoding utf8 -FilePath "$env:userprofile\Desktop\enabledBlockRules_WindowsFilteringPlatform.txt"
注意如何Out-File被直接輸送到,這避免了需要洗掉輸出檔案第一和與低效回圈-Append。
至于為什么從Add-Contentto切換會Out-File有所不同:
Set-Content/對其輸入物件Add-Content執行簡單的.ToString()字串化,對于復雜的物件,這通常會導致無用的字串表示。相比之下,
Out-File(>) /Out-File -Append(>>) 使用通常的 for-display輸出格式系統,產生與輸出到控制臺時相同的豐富格式。- 但是請注意,這些表示是針對人類觀察者的,因此通常不適合程式處理;對于后者,請使用結構化文本格式,例如 CSV 或 JSON。
有關詳細資訊,包括有關 Windows PowerShell 中 cmdlet 之間不同默認字符編碼的資訊,請參閱此答案。
uj5u.com熱心網友回復:
該死
Remove-Item -Path ($env:userprofile "\Desktop\enabledBlockRules_WindowsFilteringPlatform.txt")
get-netFirewallRule | where { $_.Action -eq 'Block' -and $_.Enabled -eq 'True' } | forEach {
$_ | Out-File -FilePath ($env:userprofile "\Desktop\enabledBlockRules_WindowsFilteringPlatform.txt") -Encoding "utf8" -Append
}
read-host -prompt "Exported to desktop ."
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379957.html
標籤:电源外壳
下一篇:使用檔案夾移動檔案
