How to define named parameter as [ref] in PowerShell

喜欢而已 提交于 2019-11-30 21:46:55

问题


I'm trying to use [ref] named parameters. However, I am getting an error:

workflow Test
{
    Param([Parameter(Mandatory=$true)][String][ref]$someString)

    write-verbose $someString -Verbose
    $someString = "this is the new string"
}

cls
$someString = "hi"
Test -someString [ref]$someString
write-host $someString

#Error: Cannot process argument transformation on parameter 'someString'. Reference type is expected in argument.

How can I fix this problem?


回答1:


I noticed that you are using a "workflow" in your example of a [ref] parameter. For simplicity, let's call it a "function" and get back to "workflow" later.

There are three things you need to change in your code:

  1. When passing a [ref] parameter to function, you need to enclose the parameter in parenthesis ().
  2. When using a [ref] parameter within a function refer to $variable.value
  3. Remove [string] type from your parameter definition. It can be a [string], or [ref], but not both.

Here is code that works:

function Test
{
    Param([Parameter(Mandatory=$true)][ref]$someString)

    write-verbose $someString.value -Verbose
    $someString.value = "this is the new string"
}
cls
$someString = "hi"
Test -someString ([ref]$someString)
write-host $someString

As for "workflows". They are very restricted, read PowerShell Workflows: Restrictions. In particular you can't invoke a method on an object within workflow. This will break the line:

$someString.value = "this is the new string"

I don't think that using [ref] parameters in a workflow is practical, because of workflow restrictions.




回答2:


I felt I needed to write this complementary very simplistic answer since this was the first google hit when searching for info on using reference parameters in Powershell functions. Although your question was not about functions but workflows:

Example using reference parameters in functions (doesn't work with workflow):

Function myFunction ([ref]$aString) {
    $aString.Value = "newValue";
}
$localVariable = "oldValue"
Write-Host $localVariable # Outputs: oldValue
myFunction ([ref]$localVariable);
Write-Host $localVariable # Outputs: newValue

With functions you can define parameter to be both a reference and another type, like this (but not with workflows):

Function myFunction ([ref][string]$aString) {
    $aString.Value = "newValue";
}
$localVariable = "oldValue"
Write-Host $localVariable # Outputs: oldValue
myFunction ([ref]$localVariable);
Write-Host $localVariable # Outputs: newValue

I agree with Jan, you should not be trying to use reference parameters in workflows because of workflow restrictions (Method invocation on objects): https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/02/powershell-workflows-restrictions/



来源:https://stackoverflow.com/questions/29596634/how-to-define-named-parameter-as-ref-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!