Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell sript to set IIS environment variable

Tags:

powershell

iis

How can I automate the following steps by using PowerShell? :

  1. Go to IIS and choose a website (website name will most likely be passed down in script as a parameter)
  2. Go Inside the Configuration Editor enter image description here
  3. Choose the aspNetCore Section in the dropdown enter image description here
  4. Go inside the environmnetVariables enter image description here
  5. Add new Collection enter image description here
  6. Create new environment varialbe enter image description here
like image 710
scorpion5211 Avatar asked Mar 08 '19 16:03

scorpion5211


People also ask

How do I set an environment variable in PowerShell script?

To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.

How do I set an environment variable in IIS?

Go to your application in IIS and choose Configuration Editor . Choose Applicationhost. config ... in From combobox. Right click on enviromentVariables element, select 'environmentVariables' element , then Edit Items .

What is $ENV in PowerShell?

Think of Env: as a drive, it's just like the C:, except Env: contains not folders but variables such as 'Path' or 'Windir'. We are going to use PowerShell to manipulate the System variables, this is an alternative to using the Windows GUI and navigating to the Control Panel, System and 'Advanced system settings'.


1 Answers

Thanks to Lex Li, Seems like Configuration Editor generates scripts automatically before savings. This has lead me to getting the final PS script to be:

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/WEBSITENAME'  -filter "system.webServer/aspNetCore/environmentVariables" -name "." -value @{name='EnvironmentVariableName';value='EnvironmentVariableValue'}
like image 56
scorpion5211 Avatar answered Nov 15 '22 05:11

scorpion5211