【发布时间】:2018-06-01 07:23:11
【问题描述】:
我在 PowerShell 中创建具有多个参数的函数时遇到问题。我有 4 个参数,它们的值可以并且将通过管道或通过 PowerShell 窗口中的直接输入传递。
我似乎无法弄清楚如何确保这 4 个参数可以在流程部分使用。
这是我所拥有的(Write-Host 部分当然仅用于测试):
function Set-NewDevEnv {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValuefromPipelineByPropertyName=$true)]
[ValidateSet('IIS', 'SQL', 'SOLR', 'Vanilla')]
[string[]]$VMType,
[Parameter(Mandatory=$true, ValuefromPipelineByPropertyName=$true)]
[string[]]$StaticIP,
[Parameter(Mandatory=$true, ValuefromPipelineByPropertyName=$true)]
[string[]]$VMName,
[Parameter(Mandatory=$true, ValuefromPipelineByPropertyName=$true)]
[string[]]$VMDNSName
)
Begin {}
Process {
Write-Host "VMType is $VMType ; StaticIP is $StaticIP ; VMName is $VMName; and VMDNSName is $VMDNSName"
}
如果我使用它,它适用于管道输入,但不适用于直接命令行输入,即:
Set-NewDevEnd -VMType "SQL","IIS" -StaticIp "192.168.0.1","192.168.0.2" -VMName "Testing1","Testing2" -VMDNSName "Testing1","Testing2"
我来了
VMType 是 SQL IIS ;静态IP为 192.168.0.1 192.168.0.2 ; VMName 是 Testing1 Testing2 ;并且 VMDNSName 是 Testing1 Testing2我知道通常情况下,要同时支持直接输入和管道输入,您需要在process 部分添加一个foreach 循环,但是我将如何使用带有多个参数的foreach 循环呢?
【问题讨论】:
标签: function powershell parameter-passing pipeline