I'm adding the below targets xml to my csproj (via a nuget package)
<Project>
<Target Name="AfterBuild">
<PropertyGroup>
<DockerExe Condition=" '$(DockerExe)'=='' ">"C:\Program Files\Docker\Docker\resources\bin\docker.exe"</DockerExe>
<ImageName Condition=" '$(ImageName)'=='' ">$(MSBuildProjectName)</ImageName>
<PowershellExe Condition=" '$(PowerShellExe)'=='' ">C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</PowershellExe>
</PropertyGroup>
<Exec Command="$(PowershellExe) -NonInteractive -executionpolicy Unrestricted -command $(MSBuildThisFileDirectory)..\tools\image_name.ps1" />
<Exec Command="$(DockerExe) build -t $(ImageName) ." />
</Target>
</Project>
and image_name.ps1
contains
$name = $(ImageName)
$name = $name.ToLower()
$name = $name.Replace(".", "_")
echo "Name is:"
echo $name
# $(ImageName) = $name
The Powershell script is actually being called, but $name
(and $(ImageName)
) is null
and I have no idea how to access and update the ImageName property set in the targets file. I tried $name = $Env:ImageName
but the result of that assignment was also null
.
The reason those variables in the powershell script are nul is because powershell knows absolutely nothing about msbuild. And msbuild didn't inform powershell what the values were supposed to be.
Anyways, all that string modification is unnecessary anyways, since msbuild can do it for you using property functions: https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019
Thus you don't even need to call a powershell script to modify the string for you.
Last of all: Jakobii's answer above is spot on for getting the powershell script to work. But in the end, you would not be able to get the modified string back out of the powershell script back into msbuild.
Im not exactly sure what you are try to do. but could this be what you are looking for? .ps1
files can have parameters similar to binary files.
C:\path\to\image_name.ps1 -ImageName 'Some Image Name'
param(
$ImageName
)
$name = $ImageName
$name = $name.ToLower()
$name = $name.Replace(".", "_")
write-host "Name is: $name"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.