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
-
Have entrourage and want to use Mail. Can't get it to work.
I had a macbook and purchased the Microsoft office for Mac which included Entrourage.
I have now purchased a new macbook Pro and want to begin to use Mail rather than Entrourage.
I have Entrourage on my new computer but i can't get it to transfer my information to the the Mail program
I am on Charter.net at my office and sbcglobal.net at home.
How can i get this to work. I can receive on Mail, but i can't send.check your outgoing port settings and make sure it's using a specific port and not the default port, also you might want to check to see if it is using SSL, TLS or any other encryption. Verify the setting from your old machine setup.
-
got my password wrong twice and my mini is disable. I connected to iTunes, but can't get it to work. Can some one guide my through?
If it's showing the red disabled screen due to incorrect passcodes then you may need to put the iPad into recovery mode : http://support.apple.com/kb/ht1808 - you should then be able to reset the iPad via your computer's iTunes and restore/resync your content to it
-
HT1151 i have a new apple external dvd drive, and i can't get it to work with my iMac?
I have a new apple dvd drive, I was told it will work with my imac, but when i plug it in ...nothing... iv been for update... still nothing. How can i get it to work???
If it is this drive, note that it is for MBA's and Mini's only:
http://store.apple.com/us/product/MC684ZM/A
Ciao. -
Is there a way to reset my messages account, I can't get iMessage to work on IPad nor Mac osx. Help please this is really frustrating.
Hi,
Is there an iPhone involved ?
If not, what happens if you remove the Apple ID on the iPad and then set it in Airplane Mode (allow a couple of minutes at this point) followed by re-adding the Apple ID ?
What, if any error messages are appearing on the Mac when you try to launch the App or Login to the iMessages server ?
Is the Apple ID linked to an iCloud account ?
In Messages 7 (Mountain Lion) this did not seem to be important but it seems it is in Mavericks/Messages 8
9:14 pm Friday; April 11, 2014
iMac 2.5Ghz i5 2011 (Mavericks 10.9)
G4/1GhzDual MDD (Leopard 10.5.8)
MacBookPro 2Gb (Snow Leopard 10.6.8)
Mac OS X (10.6.8),
Couple of iPhones and an iPad -
I bought an IR receiver and an Apple Remote, and I can't get it to work. What am I doing wrong? The receiver is a Lenovo eHome OVU430006/01 USB IR Receiver.
Hello,
You would need to pair the Apple Remote with the receiver. So I would use the generic Mac/PC receiver and follow these instructions:
http://support.apple.com/kb/HT1619
If that doesn't work then check in System Profiler to see if the IR receiver has been recognized by the PowerBook's hardware. You may require the install of drivers for this device to be recognized.
Best of luck -
I have a go daddy email account and I can't get it to work on my iPad
I have a go daddy email account and I can't get it to work on my iPad
Try turning the email account off and then turn it on again. Settings>Mail, Contacts, Calendars>select your gmail account>Off. Go to the mail app and then come back to the account in the settings and turn it on again.
If you can't find any other way for the iPad to accept the password, delete the email account and then add it back onto the iPad. -
I have a Mac Mini and I can't get it to work with my Ricoh Aficio MP 161. Any ideas?
I have a Mac Mini and I can't get it to work with my Ricoh Aficio MP 161. Any ideas?
The latest Apple driver support begins with the MP 171.
Here is Ricoh's official driver download page specifically for the Afico MP 161. Note that new drivers for this device were released on Aug 2, and include Lion and Mountain Lion.
Hopefully, this will solve your device issues. -
Iphone 5 went black. Tried pushing on/off and circle at the same time. Worked yesterday and then did it again today. Can't get it to work.
You need to hold those button longer until you see the apple logo, then release, then wait for the phone to boot back up.
-
I just bought a new computer and had files transferred along with itunes to the new computer. I can't get it to work. It doesn't recognize the extensions for itunes. I also tried downloading Quicktime and couldn't get it to download.
iOS: Device not recognized in iTunes for Windows
I would start with:
Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7 -
I bought a DATAPILOT to transfer pictures to my macbook and can't get it to work. Any suggestions please?
Best if you try and explain what you are trying to acheive.
Iphoto is a different paradigm than you're used to. I don't understand why you are trying to transfer your edited photos back to an external disk.
There are tutorials here
http://www.apple.com/findouthow/photos/
Regards
TD -
I have signed up for adobe conversion to microsoft word and i can't get it to work - can you help me
I have signed up for adobe conversion to microsoft word and I can not get it to work - can you help me?
Hi Mike,
I've checked your account. I see that you just purchased the subscription this morning. The order is still pending processing, which is why you haven't yet been able to log in an use your subscription. It can take 24-48 hours for a subscription to process fully. Once it does, you'll be able to log in and convert files.
I apologize for the inconvenience.
Best,
Sara -
Trying to connect 2010 Mac mini to alba hd tv but can't get it to work, any theories?
Trying to connect 2010 Mac mini to alba hd tv but can't get it to work, any theories?
Welcome to the Apple Community.
The iPad 1 is only able to do basic AirPlay, many providers of programming have blocked their apps from working with AirPlay. -
I just bought Quick time Pro and I can't get it to install. It shows a yellow box in my application folder, but I can't get it to work. Help!
you launch QT then go to its REGISTRATION
-
Downloaded the App Sales Call Manager. I can't get it to work on either my Iphone 4s or my Ipad2? Can anybody advise how to fix or even get back my money that i paid for it?
Contact the app developer for support. A link to their website will be in the apps description in the AppStore.
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
-
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
-
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
-
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...