我顯然在嘗試做一些錯誤的事情,所以希望有人能提供幫助,因為這看起來很容易,而且可能是一個簡單的錯誤。基本上,我正在撰寫一個創建新檔案夾的小腳本,我想包括-Confirm,但我希望它基本上只問一次用戶是否要繼續,而不是對每個創建的嵌套檔案夾進行詢問。
到目前為止,我有以下內容
function Create-NewFolderStructure{
[CmdletBinding(SupportsShouldProcess)]。
param (
[Parameter(Mandatory)
[string]$NewFolderName。
[Parameter()
$LiteralPath = (Get-Location)。
[Parameter()] [引數()
$inputFile = "zChildDirectoryNames.txt"。
)
行程 {
$here = $LiteralPath$directories = Get-Content -LiteralPath "$($here)$($inputFile)"
If (-not (Test-Path -Path "$($here)$($NewFolderName)")){
$directories | ForEach-Object {New-Item -ItemType "directory"}。 string">"directory" -Path "$($here)$($NewFolderName)" -Name $_}。
}
Else {
Write-Host "A $($NewFolderName)檔案夾已經存在"。
}
}
結束 {
}
問題是,如果我在呼叫我的函式時使用-Confirm,或者直接在New-Item中使用 - 我最終會對正在創建的每一個 "子檔案夾 "收到確認提示。即使有了第一個提示,說 "是所有 "也不能抑制未來的提示
uj5u.com熱心網友回復:
為了消費用戶對確認提示的回答,你必須實際地詢問他們!
你可以通過呼叫$PSCmdlet.ShouldContinue()來實作:
function Create-NewFolderStructure {
[CmdletBinding(SupportsShouldProcess)]。
param (
[Parameter(Mandatory, ValueFromPipeline)] [引數(Mandatory, ValueFromPipeline)
[string]$NewFolderName。
[Parameter()
$LiteralPath = (Get-Location)。
[Parameter()
$inputFile = "zChildDirectoryNames.txt"/span>。
[switch]$Force。
)
行程 {
if(-not $Force){
$yesToAll = $false.
$noToAll = $false
if($PSCmdlet.ShouldContinue("你想繼續創建新目錄'${NewFolderName}'? ", "危險!", [ref]$yesToAll, [ref]$noToAll)){
if($yesToAll){
$Force = $true $true }
}
}
else{
return }
}
}
# 在這里用實際的目錄創建邏輯替換,
# 傳遞`-Force`或`-Confirm:$false`給New-Item以抑制其自身的確認提示。
Write-Host "創建新檔案夾 ${NewFolderName}" -ForegroundColor Red
}
}
現在,如果用戶按下[A]("Yes To All"),我們將通過設定$Force來記住它,這將跳過任何后續對ShouldContinue()的呼叫:
PS ~> "檔案夾1","檔案夾2"|Creat-NewFolderStructure
危險!
你是否想繼續和創建新目錄'檔案夾 1'?
[Y] 是 [A] 是 to All [N] No [L] No to All 默認 是 "Y"/span>)。A
創建新檔案夾檔案夾1。
創建新檔案夾2檔案夾
PS ~>
我強烈建議閱讀這個深層研究,以更好地理解SupportsShouldProcess設施。你想知道的關于 ShouldProcess 的一切
uj5u.com熱心網友回復:
Mathias R. Jessen的有用回答提供了一個替代使用常見的-Confirm引數的方法,通過一個基于自定義-Force開關的稍微不同的機制,顛倒你自己嘗試的邏輯(默認顯示提示,可以用-Force跳過)。
如果你想使你的基于-Confirm的方法發揮作用:
在process塊開始時呼叫.ShouldProcess() 你自己,它呈現出熟悉的確認提示,...
......并且,如果該方法回傳 $true - 表示使用確認 - 在將 $ConfirmPreference 偏好變數的本地副本 設定為 'None' 之后,執行所需操作 以防止參與你操作的 cmdlets 對每個提示 也。
function New-FolderStructure {
[CmdletBinding(SupportsShouldProcess)]。
param (
[Parameter(Mandatory, ValueFromPipeline) ]
[string] $NewFolderName,
[字串] $LiteralPath = $PWD,
[string] $InputFile = "zChildDirectoryNames.txt"/span>.
)
# 在管道處理開始之前,這個塊被呼叫一次。
開始 {
$directories = Get-Content -LiteralPath "$LiteralPath$InputFile"/span>
}
# 如果引數$NewFolderName被系結,此塊將被呼叫一次。
# 通過*引數*,如果通過*管道*系結,則對每個輸入字串呼叫一次。
行程 {
# 如果通過了-Confirm,這將提示確認。
# 并在用戶確認的情況下回傳`$true`。
# 否則將自動回傳`$true`,這也會發生。
# 如果之前的輸入字串選擇了'[A] Yes to All'.
if ($PSCmdlet.ShouldProcess($NewFolderName) {
# 防止下面呼叫的cmdlet也**提示確認。。
# Note: This creates a *local copy* of the $ConfirmPreference
# preference變數的本地拷貝。
$ConfirmPreference = 'None'/span>
如果(-not (Test-Path -LiteralPath "$LiteralPath$NewFolderName") {
$directories | ForEach-Object { New-Item -ItemType Directory -Path "$LiteralPath$NewFolderName"/span> -Name $_ }
}
Else {
Write-Host "A $NewFolderName檔案夾已經存在"。
}
}
}
}
注意:
我將你的函式名稱從
Creat-NewFolderStructure改為New-FolderStructure,以符合PowerShell的命名慣例,使用認可的 "動詞"New,并在一些方面簡化了代碼。ValueFromPipeline被添加為$NewFolderName引數的一個屬性,以便支持通過管道傳遞多個值(例如:
'foo', 'bar' | New-FolderStructure -Confirm)- 注意,
process塊然后被呼叫為每個輸入字串,并且也將提示每個,除非你回應[A] Yes to All);PowerShell記住這個process呼叫之間的選擇,并使。 ShouldProcess()在隨后的呼叫中自動回傳$true;類似地,回應[L] No to All在隨后的呼叫中自動回傳$false。
- 注意,
需要將
$ConfirmPreference設定為'None'是因為PowerShell自動將呼叫者對-Confirm的使用翻譯成一個函式本地的$ConfirmPreference變數,值為'Low'。這使得所有支持-Confirm的cmdlet的行為就像-Confirm被傳遞一樣。.ShouldProcess()不接收此$ConfirmPreference副本值的功能內變化,所以將其設定為'None'是可以的,不會影響后續process的呼叫的提示邏輯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332617.html
標籤:
