I'm having an issue here while creating a function with multiple parameters in PowerShell. I have 4 parameters, and their value can and will either be passed via pipeline or via direct input in a PowerShell window.
I can't seem to figure out how to make sure the 4 parameters can be used in the process section.
Here's what I have (the Write-Host section is only for testing, of course):
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"
}
If I use this, it works fine with Pipeline input, but not with direct command line input, i.e.:
Set-NewDevEnd -VMType "SQL","IIS" -StaticIp "192.168.0.1","192.168.0.2" -VMName "Testing1","Testing2" -VMDNSName "Testing1","Testing2"
I'm getting
VMType is SQL IIS ; StaticIP is 192.168.0.1 192.168.0.2 ; VMName is Testing1 Testing2 ; and VMDNSName is Testing1 Testing2
I know that usually, to support both direct input and pipeline input, you need to add a foreach loop in the process section, but how would I go about using a foreach loop with multiple parameters?