0

I have the following function.

function Params {
    param (
        [Parameter(Mandatory = $true)]
        [Alias('Param1')]
        [AllowNull()]
        ${Param1[default value]}
    )
    [ref] $Param1 = 
        if (${Param1[default value]}) {
            ${Param1[default value]}
        } else {
            'default'
        }
}

Params

$input1 = $null

"the param input is $([ref]$input1)"

If i input something for parameter on prompt or if i leave it to default value, i get this as output for $([ref]$input)

the param input is System.Management.Automation.PSReference`1[System.Management.Automation.LanguagePrimitives+Null]

Why am i not getting a value instead?

I want this output for example:

the param input is default

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • the word `$Input` is a reserved word and should NOT be used for anything other than _reading from it_. – Lee_Dailey May 06 '19 at 18:50
  • @Lee_Dailey Thanks for pointing that out; ok, i used a different word. i am still getting same error – Cataster May 06 '19 at 18:50
  • this line `${Param1[default value]}` creates a variable named `Param1[default value]` - which makes no sense. [*grin*] ///// what is your goal? this code is ... strange and nearly meaningless to me ... [*blush*] – Lee_Dailey May 06 '19 at 18:53
  • @Lee_Dailey the function i used is similar to this: https://stackoverflow.com/a/39880424/8397835 – Cataster May 06 '19 at 18:55
  • @Lee_Dailey essentially im just trying to build on that function by having a nice output of whatever value the Param1 is set to. i looked up and it said you eithe rhave to call the function in the scope or use reference as i did here. using ref is much more like it in my case so i went with it – Cataster May 06 '19 at 18:57
  • 1
    the answer by BartekB that you have ... mangled ... is bizarre. it _works_ but does so quite strangely. if you really want to use a convoluted, obscure, not-QUITE-insane bit of code, then stick with what he showed. if you want reasonable, fairly direct, far-from-insane code ... use the accepted answer. [*grin*] – Lee_Dailey May 06 '19 at 19:11
  • @Lee_Dailey unfortunately i would have to define that at the beginning of script otherwise i would get Unexpected attribute 'CmdletBinding'. – Cataster May 06 '19 at 19:24
  • that is a SHORT snippet - not an entire function. [*grin*] simply wrap it in the normal function definition to make it work in your script. – Lee_Dailey May 06 '19 at 19:45
  • @Lee_Dailey but then i would still need to use Reference to accomplish what i am trying to do...are you saying the accepted answer will work with ref? in any case, i defined it at the beginning without function and the cmdletbinding works just fine, so i guess its suiting my purposes already – Cataster May 06 '19 at 19:50
  • don't use `[ref]` unless you have a NEED for it. i can't see any need - and don't see any way to make it work with the code provided. – Lee_Dailey May 06 '19 at 19:52
  • @Lee_Dailey the need would be lets say if i used param in a function, and lets say i wanna pass that param variable later in the script to an output sentence, like my post – Cataster May 06 '19 at 19:53
  • i am sorry, but i can't think of any reason to do it that way. [*blush*] it seems like a waste of time and effort to achieve something that can be done directly instead of in a convoluted, obscure, difficult-to-understand method. – Lee_Dailey May 06 '19 at 20:06
  • @Lee_Dailey its cool, i found a way around it :). thanks for your suggestions so far! – Cataster May 06 '19 at 20:08
  • neato! please, would you post the solution? i am oh-so-curious ... [*grin*] – Lee_Dailey May 06 '19 at 20:35
  • @Lee_Dailey i posted an answer :) would appreciate an upvote ^_^ – Cataster May 06 '19 at 20:54
  • 1
    thank you for posting it! [*grin*] ///// i upvoted it - not because it answered your original question, but because it answered your _intent_ that drove you to the original question. [*grin*] – Lee_Dailey May 06 '19 at 22:13
  • @Lee_Dailey thank you for your guidance – Cataster May 06 '19 at 22:21
  • 1
    you are most welcome! glad to have helped a little bit ... [*grin*] – Lee_Dailey May 06 '19 at 22:22

2 Answers2

1

I ended up resorting to a different method to achieve what i want:

Defining this at the top of script:

[CmdletBinding()]
Param(
    $Param1 = (Read-Host -prompt "Param1")
)
if (!$Param1) { "default" }

"the param input is $Param1"
Cataster
  • 3,081
  • 5
  • 32
  • 79
1

The [ref] type accelerator (it's not a type accelerator in the usual sense, but it does create PSReference objects, so... it sort of is) gets you, as it tells you, a PSReference object.

In order to retrieve the value from it, you'd need to ask for it specifically. In your code, you can access it by pulling the Value property from the created reference object.

"the param input is $(([ref]$input1).Value)"

However, given that $input1 isn't assigned to, you might have to refactor a bit to get what you're after.

Rain
  • 153
  • 5