Complex strings as positional parameters

This topic contains 2 replies, has 2 voices, and was last updated by  Richard Schaefer 1 year, 6 months ago.

  • Author
  • #34855

    Richard Schaefer

    I'm passing in several file paths as positional parameters to a script like this:

    powershell -nologo -file .\Create-AnalysisProps.ps1 "14" "2010=10;2012=11;2013=12;2015=14" 
        "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Team Tools\Static Analysis Tools\FXCop\FXCopCmd.exe" 
        "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Team Tools\Static Analysis Tools\Rule Sets\" 
        "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Team Tools\Static Analysis Tools\FxCop\Metrics.exe" 
        "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Common7\IDE\mstest.exe" 

    I'm getting the following error:

    .ps1 : A positional parameter cannot be found that accepts argument 'Studio'.

    Here's the Param section of my script:

    	# VS Version
    	# Version Table
    	# FXCopCmdTemplate
    	[String]$FXCopCmdTemplate="C:\VS ##\Test Tools\FXcopcmd.exe",
    	# FXCopRulesTemplate
    	[String]$FXCopRulesTemplate="C:\VS ##\Rule Sets\",
    	# MSMetricsTemplate
    	[String]$MSMetricsTemplate="C:\VS ##\Test Tools\Metrics.exe",
    	# VSTest Template
    	[String]$VSTestTemplate="C:\VS ##\Test Tools\MSTest.exe",
    	# Output XML File Location

    All my strings are wrapped in quotes. Any idea why this would behave this way? I have other scripts that accept file paths as strings with no problems...

  • #34856

    Dave Wyatt

    I'm not sure what's doing this, but that trailing backslash before the closing quotation mark in "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Team Tools\Static Analysis Tools\Rule Sets\" is somehow escaping the closing quote and screwing up all the rest of the input. The PowerShell engine doesn't do this; it must be something going on with the console subsystem, or maybe it's something powershell.exe does when you're passing in arguments (so quotes can be escaped from cmd.exe / etc.)

    If you're doing this from inside a powershell session already, there's no reason to make another call to powershell.exe. Just do this instead, and the problem goes away:

    .\Create-AnalysisProps.ps1 "14" # The rest of your really long command line

    If you do need to be calling powershell.exe, then doubling up that trailing backslash works:

    "d:\Program Files (x86)\Microsoft Visual Studio ##.0\Team Tools\Static Analysis Tools\Rule Sets\\"

  • #34857

    Richard Schaefer

    I do need to call PowerShell from Cmd.exe, it's part of a bigger .cmd file where those values actually come from previous steps as %var1% cmd variables. The pre-substitution command looks like:

    powershell -file .\Create-AnalysisProps.ps1 "%ver%" "%vertable%" "%fxpath%" etc

    Changing the one parameter with the trailing backslash to a double-backslash did the trick. At least the parameters are getting into the script now. Of course I have issues within the script but (theoretically) I can solve those.


You must be logged in to reply to this topic.