I can't get ParameterSets to work

I'm new to PowerShell, and I'm having trouble with parameter sets. I've created a function that will retrieve memory info from a computer. I have the function working properly, except for the parameters sets. Here's the syntax I want to support:
#Run without parameters for memory usage on localhost:
Get-MemoryUsage
#Get memory usage on remote system:
Get-MemoryUsage [-ComputerName] <String>
#Examples:
# Get-MemoryUsage -ComputerName mydc01
# Get-MemoryUsage mydc01
# "mydc01", "mydc02" | Get-MemoryUsage
#Get memory usage on remote system running on Hyper-V host, where "name" is the name of the virtual machine in Hyper-V manager:
Get-MemoryUsage [-ComputerName] <String> [-HostName] <String> [-Name] <String>
#Examples:
# Get-MemoryUsage -ComputerName mydc01 -HostName hvsvr -Name TheDC
# Get-MemoryUsage mydc01 hvsvr TheDC
#Get memory usage on remote system running on Hyper-V host, where "ID" is the GUID of the virtual machine:
Get-MemoryUsage [-ComputerName] <String> [-HostName] <String> [-ID] <GUID>
#Examples:
# Get-MemoryUsage -ComputerName mydc01 -HostName hvsvr -ID 837b8cbc-b89c-4070-bf65-4b674bb380e4
# Get-MemoryUsage mydc01 hvsvr [GUID]837b8cbc-b89c-4070-bf65-4b674bb380e4
#Lastly, I want to be able to pipeline in a [Microsoft.HyperV.PowerShell.VirtualMachine] object like so:
Get-VM -ComputerName hvsvr -Name TheDC | Get-MemoryUsage -Computername mydc01
$vm = Get-VM -Name TheDC ; Get-MemoryUsage mydc01 $vm
To summarize syntax:
You must always specify the computer name, unless you want to target the local computer
If targeting a Hyper-V virtual machine, you must also specify one of the following:
1. The Hyper-V server and virtual machine name
2. The Hyper-V server and virtual machine GUID
3. A [Microsoft.HyperV.PowerShell.VirtualMachine] object
Here's the function declaration:
function Get-MemoryUsage {
[CmdletBinding()]
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="notVM", HelpMessage='Enter the computer name')]
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="byName", HelpMessage='Enter the computer name of the guest virtual machine')]
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="byID", HelpMessage='Enter the computer name of the guest virtual machine')]
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="byObj", HelpMessage='Enter the computer name of the guest virtual machine')]
[Alias('GuestName','GuestComputerName')] [String[]] $ComputerName=$env:COMPUTERNAME,
[Parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="byName", HelpMessage='Enter the computer name of the Hyper-V host')]
[Parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="byID", HelpMessage='Enter the computer name of the Hyper-V host')]
[Alias('host','server')] [String[]] $HostName,
[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="byObj")]
[Alias('VirtualMachine')] [Microsoft.HyperV.PowerShell.VirtualMachine[]] $VM,
[Parameter(Position=2, Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="byName", HelpMessage='Enter the name of the virtual machine in Hyper-V')]
[Alias('VirtualMachineName','Guest')][String[]] $Name,
[Parameter(Position=2, Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="byID", HelpMessage='Enter the GUID of the virtual machine')]
[Alias('VirtualMachineId','vmid')][GUID[]] $ID
process {
Write-Host $PsCmdlet.ParameterSetName
It seems to work as expected except for this example (sometimes the user will pipe in both Name and Id, and I cannot stop them):
New-Object –Typename PSObject –Prop @{'HostName'="hvsvr";'GuestComputerName'="mydc01";'VirtualMachineId'=[GUID]"837b8cbc-b89c-4070-bf65-4b674bb380e4";'VirtualMachineName'="TheDC"} | Get-MemoryUsage
#Error:
Get-MemoryUsage : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:200
+ ... TheDC} | Get-MemoryUsage
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{VirtualMachin...m3 server 2012}:PSObject) [Get-MemoryUsage], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Get-MemoryUsage
#If I remove either the 'VirtualMachineName' or 'VirtualMachineId' property, it works. Or if I remove the [GUID] cast, it works.
How can I fix this?
This whole declaration seems sloppy to me.  Is there a better way?  Suggestions are welcome.
-Tony

Take a real close look at what you have posted:
function Get-MemoryUsage {
Param (
[Parameter(
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="notVM",
HelpMessage='Enter the computer name'
[Parameter(Position=0,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byName",
HelpMessage='Enter the computer name of the guest virtual machine'
[Parameter(Position=0,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byID",
HelpMessage='Enter the computer name of the guest virtual machine'
[Parameter(
Position=0,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byObj",
HelpMessage='Enter the computer name of the guest virtual machine'
[Alias('GuestName','GuestComputerName')][String[]]$ComputerName=$env:COMPUTERNAME,
[Parameter(Position=1, Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byName",
HelpMessage='Enter the computer name of the Hyper-V host'
[Parameter(Position=1,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byID",
HelpMessage='Enter the computer name of the Hyper-V host'
[Alias('host','server')][String[]] $HostName,
[Parameter(
Position=1,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byObj"
[Alias('VirtualMachine')][Microsoft.HyperV.PowerShell.VirtualMachine[]]$VM,
[Parameter(
Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byName",
HelpMessage='Enter the name of the virtual machine in Hyper-V'
[Alias('VirtualMachineName','Guest')][String[]] $Name,
[Parameter(Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byID",
HelpMessage='Enter the GUID of the virtual machine'
[Alias('VirtualMachineId','vmid')][GUID[]] $ID
process {
Write-Host $PsCmdlet.ParameterSetName
It makes no sense.  YOu need to figure out why you have so many 'Parameter' statements.
When laying out parameters use this method as it iseasier to spot errors:
The order also helps to avoid errors and make incremental design easier:
[Alias('VirtualMachine')]
[Alias('VMach')]
[ .... validation rules ...]
[Parameter(
Position=1,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="byObj"
)][Microsoft.HyperV.PowerShell.VirtualMachine[]]$VM,
¯\_(ツ)_/¯

Similar Messages

Maybe you are looking for

  • Form takes up to 2 minutes to open - why??

    I'm a fairly new user to LiveCycle. I created a performance appraisal review form in LiveCycle v9.0, yet it takes almost 2 minutes to open. The files size is showing only 333KB. Other forms I've created do not take very long at all to pop open. I hav

  • ITSmobile setup

    Hi Everyone, I am in the midst of setting up ITSmobile technology to enable end-users to use mobile devices with barcoding enabled to execute RF transactions in SAP systems. We are running SAP 4.7 with SAP Kernel 6.40 and SAP Basis Component 6.20. I

  • App store tells me that 8 apps haven't been downloaded. Any ideas or suggestions on how to check?

    App store tells me that 8 apps haven't been downloaded. Any ideas or suggestions

  • Merging Companies - How to handle employees.

    Hello everybody. I'm currently undergoing a project, in which six companies will be merged into one, only, in SAP. My doubt is mainly in the Employees: what type of special attention should i have in terms of the system, regarding this subject (besid

  • ITunes Enable/Disable

    I got a new lap top. I want this computer to be my main iTunes one. I sold my old lap top & arent sure that I disabled iTunes on it. I looked in account but cant see how to make this computer the main iTunes...