thử nghiệm kịch bản:Powershell scripting: cách được đề xuất để triển khai ShouldProcess khi các cuộc gọi hàm được lồng nhau?
function outer
{
[cmdletbinding(supportsshouldprocess=$true)]
param($s)
process
{
$pscmdlet.shouldprocess("outer $s", "ShouldProcess") | out-null
"" | out-file "outer $s"
inner ImplicitPassthru
inner VerbosePassthru -Verbose:$Verbose
inner WhatifPassthru -WhatIf:$WhatIf
}
}
function inner
{
[cmdletbinding(supportsshouldprocess=$true)]
param($s)
process
{
$pscmdlet.shouldprocess("inner $s", "ShouldProcess") | out-null
"" | out-file "inner $s"
}
}
"`n** NORMAL **"
outer normal
"`n** VERBOSE **"
outer verbose -Verbose
"`n** WHATIF **"
outer whatif -WhatIf
Output:
** NORMAL **
VERBOSE: Performing operation "ShouldProcess" on Target "inner VerbosePassthru".
What if: Performing operation "ShouldProcess" on Target "inner WhatifPassthru".
What if: Performing operation "Output to File" on Target "inner WhatifPassthru".
** VERBOSE **
VERBOSE: Performing operation "ShouldProcess" on Target "outer verbose".
VERBOSE: Performing operation "ShouldProcess" on Target "inner VerbosePassthru".
What if: Performing operation "ShouldProcess" on Target "inner WhatifPassthru".
What if: Performing operation "Output to File" on Target "inner WhatifPassthru".
** WHATIF **
What if: Performing operation "ShouldProcess" on Target "outer whatif".
What if: Performing operation "Output to File" on Target "outer whatif".
What if: Performing operation "ShouldProcess" on Target "inner ImplicitPassthru".
What if: Performing operation "Output to File" on Target "inner ImplicitPassthru".
What if: Performing operation "ShouldProcess" on Target "inner VerbosePassthru".
What if: Performing operation "Output to File" on Target "inner VerbosePassthru".
What if: Performing operation "ShouldProcess" on Target "inner WhatifPassthru".
What if: Performing operation "Output to File" on Target "inner WhatifPassthru".
Để mắt của tôi có một số oddities đây:
- Xác định -WhatIf: $ foo sẽ luôn bật $ WhatIF trong callee (và callees của nó), không có vấn đề gì $ foo.
- Khi bạn chỉ định -WhatIf "cho thực" (mà không ràng buộc nó vào một biến hiện tại), nó sẽ lan truyền đến callees ngầm. Không cần passthru hoặc splatting.
- Không giống như -WhatIf, rõ ràng -Xác lý không xếp tầng để callees ngầm.
- Khi bạn cố gắng theo cách thủ công passthru -Xoá: $ foo, bạn thấy hành vi tương tự như -WhatIf: $ foo. Nhưng nó chỉ ảnh hưởng đến các kịch bản thủ công kiểm tra $ psCmdlet.ShouldProcess() - được xây dựng trong các lệnh ghép ngắn không bị ảnh hưởng.
N.B.: Xác nhận hành vi giống với WhatIf. Tôi bỏ qua nó một cách ngắn gọn.
Tìm kiếm trên web và Kết nối, tôi thấy hầu như không có bất kỳ cuộc thảo luận sâu về hành vi ShouldProcess (pro hoặc con) liên quan đến các chức năng nâng cao. Điều gần nhất là a post from James O'Neill đề xuất chuyển một phiên bản $ psCmdlet duy nhất trong suốt ngăn xếp cuộc gọi. Tuy nhiên, ông làm như vậy để giải quyết một vấn đề hoàn toàn khác (tránh nhiều lời nhắc -Xác nhận). Trong khi đó, khi bạn gắn bó với tiêu chuẩn $ psCmdlet được cung cấp cho mỗi chức năng, tôi thấy không có tài liệu về những gì mong đợi ... nhiều mẫu thiết kế ít hơn, thực hành tốt nhất, v.v ...
Bug nộp: https://connect.microsoft.com/PowerShell/feedback/details/535559/shouldprocess-cmdlets-the-verbose-preference-is-not-evaluated-in-callees Buộc null - > True cũng là một lỗi IMO, vì nó không xảy ra với các thông số chuyển đổi bình thường. Đã nộp riêng biệt @ https://connect.microsoft.com/PowerShell/feedback/details/535557/shouldprocess-synthetic-parameters-should-not-coerce-null-true –
Đã bỏ phiếu. Cảm ơn bạn đã nộp đơn. –
Các bạn làm việc tốt. – JasonMArcher