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:
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...
This question is related to
powershell
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
I was solving same problem recently. I was designing a write cmdlet for my Subtitle module. I had six different user stories:
I end up in the big frustration because I though that 4 parameters will be enough. Like most of the times, the frustration was pointless because it was my fault. I didn't know enough about parameter sets.
After some research in documentation, I realized where is the problem. With knowledge how the parameter sets should be used, I developed a general and simple approach how to solve this problem. A pencil and a sheet of paper is required but a spreadsheet editor is better:
Example:
The practical example could be seen over here.
BTW: The parameter uniqueness within parameter sets is the reason why the ParameterSetName
property doesn't support [String[]]
. It doesn't really make any sense.
Fairly new to using PowerShell, think I might be able to help. Could you try this?
I believe you're not getting the correct parameters to your script block:
param([string]$one, [string]$two)
$res = Invoke-Command -Credential $migratorCreds -ScriptBlock {Get-LocalUsers -parentNodeXML $args[0] -migratorUser $args[1] } -ArgumentList $xmlPRE, $migratorCreds
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.
Source: Stackoverflow.com