How to set environmentalVariable to the application pool using powershell?

Lev Anni 41 Reputation points
2021-12-24T08:49:42.067+00:00

please please anyone can tell me how to add these entries using powershell, I know I'm bit close but having errors.

<applicationPools>  
   <add name="Contoso" managedRuntimeVersion="v4.0" managedPipelineMode="Classic">  
      <environmentVariables>  
         <add name="foo" value="bar" />  
      </environmentVariables>  
   </add>  
</applicationPools>  

There is a cmd version of the command found on Microsoft:

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].environmentVariables.[name='foo',value='bar']" /commit:apphost

but I need to do it through powershell

I'm trying something like this:

set-webconfigurationproperty -pspath 'machine/webroot/apphost' -filter "system.applicationhost/applicationpools/add[@DeezNutz ='Contoso']/environmentVariables" -name "foo" -value "bar"

WARNING: Property foo is not found on /system.applicationHost/applicationPools/add[@DeezNutz ='Contoso']/environmentVariables.

Please help

Internet Information Services
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,418 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,331 Reputation points
    2021-12-24T20:48:32.647+00:00

    I finally found it.

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='Auth']/environmentVariables" -name "." -value @{name='aaa';value='zzzz'}  
    

    In the configuration editor, I navigated to my Auth app pool and added an environment variable using the collection editor. Then click on the Generate Script action and copy the Powershell statement.

    160393-capture1.jpg

    160422-capture.jpg


3 additional answers

Sort by: Most helpful
  1. MotoX80 32,331 Reputation points
    2021-12-24T13:57:16.057+00:00

    but I need to do it through powershell

    Powershell will happily run executables for you. Just run the appcmd.exe version in your PS script.

    Try using Add--webconfigurationproperty instead of Set-webconfigurationproperty.

    0 comments No comments

  2. Lev Anni 41 Reputation points
    2021-12-24T15:04:16.437+00:00

    I just wonder, there must be a way. Add--webconfigurationproperty does nothing but empty command line without errors, no changes are made.

    0 comments No comments

  3. Lev Anni 41 Reputation points
    2021-12-24T15:08:08.907+00:00

    By the way, I'm trying all these because I need global variable to be used in php application. Problem is that .user.ini file is not honored from a virtual directory, IIS sees it only from physical directory, I would be glad to hear any suggestions!!!