0

 

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

2 Answers 2

1

Is the Invoke-Command cmdlet required? I believe that command is intended for remoting (executing commands on remote machines).

Looking at the PowerShell.exe syntax, I can't see any reference to an ArgumentList parameter:

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

There is an args parameter but I couldn't get it to work. I'm not actually sure whether its purpose is to pass parameter values to the PowerShell command or not. The way I would go about this is...

1) Create a ps1 file:

param (
    $Path
)
Write-Host $Path

2) Launch the ps1 script using the below batch file:

powershell -file .\test.ps1 -path 'C:\Test'

Hope this is a compatible solution to your scenario.

1
  • A separate script file is an option I have considered, but it would be a last resort as I don't want to introduce intermediate scripts in each project that may be effected. I will however review my use of -Command and the associated -args parameter to the scriptblock. Thanks for taking the time to reply.
    – Paul Eden
    Commented Feb 13, 2018 at 17:17
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.