1
votes

I have a PowerShell script that contains this at the top:

Param(
    # [snip]
    [string] [Parameter(Mandatory=$true)] $Server
)

In my VSTS Release definition, I added an Azure Powershell task calling the script, and passed the argument as:

-Server '$(ServerName)' [snip]

However, when I trigger a new Release, at the step for this script, I get this error:

##[error]A parameter cannot be found that matches parameter name 'Server'.

I verified in the log output that the server name is passed properly. I even copy/pasted the command logged, and after fixing paths, it ran locally with no problems.

Why is this happening, and how can I fix it?

2
It sounds like there was an earlier definition of your script/function loaded. - Matt
@Matt Can you elaborate on that? If that was the case, shouldn't that mean that changing the parameter name would not affect anything? - Scott Weldon
Yeah I know. Its a series of timing errors I am suggesting that I cannot reproduce as I don't have the environment. It would be like you made v1 of the script and it did not have that name then you "published" it. Next you update the script to make it better but neglected to "publish" it. Then testing would fail as you would assume a version is there that is not present. Then, finally, you test again with v3 of the script and "publish" with testing going smoothly. That would have the appearance of changing the name being what fixed it. Suggesting that correlation does not equal causation. - Matt
@Matt Ah okay. I don't think that's what happened in this case, since there is no manual publish in the process, but the script is automatically published as a build artifact. Thanks for the suggestion though! - Scott Weldon

2 Answers

1
votes

Changing the name of the argument fixed the issue. My script now contains:

Param(
    # [snip]
    [string] [Parameter(Mandatory=$true)] $ServerName
)

And I pass the arguments as:

-ServerName '$(ServerName)' [snip]

As for the why, I can only speculate. I checked the source code but couldn't find anything obvious. My only guess is that the Azure PowerShell task overwrote $Server for some reason (though I'm not sure why the log output would show the correct argument in that case).

0
votes

Your $Server variable is already declared. I give you some trick when you want to test code or something like this. First remove variable and clear screen like this example:

cls
Remove-Variable server
function ssssss 
{
    param
    (
        [string][parameter(Mandatory = $true)]$server
    )
    Write-Host $server
}
ssssss -server "Enter servername here"

It's working fine.