If the Mandatory parameter is not specified, the request does not interpret it as a string

I have a required parameter, for example:

[Parameter(Mandatory=$true)]$foo

I want PowerShell to request it if the user hasn’t. However, I would like to be able to supply a variable; that is, if $barprovided, it does not bind it like that $foo = "$bar".

+4
source share
1 answer

You can use a function ExpandStringto expand this variable, for example:

function Test-Param
{
    Param(
        [Parameter(Mandatory=$true)]$foo
    )

    $foo = $ExecutionContext.InvokeCommand.ExpandString($foo)

    Write-Host $foo   
}

You can test it using:

$bar = "hello"
Test-Param

Now just pass $barin the invitation and you will receive hello.

+4
source

Source: https://habr.com/ru/post/1657094/


All Articles