Setting variables in IIS using powershell

1

trying to automate the configuration of a website, it is require to setup some environment variables for an netcore application.

I prepared the following script:

$env:sitename="MyApp"

$env:IMAGEDIRECTORYPATH="C:\Temp\Images\"
$env:REDISINSTANCENAME="DEV"
$env:CACHESERVERHOSTIP="192.168.0.1"
$env:QUEUEMACHINENAME="BOG11"
$env:QUEUESALGORITHMSQUEUE="BOG11\private$\Algorithms"

Import-Module WebAdministration

$envVariables = (
     @{name='SignalRHub';value="$env:IMAGEDIRECTORYPATH"},
     @{name='ApiServerUrl';value="$env:QUEUESALGORITHMSQUEUE"} 
)

$envVariables2 = (
     @{name='SignalRHub3';value="$env:IMAGEDIRECTORYPATH"},
     @{name='ApiServerUrl3';value="$env:QUEUESALGORITHMSQUEUE"} 
)

set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$env:SiteName"  -filter "system.webServer/aspNetCore/environmentVariables" -name "." -value $envVariables
set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$env:SiteName"  -filter "system.webServer/aspNetCore/environmentVariables" -name "." -value $envVariables2

I ran the script in Server A, it works perfectly.

then I ran the script in Server B, and I got this:

set-WebConfigurationProperty : The request is not supported. (Exception from HRESULT: 0x80070032)
At line:23 char:1
+ set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$env:Si ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-WebConfigurationProperty], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand

set-WebConfigurationProperty : The request is not supported. (Exception from HRESULT: 0x80070032)
At line:24 char:1
+ set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$env:Si ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-WebConfigurationProperty], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand

Server B was cloned from Server B.

What could cause this behavior?

powershell
iis
asked on Stack Overflow Jun 14, 2018 by XtianGIS

2 Answers

1

Can you instead try this command:

Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location $env:sitename -filter "system.webServer/aspNetCore/environmentVariables" -name "." -value $envVars
answered on Stack Overflow Jun 15, 2018 by Beno
0

I ran into the same problem. Worked on Development server, got the error described by the original poster on QA server. One is a clone of the other. The issue was that to test the QA server I first setup the site in IIS manually and entered the environment variable through the Configuration Editor in IIS manually. Running the script against that then failed as described. However, when I cleared the existing manually-entered configuration so that there were zero environment variables, the script using Set-WebConfigurationProperty to set the environment variables worked. I did not have to use the -Location parameter.

UPDATE: Spoke too soon. Turns out the same variable was set at the MACHINE/WEBROOT/APPHOST level. When clearing them out at the site level, the script worked but after that it broke again when updating on a subsequent deployment. For me the answer was to remove the duplicate environment variable at the APPHOST level if updating at the site level or updating at the APPHOST level instead of at the site level. Placing the variable at the APPHOST level, of course, would set that for all sites. Hope this helps.

answered on Stack Overflow Aug 19, 2019 by Ricardo • edited Aug 19, 2019 by Ricardo

User contributions licensed under CC BY-SA 3.0