相关疑难解决方法(0)

如何在调用其他Cmdlet的Cmdlet中支持PowerShell的-WhatIf&-Confirm参数?

我有一个支持-WhatIf&-Confirm参数的PowerShell脚本cmdlet .

它通过$PSCmdlet.ShouldProcess()在执行更改之前调用方法来完成此操作.
这按预期工作.

我遇到的问题是我的Cmdlet是通过调用其他Cmdlet实现的,并且-WhatIf或者-Confirm参数不会传递给我调用的Cmdlet.

我怎么能沿的值传递-WhatIf-Confirm我从我的Cmdlet的调用的cmdlet?

例如,如果我的Cmdlet是Stop-CompanyXyzServices,它用于Stop-Service实现其操作.

如果-WhatIf传递给Stop-CompanyXyzServices我,我希望它也传递给Stop-Service.

这可能吗?

powershell cmdlets powershell-2.0

22
推荐指数
2
解决办法
2万
查看次数

如何传播-详细到模块功能?

根据回答这样一个和我自己的经验,PowerShell的可以照顾传播-Verbose(和-Debug)自动,这是非常方便的。但是,当我要传播冗长的功能在模块中时,此操作停止。用于测试此代码:

在名为Modc:的目录中创建一个目录,并添加2个文件:

档案c:\Mod\Functions.ps1

function Show-VerbosityB { [cmdletbinding()]Param()
  Write-Output "Show-VerbosityB called"
  Write-Verbose "Show-VerbosityB is Verbose"
}
Run Code Online (Sandbox Code Playgroud)

档案c:\Mod\Mod.psd1

@{
ModuleVersion = '1.0.0.0'
NestedModules = @('Functions.ps1')
FunctionsToExport = @('*-*')
}
Run Code Online (Sandbox Code Playgroud)

现在创建主脚本,说c:\Foo.ps1

Import-Module c:\Mod

function Show-VerbosityA { [cmdletbinding()]Param()
  Write-Output "Show-VerbosityA called"
  Write-Verbose "Show-VerbosityA is Verbose"
}

function Show-Verbosity { [cmdletbinding()]Param()
  Write-Output "Show-Verbosity called"
  Write-Verbose "Show-Verbosity is Verbose"
  Write-Output "Testing propagation"
  Show-VerbosityA
  Show-VerbosityB
}

Show-Verbosity -Verbose
Run Code Online (Sandbox Code Playgroud)

结果是

PS> . C:\Foo.ps1
Show-Verbosity called
VERBOSE: Show-Verbosity is …
Run Code Online (Sandbox Code Playgroud)

powershell cmdlets

3
推荐指数
1
解决办法
932
查看次数

标签 统计

cmdlets ×2

powershell ×2

powershell-2.0 ×1