0

I hava to add some environment to appPools? and i tried this code:

$Appcmd = [System.Environment]::SystemDirectory + "\inetsrv\appcmd.exe"

& $appcmd --% set config -section:system.applicationHost/applicationPools /+""[name='$Task.eProto_Pool'].environmentVariables.[name='PRODUCT_NAME',value='eProto']"" /commit:apphost"

but $Task in second line does not work, How can I past a variable to this string? I also tried %Task%

  • remove `--%` if you want variable expansion in your arguments :) alternatively prepare the arguments in a separate variable and invoke `appcmd` with: `& $appcmd $arguments` – Mathias R. Jessen Jan 17 '19 at 16:02

1 Answers1

0

.eProto_Pool is a property of $Task. If you want to dereference (that is, retrieve one single property of an object) within a string, you need to wrap the string with $(), the subexpression operator in PowerShell.

For example, I'll make a new hashtable called $MyString that has two properties.

$MyString = @{Name = "Stephen";Value="CoolDude"}

>$MyString

Name                           Value
----                           -----
Value                          CoolDude
Name                           Stephen

Look what happens if I try to reference it inside a string with regular string expansion. This is basically what you were doing in your example above. See how it fails to work as you would expect?

write-host " The user $MyString.Name is a $MyString.Value"
 The user System.Collections.Hashtable.Name is a System.Collections.Hashtable.Value

Time to use the subexpression operator to save the day.

write-host " The user $($MyString.Name) is a $($MyString.Value)"
 The user Stephen is a CoolDude

When in doubt, subexpression it out.

On second glance

I think it might be the percentage sign % which is causing you grief. This is a shorthand for the ForEach-Object command in PowerShell. Try this instead:

Invoke-expression "$appcmd --% set config -section:system.applicationHost/applicationPools /+`"`"[name='$($Task.eProto_Pool)'].environmentVariables.[name='PRODUCT_NAME',value='eProto']`"`" /commit:apphost`""

This should escape the strings like you need, and also pass the parameters in, like the eProto_Pool property of $Task.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • it does not work because of --% I think, anf $(Task) is not helpfull, it read $($Task) as a string not parameter – Pavel Mekhnin Jan 18 '19 at 08:47
  • `$SourceBranch = $json.SourceBranch $SourceBranch $Name = $SourceBranch.split("/")[1] #ESITE-1234_test or develop $Name $Task = $name.split("_")[0] #ESITE-1234 or develop $Task $Appcmd = [System.Environment]::SystemDirectory + "\inetsrv\appcmd.exe" & $appcmd --% set config -section:system.applicationHost/applicationPools /+""[name='$($Task).eProto_Pool'].environmentVariables.[name='PRODUCT_NAME',value='eProto']"" /commit:apphost ` – Pavel Mekhnin Jan 18 '19 at 08:49