Search code examples
powershell-3.0

Argumentlist failing to populate param in scriptblock


 

This is not a post about Visual studio, as I can replicate the issue on the powershell commandline

I am trying to launch a powershell script as part of a postbuild event in Visual Studio 2015.  For completeness, my postbuild event code is:

powershell -executionPolicy bypass -Command  “& {Invoke-command -scriptblock {Param($path); write-host $path} -ArgumentList '$(ProjectDir)'}"

 

This is then converted into a batch file by VS as this:

powershell -executionPolicy bypass -Command  “& {Invoke-command -scriptblock {Param($path); write-host $path} -ArgumentList 'd:\somepath\somefile.msi'}"

 

 

Its purpose is to counter the fact that developers have data on different drives to each other so I don’t know where the script will ultimately live.  I will be using the volume information at the start of the path string but, for now, getting it to echo the path will be a win..

 

If I take the Invoke-Command and just run that, all works as expected:

PS D:\> Invoke-command -scriptblock {Param($path); write-host $path} -ArgumentList 'd:\somepath\somefile.msi'

d:\somepath\somefile.msi 

 

If I wrap that into a –command parameter to be passed into powershell, the argumentlist never populates the parameter and I get a null $path string in the scriptblock.

PS D:\ > powershell -Command “& {Invoke-command -scriptblock {Param($path); write-host $path} -ArgumentList 'd:\somepath\somefile.msi'}"

             

 

Any suggestions as to where I’m going wrong?

 

Many thanks


Solution

  • After reviewing my -Command usage as suggested by @Michaeljames it became apparent that I had possibly mixed 2 different aproaches.

    Correcting my code as follows had the desired effect:

    PS H:\> powershell -Command {param([string]$path) write-host "$($path.substring(0,1)):\Scripts\somescript.ps1"} -Args 'd:\somepath\somefile.msi'
    
    D:\Scripts\somescript.ps1