[英]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.