Parameterising values causes error in powershell/msbuild command - powershell

I have a powershell script that in turn calls msbuild.exe and runs it with a solution value and a parameters argument. If i run this script by writing the line manually it runs as expected and builds the solution, however, due to the fact that this script is designed to build multiple solutions, the parameters, solution file path, and msbuild path are all passed as variables.
The line that i need to run looks like this:
& 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe' FooBar.sln "/t:rebuild" /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries\\ | Out-Host
And the way it is split into variables is as follows:
$1 = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe'
$2 = 'FooBar.sln'
$3 = "/t:rebuild /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries\\ | Out-Host"
& $1 $2 $3
For context, to get these values we use an XML with many nodes with 3 properties on each.
The error i get when i run the paramaterised script is as follows:
Unhandled Exception: System.ArgumentException: The name "Foo_Bar:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.bar.Binaries\\ | Out-Host" contains
an invalid character ".".
Obviously this is due to how powershell is handling the strings but I can't figure out how to make it work.

| Out-Host should not be passed as part of an argument to an application - but that's not the issue here.
It seems to be a combination of combining all command line switches in a single string and having . anywhere in said argument - if I split the string into individual parts or replace your OutDir argument with a path that doesn't contain any ., msbuild proceeds to attempt to build the projects:
$msbuild = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe'
$solution = 'FooBar.sln'
$arguments = "/t:rebuild /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries"
&$msbuild $solution $arguments.Split(' ') |Out-Host

Related

Autorunsc64 (sysinternals) command line issue in PowerShell

I am trying to execute autorunsc64.exe (Sysinternals) in PowerShell like so:
"C:\Program Files (x86)\Autoruns\autorunsc64.exe" -a * > "C:\Program Files (x86)\Autoruns\output.txt"
However, it does not like single or double quotes anywhere. I've tried many variations but cannot seem to find the solution. I keep getting the following errors:
Unexpected token '-accepteula' in expression or statement
Unexpected token '-a' in expression or statement
If the paths do not have spaces, it works without issue:
C:\Temp\Autoruns\autorunsc64.exe -accepteula -a * > C:\Temp\Autoruns\output.txt
How can I type this up to work with C:\Program Files (x86)\Autoruns\autorunsc64.exe -a * > C:\Program Files (x86)\Autoruns\output.txt so it can run from these locations using PowerShell?
Very common question.
& "C:\Program Files (x86)\Autoruns\autorunsc64.exe"
Or even
C":\Program Files (x86)\Autoruns\autorunsc64.exe"
I would just add it to the path.

Run TextTransform.exe with args from Powershell

I want to run TextTransform.exe with the parameter args on my template while in PowerShell v3. Here are my variables and the options I tried. Note that it works with no '-a' parameters, but I need the args to run the template correctly.
$textTransformPath = "C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\14.0\TextTransform.exe"
$templateath = "$ProjectPath\TheTemplate.tt"
$textTransformParams = "-a !!TheParam!$TheValue"
#& "$textTransformPath" "$templatePath" <-- this runs, but no args!
# these don't work:
& "$textTransformPath" "$templatePath" $textTransformParams
& "$textTransformPath" "$templatePath" "$textTransformParams"
I don't know why this is so difficult, it seems like it should be really easy. If I type out this in the standard command line it works, which verifies the problem is with my PS syntax.
It never fails...one last search after almost giving up and found the answer:
Using $(SolutionDir) when running template via TextTransform.exe
The combo I apparently didn't try previously was:
& "$textTransformPath" "$templatePath" -a !!TheParam!$TheValue
Hope this helps someone else.

TFSBuild to include an array in parameters to powershell

I have a TFS 2010 build which calls a powershell script for deployment. I've defined several arguments for the build script and these have worked great. They are used by the build and also included in the arguments that are passed into Powershell via the Arguments property of the InvokeProcess control.
I now have a requirement for the powershell script to deploy to a variable number of servers, so I'd like to pass the server ID's in on the argument list from TFS.
In the build definition, I have declared a new argument called TargetServers of type string[]. I have populated this from the Build Process Parameters dialog prior to executing a build.
I have set the FileName property of the InvokeProcess control to "Powershell", and the Arguments property as follows:
String.Format(" ""& '{0}' '{1}' '{2}' '{3}' '{4}' '{5}' '{6}' '{7}' '{8}' '{9}' "" ", DeploymentScriptFileName, IO.Path.GetDirectoryName(DeploymentScriptFileName), "ExecuteBizTalkAppMSI.ps1", MSIFileName, BTDFFilename, TargetServerPath, TargetServers, ServerDeploymentFolder, InstallFolder, HostInstanceFilter, ApplicationName)
My problem is that the TargetServers argument being passed to Powershell is simply System.String[].
From the build log I can see the following output of the Invoke Process control:
Powershell "& 'C:\Builds\3\x.Int.MIS.Deployment\CopyDeployScriptThenExecute.ps1'
'C:\Builds\3\\x.Int.MIS.Deployment' 'ExecuteBizTalkAppMSI.ps1'
'x.Int.MIS-3.0.0.msi' 'x.Int.MIS.Deployment.btdfproj'
'\\d-vasbiz01\BizTalkDeployment' 'System.String[]' 'c:\BizTalkDeployment'
'c:\Program Files (x86)\x.Int.MIS for BizTalk 2010\3.0' 'BTSSvc*MIS*' "
Can anyone please advise how to pass the array?
As strings delimited by commas:
PS> function foo([string[]]$x){$x}
PS> foo a,2,3
a
2
3
If you want, you can put quotes around each individual item but you don't need to unless they contain spaces or other characters reserved for the syntax.

How to pass a variable as an argument to a command with quotes in powershell

My powershell script takes the following parameter:
Param($BackedUpFilePath)
The value that is getting passed into my script is:
"\123.123.123.123\Backups\Website.7z"
I have another variable which is the location I want to extract the file:
$WebsiteDeploymentFolder = "C:\example"
I am trying to extract the archive with the following command:
`7z x $BackedUpFilePath -o$WebsiteDeploymentFolder -aoa
I keep getting the following error:
Error:
cannot find archive
The following works but I need $BackedUpFilePath to be dynamic:
`7z x '\123.123.123.123\Backups\Website.7z' -o$WebsiteDeploymentFolder -aoa
I think I need to pass $BackedUpFilePath to 7z with quotes but they seem to get stripped out no matter what I try. I am in quote hell.
Thanks.
EDIT: It turns out the problem was I was passing in "'\123.123.123.123\Backups\Website.7z'". (extra single quotes)
The easiest way to work with external command line applications in PowerShell (in my opinion) is to use aliases. For example, the following works fine for me.
Set-Alias Szip C:\Utilities\7zip\7za.exe
$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'
SZip a $Archive $Filename
PowerShell takes care of delimiting the parameters correctly.

Powershell error in Executing NUnit test

This might sound silly but I have been trying to execute a NUnit test using powershell script, had several attempts but no hope. is there different format or do I need to add a plugin?
Any help would be appriciated...
Command = "c:\Program Files\NUnit 2.4.8\bin\nunit-console.exe" /config=Release "C:\projects\IntegrationTests\IntegrationTests.nunit" 2>&1
Output as below:
PS C:\tests> "c:\Program Files\NUnit2.4.8\bin\nunit-console.exe" /config=Release
"C:\projects\IntegrationTests\IntegrationTests.nunit" 2>&1
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:55 + "c:\Program Files\NUnit 2.4.8\bin\nunit-console.exe" / <<<<
config=Release "C:\projects\IntegrationTests\IntegrationTests.nunit" 2>&1
Thanks in Advance
You didn't put the part
/config=Release
inside your quoted command text.
Your command should probably look like
"c:\Program Files\NUnit 2.4.8\bin\nunit-console.exe /config=Release C:\projects\IntegrationTests\IntegrationTests.nunit" 2>&1
... i didn't check nunit-console.exe command line options, but i suppose you already tested if the nunit command works.
Sorry for the mess in the top dialog, proper code version below
& 'c:\Program Files\NUnit 2.4.\bin\nunitconsole.exe' /config=Release C:\Projects\IntegrationTests\IntegrationTests.nunit 2>&1