Thursday, April 21, 2011

Misleading error message in PowerShell script: "Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property"

I recently ran into the following error message while modifying a PowerShell script:

Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property

The cause of the error was that I had added a line of code in front of my param declaration:

Set-StrictMode -Version 2

param(
    [string][parameter(mandatory=$true )] $WebsitePhysicalPath,

5 comments:

  1. this can occur if you have a variable declared before the "param()" declarations.

    ReplyDelete
  2. This one of those things that makes me want to TP the house of the developer in charge of issuing those non-sensical error messages. I just lost time that I could have spent on better things because of such lack of clarity. Thanks for posting this.

    ReplyDelete
  3. Thanks for the post. This was exactly my problem; I was trying to add Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 above my "Param()"

    ReplyDelete
  4. #Powershell BUG: Cannot use Param if you have function inside your script
    Function a ([string]$var){

    }


    param([string]$foo = "foo")
    Write-Host "Arg: $foo"
    Write-Host "Arg: $bar"

    Comment out the function declaration and it works!

    ---------------------------------
    found answer to my own problem.

    #workAround: Place the param( Statement for the Main at the TOP of the script, before the Function declaration.

    ReplyDelete
  5. Hi is this still an active conversation?

    ReplyDelete