Problem with Powershell variable substitution in Strings

69 Views Asked by At

I have the following Powershell function, which prepares a query I want to use for a request against an API.

function getUserID ([String]$loginName, [String] $firstName, [String] $lastName, [String] $employeeId)
{
    $queryText = "loginid:$loginName vorname:$firstName nachname:$lastname personalnummer:$employeeId"
    $requestData = (@{
        "query" = "$queryText"
    } | ConvertTo-Json)
    return $requestData
}

If I call it like this:

$test = getUserID("blibla", "Bli", "Bla", "34654")
$test

I would expect the following output:

{
    "query": "loginid:blibla vorname:Bli nachname:Bla personalnummer:34654"
}

But the actual output looks like this:

{
    "query": "loginid:blibla Bli Bla 34654 vorname: nachname: personalnummer:"
}

I don't understand why the variable substitution seems to break after the first substitution. Or at least it doesn't behave the way, I would expect it to.

Any help is appreciated!

Solution: wrong function call. It should be:

getUserID "blibla" "Bli" "Bla" "34654"
0

There are 0 best solutions below