1

From a Visual STudio external tool, I would like to call a powershell script like this :

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "{ Add-PSSnapin Microsoft.SharePOint.POwerShell -EA 0; & ""C:\a very long path to my script with spaces\my-script.ps1"" -ProjectFile ""c:\another very long path\to my visual studio project file\of a \customerprojet.csproj"" -WebPartDefinition ""c:\another very long path\to my visual studio project file\of a\file-in-my-customerproject.xml"" }"

Actually, I defined my external tools to use some tokens :

-command { Add-PSSnapin Microsoft.SharePOint.POwerShell -EA 0; & ""C:\a very long path to my script with spaces\my-script.ps1"" -ProjectFile ""$(ProjectFileName)"" -WebPartDefinition ""$(ItemPath)"" }

However, this command line exceed the 260 chars limits allowed by Windows, and it does not works. It seems the command line is truncated (missing } at the end of my command.

How can I solve this?

[Edit] Actually, there was an issue with the project file. The token $(ProjectFileName) does not include the project directory, so I have to put instead:

-command { Add-PSSnapin Microsoft.SharePOint.POwerShell -EA 0; & """C:\a very long path to my script with spaces\my-script.ps1""" -ProjectFile ""$(ProjectFileName)\$(ProjectFileName)"" -WebPartDefinition ""$(ItemPath)"" }

I also have to "triple" the quotes around the ps1 path (not understanding why though)

But this does not works. The argument is too long and visual studio forbid me to put this value.

[Edit2] To be sure of the syntax, here is how I call the script successfully from the command line :

powershell -command "& { Add-PSSnapin Microsoft.SharePOint.POwerShell -EA 0; & """C:\path-to\myscript.ps1""" -ProjectFile """C:\Projects\someproject\someproject.csproj""" -WebPartDefinition """C:\Projects\someproject\somefolder\somefile.xml""" -Verbose }"

Which can be tokenized as :

powershell -command "& { Add-PSSnapin Microsoft.SharePOint.POwerShell -EA 0; & """C:\path-to\myscript.ps1""" -ProjectFile """$(ProjectFileName)\$(ProjectFileName)""" -WebPartDefinition """$(ItemPath)""" -Verbose }"

But the command line is still too long with the real paths :(

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • Looking [here](http://stackoverflow.com/questions/3205027/maximum-length-of-command-line-string) the command line accepts ~8K characters. Edit the question to add the error message you see, so we can make our own deductions – Zasz Jul 16 '14 at 12:49
  • I may have misunderstood the issue. Please see my edit. – Steve B Jul 16 '14 at 12:57
  • we use Triple quotes if we want to have a quote inside a string. You can also use grave accent quote instead of triple quote like this : `"\`"` – Zasz Jul 16 '14 at 13:05
  • Are you trying to call powershell using MSBuild xml file? If yes, [Refer here](http://www.asp.net/web-forms/tutorials/deployment/advanced-enterprise-web-deployment/running-windows-powershell-scripts-from-msbuild-project-files) to see how you can format your command string. – Zasz Jul 16 '14 at 13:09
  • No, I want to call a script from the external tools of Visual Studio. This is not part of build process, but a script that generates some code (and which is a bit long to process), and the script should scopes the current file – Steve B Jul 16 '14 at 13:12
  • I created an echo.bat external tool, which just echos the args. I sent an 875 character long argument, and the batch file was invoked properly, and echoed the whole big argument string. – Zasz Jul 16 '14 at 13:22
  • I tried this, which works, and pauses to see output : `-command "& { echo \`"Hi\`" } ; cmd /c pause | out-null"` replace the echo slowly clause by clause with your complex command, and paste error wherever it fails to run. The above line goes into the arguments text box, and "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" goes into the Command TextBox – Zasz Jul 16 '14 at 13:28
  • @Zasz: visual does not let me enter the full arguments in edit external tool dialog. The argument textbox has length restriction. Actually, when I paste my command line, it's truncated – Steve B Jul 16 '14 at 13:31
  • based on my test, only the 151 first characters are kept in the visual studio dialog* – Steve B Jul 16 '14 at 13:37
  • HAHA And in VS2013 it allows 250 chars. No solution except to create a shorter symlink to your script file. use mklink to create symlinks with shorter paths. – Zasz Jul 16 '14 at 13:45

1 Answers1

1

I was encountering a similar problem using Dr. Memory.

The Arguments field of External Tools only allows 250 characters (I needed 321).

I was able to workaround the issue by creating a batch file. Perhaps you could pass the tokens as arguments to the batch file and then build the full command in the batch and call from there?

gollumullog
  • 1,249
  • 2
  • 14
  • 22