Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-Command error "Parameter set cannot be resolved using the specified named parameters"

Tags:

Hope you can help me with a problem trying to execute a script block with alternate credentials on a local computer. I've been thoroughly searching on forums and doing some googling and found two possible approach to solve my problem:

  1. Use Invoke-Command
  2. Use Start-Job

Using approach #1 I had this code:

$res = Invoke-Command -Credential $migratorCreds -ScriptBlock {param($one, $two) Get-LocalUsers -parentNodeXML $one -migratorUser $two } -ArgumentList $xmlPRE,$migratorCreds 

where Get-LocalUsers is a custom function stored in a custom module (*.psm1).

My problem is that every time I run this code I get following error:

Parameter set cannot be resolved using the specified named parameters

So it's obvious that I must be missing something, could you help me on this topic?

Thanks in advance...

like image 717
SantiFdezMunoz Avatar asked Aug 09 '13 09:08

SantiFdezMunoz


People also ask

How do you declare a parameter in PowerShell?

Long description. The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.

What is invoke expression in PowerShell?

Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.

How does invoke command work?

The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.


2 Answers

The error you have is because -credential without -computername can't exist.

You can try this way:

Invoke-Command -Credential $migratorCreds  -ScriptBlock ${function:Get-LocalUsers} -ArgumentList $xmlPRE,$migratorCreds -computername YOURCOMPUTERNAME 
like image 133
CB. Avatar answered Oct 08 '22 14:10

CB.


The accepted answer is correct regarding the Invoke-Command cmdlet, but more broadly speaking, cmdlets can have parameter sets where groups of input parameters are defined, such that you can't use two parameters that aren't members of the same parameter set.

If you're running into this error with any other cmdlet, look up its Microsoft documentation, and see if the the top of the page has distinct sets of parameters listed. For example, the documentation for Set-AzureDeployment defines three sets at the top of the page.

like image 34
BobbyA Avatar answered Oct 08 '22 13:10

BobbyA