Creating sites and application pools in IIS from Powershell

I was messing around with my test/dev virtual machine over on Azure, after having blown away the old one so that I could create one with Server 2022, and got tired of creating the pools and sites manually. The code below is the result of a few hours of research and tinkering around, but works well for what I am doing.

This first part is used when creating the main part of the domain on the server.

$Domain="domain.tld"

New-Item -ItemType Directory -Path "W:\inetpub\vhosts\$Domain\" -Name httpdocs
New-Item -ItemType Directory -Path "W:\inetpub\vhosts\$Domain\" -Name logs

New-Item IIS:\AppPools\$Domain

New-Item IIS:\Sites\$Domain -bindings @{protocol="http";bindingInformation="*:80:$Domain"} -physicalPath W:\inetpub\vhosts\$Domain
Set-ItemProperty "IIS:\Sites\$Domain" -name logFile -value @{directory="W:\inetpub\vhosts\$Domain\logs"}
Set-ItemProperty IIS:\Sites\$Domain -name applicationPool -value $Domain
Code language: PowerShell (powershell)

The code below is used for adding a subdomain, and would need to have the wildcard SSL issued for the related main domain already, which I am currently getting from Let’s Encrypt via the Certify the Web application in Windows.

$Domain="sub.domain.tld"
$Parent=$Domain.Substring($Domain.IndexOf(".") + 1)
$Child=$Domain.Split('.')[0]
$Cert = (Get-ChildItem Cert:\LocalMachine\My | Where{$_.Subject -eq "CN=*.$Parent"}).ThumbPrint

New-Item W:\inetpub\vhosts\$Parent\$Child -type Directory

New-Item IIS:\AppPools\$Domain

New-Item iis:\Sites\$Domain -bindings @{protocol="http";bindingInformation="*:80:$Domain"} -physicalPath W:\inetpub\vhosts\$Parent
Set-ItemProperty "IIS:\Sites\$Domain" -name logFile -value @{directory="W:\inetpub\vhosts\$Parent\logs"}
New-IISSiteBinding -Name "$Domain" -BindingInformation "*:443:$Domain" -CertificateThumbPrint $Cert -CertStoreLocation "Cert:\LocalMachine\My" -Protocol https
Set-ItemProperty IIS:\Sites\$Domain -name applicationPool -value $Domain
Code language: PowerShell (powershell)

Finally, two simple lines that can be used to remove the pool and site for any domain/subdomain.

Remove-WebSite -Name "$Domain"
Remove-WebAppPool -Name "$Domain"
Code language: PowerShell (powershell)

Working with firewall rules

Needed to be able to work with firewall rules on servers at work, so decided to start by getting the actual rule query and update part done, so I can add them to a more fleshed out script later.

#NetFirewallRule method for newer versions of Powershell

#Create a new test rule
New-NetFirewallRule -DisplayName "BLACKLIST_IN" -RemoteAddress 9.9.9.9 -Direction Inbound -Protocol TCP -LocalPort Any -Action Block

#Get the IP addresses in the current rule for the remote address field
$curIP = (Get-NetFirewallRule -DisplayName "BLACKLIST_IN" | Get-NetFirewallAddressFilter).RemoteAddress

#Define the new IP addresses to add, and then merge the lists
$newIPs = "9.9.9.10-9.9.9.11","9.9.9.14/31"
$addIPs = @($newIPs) + @($curIP)

#Set the rule with the new list
Set-NetFirewallRule -DisplayName "BLACKLIST_IN" -RemoteAddress $addIPs
Code language: PowerShell (powershell)
#netsh version for older versions of Powershell

#Get the current rule and convert it from comma separated to a list
$netshout = netsh advfirewall firewall show rule name="BLACKLIST_IN"
$nshIP=($netshout | findstr RemoteIP).trim("RemoteIP: ").split(",")

#Define the new IP addresses to add, and then merge the lists
$newnshIPs = "9.9.9.13","9.9.9.14/31"
$nshaddIPs = @($nshIP) + @($newnshIPs)

#Take the new list and convert back to comma separated
$IPList = ($nshaddIPs | Select-Object) -join ","

#Set the rule with the new list
netsh advfirewall firewall set rule name ="BLACKLIST_IN" new remoteip=$IPList
Code language: PowerShell (powershell)

The differences between languages…

I’ve been learning how a few different languages can do the same task, so decided to put up the examples below.

#!/usr/bin/python

number = 5
counter = 1
while (counter <=10):
    if counter < number:
        print (counter, ' is less than ', number)
    elif counter == number:
        print (counter, ' equals ', number)
    else:
        print (counter, ' is greater than ', number)
    counter = counter + 1
Code language: Python (python)
#powershell

$numValue = @(1..10)
$counter = 0

while ($counter -lt $numValue.length){
if ($numValue[$counter] -lt 5){
    write-host "Less than`n"
}
elseif ($numValue[$counter] -eq 5) {
    write-host "Equal`n"
}
else {
    write-host "Greater than`n"
}
$counter += 1
}Code language: PowerShell (powershell)
//C++
#include <iostream>
using namespace std;

int main()
{
    int counter = 1;
    while (counter <= 10) {
        if (counter > 5) { cout << "Greater than\n"; }
        else if (counter == 5) { cout << "Equal\n"; }
        else { cout << "Less than\n"; }
        counter++;
    }
    system("pause>0");
}

Code language: C++ (cpp)