简体   繁体   English

如何轻松在cmdlet之间传递所有参数?

[英]How do I pass all parameters between cmdlets easily?

Using CmdletBinding, is there an easy way to regurgitate the exact parameters that a cmdlet was called with, so I can call another cmdlet with the exact same parameters? 使用CmdletBinding,是否有一种简单的方法来重新定义调用cmdlet的确切参数,因此我可以使用完全相同的参数来调用另一个cmdlet?

I'm writing Powershell Cmdlets in Powershell. 我在Powershell中编写Powershell Cmdlet。 I'm using advanced functions. 我正在使用高级功能。 I have a cmdlet called Get-Environment, with several optional parameters like [string]EnvironmentName and [switch]Active. 我有一个名为Get-Environment的cmdlet,其中包含几个可选参数,例如[string] EnvironmentName和[switch] Active。 I have another cmdlet, called Get-Machine, with all of the same optional parameters; 我还有另一个名为Get-Machine的cmdlet,具有所有相同的可选参数。 it calls Get-Environment. 它称为Get-Environment。 Originally, before I added the [switch]Active parameter, I simply called Get-Environment with all variables explicitly (see below). 最初,在添加[switch] Active参数之前,我只是简单地调用了Get-Environment,并显式地使用了所有变量(请参见下文)。

I can't do the same thing now, because if I add "active" then it will be set. 我现在不能做同样的事情,因为如果我添加“ active”,那么它将被设置。 I don't want to have make a test in Get-Machine to see if Active is true and have two different versions of the Get-Environment call. 我不想在Get-Machine中进行测试以查看Active是否为true并具有两个不同版本的Get-Environment调用。 I'd prefer to not have to trawl through the $PSBoundParameters hashtable and reconstruct the original strings, but that looks like the only feasible way forward (unless I'm missing something.) 我宁愿不必遍历$ PSBoundParameters哈希表并重建原始字符串,但这看起来是唯一可行的方法(除非我遗漏了一些东西。)

Original code inside get-machine: get-machine内的原始代码:

$environments = get-Environment -EnvironmentName $EnvironmentName

Oh for Pete's sake. 噢,看在皮特的份上。 I found it. 我找到了。 I was missing the big stupid easy thing. 我错过了愚蠢的大事。 I'll leave this up for others, and in case someone has an even better answer. 我会把这个留给别人,以防有人得到更好的答案。

https://ss64.com/ps/psboundparameters.html https://ss64.com/ps/psboundparameters.html

$PSBoundParameters can be used to call a subordinate function or cmdlet passing the same parameters - PowerShell will automatically splat the hash table's values instead of having to type each of the parameters: get-otherthing @PSBoundParameters $ PSBoundParameters可用于调用传递相同参数的下级函数或cmdlet-PowerShell将自动生成哈希表的值,而不必键入每个参数:get-otherthing @PSBoundParameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在管道上的cmdlet之间传递常用的Powershell命令行参数? - How do I pass common powershell command line parameters between cmdlets on the pipeline? 如何使用相同的参数动态调用 2 个不同的 cmdlet? (不是动态参数。) - How do I dynamically call 2 different cmdlets with the same parameters? (Not dynamic parameters.) 如何通过 PowerShell Cmdlet 从 schtasks.exe 获取所有可用信息? - How do I get all the information available from schtasks.exe via PowerShell Cmdlets? 如何通过Powershell cmdlet更新Azure角色中的设置? - How do I update a setting in an Azure Role via the powershell cmdlets? 如何在给定模块中查找cmdlet(从文件加载) - How do I find the cmdlets in a given module (loaded from file) 如何使用Powershell cmdlet处理logman结果? - How do I work with logman results using powershell cmdlets? 如何正确传递参数? - How do I pass parameters properly? 如何在调用其他 Cmdlet 的 Cmdlet 中支持 PowerShell 的 -WhatIf 和 -Confirm 参数? - How do you support PowerShell's -WhatIf & -Confirm parameters in a Cmdlet that calls other Cmdlets? 如何在模块中的cmdlet之间共享数据? - How to share data between cmdlets in a module? 如何在powershell中列出所有已安装、可运行的cmdlet? - How to list all installed, runnable cmdlets in powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM