我的問題是 powershell 7.2 switch [L] 不是全部
我找到了有關 -confirm 的資訊,但沒有幫助我如何將自動 [L] 'Not to all' 設定為一個函式。我不會讓用戶進行輸入。-Confirm:$false, ConfirmPreference = "None" 或 'Set-ExecutionPolicy Unrestricted' 沒有幫助。查詢每次都來。您知道如何將 [L] 設定為自動的想法/示例嗎?
![我如何自動設定 [L] 不是全部?](https://img.uj5u.com/2022/01/03/b79ba89539a44f1f8f30c294db5bff1d.jpg)
uj5u.com熱心網友回復:
PowerShell 的確認提示并非旨在以編程方式回應;反而:
要么:用戶應該以互動方式決定。
或者:應該完全抑制提示(暗示
[A] Yes to All回應),這在特定情況下需要:-Confirm:$false,對于定義確認影響(與 cmdlet 操作相關聯的潛在破壞性/風險的評級)的 cmdlet,可以使用公共-Confirm引數或$ConfirmPreference首選項變數進行控制,例如Remove-Item-Force, 否則會無條件地顯示確認提示的cmdlet ,例如Set-ExecutionPolicy.- 該警告是
-Force通常具有在其他cmdlet,包括的背景關系無關的含義Get-ChildItem和Remove-Item,在那里它被用來包括在列舉/缺失隱藏物品。
- 該警告是
在這種情況下
Remove-Item,特別是 -除了確認影響機制之外 - 您需要-Recurse明確表示洗掉目錄及其所有內容的意圖。
在您的特定情況下,如果目標目錄為非空,則相當于默認為[L] No to All根本不呼叫Remove-Item:
# Assume $dir contains the literal directory path of interest.
# Note that -Force in the case of Get-ChildItem causes inclusion of hidden items.
if (Get-ChildItem -Force -LiteralPath $dir | Select-Object -First 1) {
Write-Error "Cannot remove directory $dir, because it is not empty."
}
else {
Remove-Item -LiteralPath $dir
}
相反,如上所述,Remove-Item -Recurse -Confirm:$false -Force -LiteralPath $dir會抑制確認提示,但會悄悄地進行洗掉([A] Yes to All暗示)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402171.html
