Modifying the host file in C:\Windows\System32\drivers\etc when creating IIS website - powershell

I am trying to create my website through powershell by using the following command
New-Website -Name 'mytestapp' -force -Port 8080 -HostHeader mytestsite -PhysicalPath 'C:\Users\myuser\Desktop\Docker\PublishOutput' -ApplicationPool '.NET v4.5'
I also set the hostheader and indeed I can see in IIS the relative binding. Unfortunately when I navigate to the website nothing happens, because in the host file in C:\Windows\System32\drivers\etc there is no mapping between 127.0.0.1 and mytestsite , which I would expect to happen from the creation of the site itself. How do I do this in a smart way through powershell? I don't want a command to modify a text file, I would expect there is some powershell command IIS related that does this for me.

Related

How to set IP Address Restrictions for Management Service in IIS through PowerShell?

I'm looking for a way to script the whole IIS configuration through PowerShell and I've already done most of it. The problem I'm facing right now is how to set 'IP Address Restrictions' for Management Service in IIS.
I know there is a simple way to do it for a domain or a site but my goal is to limit the number of IP's being able to deploy to IIS.
Including screenshot for clarification:
IIS Management Service
Following #BruceZhang advice, I've firstly set up the required value using the UI, then looked up the registry 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WebManagement\Server\RemoteRestrictions' and found out the encrypted value for my ip address. From now on setting it up was only a matter of changing the value for this key in the registry through PowerShell script. It works fine, thanks!
Stop-Service -Name "WMSVC"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WebManagement\Server" -Name "RemoteRestrictions" -Value /wEZAgAAAAEAAABnAgAAABkBAAAAAAAAABkDAAAAAQAAAC4EA8ADqAMdAx0CAAAALgQD/wP/A/8D/wMAAAB
Start-Service -Name "WMSVC"

Windows authentication fails and needs to be reset

I have an existing IIS web app with windows authentication enabled. When I run this command, to set bindings, Windows authentication fails and I have to manually disable and re-enable windows authentication for it to work.
import-module WebAdministration
Set-ItemProperty "IIS:\Sites\Test\My" -name bindings -value (#{protocol="https";bindingInformation="*:443:my.test.com";sslFlags=1},#{protocol="net.tcp";bindingInformation="808:my.test.com"})
I have tried setting windows authentication with powershell but had no effect.
Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/WindowsAuthentication -name enabled -value true -PSPath "IIS:\Sites\TestMy"
Any suggestions?
The cause of the issue may be that we haven’t bound a certificate to the specific port yet.
After specifying an Https binding, please bind a certificate to that port.
Netsh http add sslcert ipport=1.1.1.1:443
certhash=0102030405060708090A0B0C0D0E0F1011121314
appid={00112233-4455-6677-8899-AABBCCDDEEFF}
https://learn.microsoft.com/en-us/windows/win32/http/add-sslcert
This is commonly finished by the IIS. Without this procedure, the website could work improperly over https binding. You could try to set up an HTTP binding on the website in order to rule this point out.
Feel free to let me know if the problem persists.
 

How to remove web.config file in iis

I have an IIS web page created by this command
New-Website -Force -Name TestSite -Port 8080 -HostHeader TestSite -PhysicalPath C:\inetpub\wwwroot
And i want to be able to pass the command into script and clear ALL configs in iis and than create new web-site.
I've tried some commands from Microsoft site. Like Clear-IISConfigCollection
But i'm not fully sure of how it works.
So i'm asking for help of how to CLEAR configs before creating new web page.
Thanks.

Changing default directory and default html page for IIS through Powershell

I am looking to use Powershell to programatically change the default directory (and the home page - a simple static html) of IIS. For example, I created a new Drive 'D:\' and placed an 'index.html' and I would like IIS to launch this html instead of "C:\inetpub\www\iisstart.htm".
I can do this switch manually by RDP'ing into the machine but I need to achieve this using Powershell.
I am not sure if this is the way to do it but I tried this:
Set-ItemProperty 'IIS:\Sites\Default Web Site\' -name physicalPath -value "D:\"
The command doesn't throw any error but I get 500 - Internal server error.
If I check using another command as below, I get this displayed. But when I type the public IP of this machine the earlier page displaying content of C:\inetpub\www\iisstart.htm vanishes and I get 500 server error.
PS C:\Users\Administrator> Get-Website -Name 'Default Web Site'
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Default Web Site 1 Started D:\ http *:80:
I am pretty sure there must be a very simple way to do this but I couldn't find out what it is, in spite of hunting for hours together. Your help would be much appreciated!
Please note I am only trying to host this simple static html page for now (and nothing more).

Duplicate entry warning in Powershell script while working with IIS?

My script is whitelisting IP for a particular URL in IIS.
Set-WebConfigurationProperty -Filter /system.webserver/security/ipsecurity -Name allowUnlisted -Value $false -Location "default web site"
Add-WebConfiguration /system.webserver/security/ipsecurity -location "default web site" -Value #{ipAddress = 129.0.0.1 ;subnetmask = 255.255.255.0 ;allowed="true"} -pspath IIS:\
It works perfectly except when I remove the entry manually from IIS, and again i try to run this script it warns me "cannot add duplicate entry of type add". I cannot see the entry in IIS. Is there a way to remove that duplicate entry via powershell.
Reason behind this is that, applicationhost.config file in inetserv folder has that entry. If you add it via script, remove it via script, or you can open config file in notepad and find the entry and delete it. Save the file after.