将 -WhatIf 标志传递给在 powershell 脚本中调用的所有 cmdlet

Pass -WhatIf flag to all cmdlets being called in powershell script

我有一个测试 powershell 脚本,我正在尝试将 whatif 标志传递给脚本中的所有 cmdlet。在我的脚本中,我调用了 3 个 cmdlet new-Item、copy-item 和 new-customCmdlet。最后一个是我编写的自定义 cmdlet。我添加了:

[cmdletBinding(SupportsShouldProcess=$True)] 

测试脚本和自定义 cmdlet。当我 运行 这个概念验证脚本与 -Whatif 两个系统 cmdlet(New-Item 和 copy-Item)运行 在 whatif 模式下但自定义没有。我认为如果一个 cmdlet 有 supportsShouldProcess 如果脚本在 whatif 中 运行ning 它应该得到 whatif 标志。两个系统 cmdlet 都是这种情况,但我的不是。我已经阅读了有关查看调用堆栈的其他文章,并且了解如何查看是否已设置 whatif。对于此示例,我只希望 cmdlet 使用脚本 运行 所带的 -whatif 标志。

这是我的脚本 运行 whatif 标志: PS c:> script.ps1 -WhatIf

[cmdletBinding(SupportsShouldProcess=$True)]
param()
Import-Module c:\mycmdlet.dll

New-Item -ItemType file NewItem.txt
Copy-Item item1.txt copiedItem.txt
Copy-Custom test.txt CopiedTest.txt

这是 cmdlet 代码:

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Reflection;

namespace poc.whatif.Cmdlet
{
    [Cmdlet(VerbsCommon.Copy, "Custom")]
    public class CopyCustom : PSCmdlet
    {
        #region paramaters
        [Parameter(Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Source File")]
        public string srcFile { get; set; }

        [Parameter(Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Target File")]
        public string targetFile { get; set; }
        #endregion


    protected override void BeginProcessing()
    {
        WriteVerbose("Starting Copy-Custom");
    }

    protected override void ProcessRecord()
    {
       WriteVerbose("Copying files from here to there");   
    }

    protected override void EndProcessing()
    {
        WriteVerbose("Ending Copy-Custom");
    }
}

我不使用 C# cmdlet,但据我所知,您的自定义 cmdlet 尚未声明它支持 ShouldProcess,那么它如何使用它呢?例如:

[Cmdlet(VerbsCommon.Copy, "Custom")]

应该是这样的

[Cmdlet(VerbsCommon.Copy, "Custom", SupportsShouldProcess = true)]

然后您需要在 begin/processing/end-methods 中使用 ShouldProcess() 方法来实际定义 whatif 消息应该说的位置和时间。

您错过了 SupportShouldProcess attribute. Also you are missing the call to ShouldProcess also make sure you look at ShouldContinue and use a Force parameter as well. See the required development guidelines 了解更多信息。

重写 ProcessRecord:

protected override void ProcessRecord()
{
    if(ShouldProcess("target","action"))
    {
        if(ShouldContinue("Do Stuff","Do stuff to this object?"))
        {
            WriteVerbose("Copying files from here to there");
        }
    }
}

我对 C# 也不太了解,但是 preference variable $WhatIfPreference 似乎 非常 与您的问题范围相关。

Determines whether WhatIf is automatically enabled for every command that supports it. When WhatIf is enabled, the cmdlet reports the expected effect of the command, but does not execute the command.

因此,如果您只是谈论大多数支持 -WhatIf 的内置 cmdlet,那么我认为这只是一个问题:

$WhatIfPreference = 1 # Default is 0