Skip to content

Instantly share code, notes, and snippets.

@hoangitk
Last active November 11, 2024 03:10
Show Gist options
  • Save hoangitk/b86fe6473a7be1ea6041d6ae28459ec8 to your computer and use it in GitHub Desktop.
Save hoangitk/b86fe6473a7be1ea6041d6ae28459ec8 to your computer and use it in GitHub Desktop.
[Powershell WebAdministration] #powershell #web #admin

Web Administration

Modules

Import-Module WebAdministration

Create new App Pools

new-item AppPools\DemoAppPool

Create new Website

New-Item IIS:\Sites\DemoSite -physicalPath C:\DemoSite -bindings @{protocol="http";bindingInformation=":8080:"}
Set-ItemProperty IIS:\Sites\DemoSite -name applicationPool -value DemoAppPool

New-Item IIS:\Sites\DemoSite\DemoApp -physicalPath C:\DemoSite\DemoApp -type Application
Set-ItemProperty IIS:\sites\DemoSite\DemoApp -name applicationPool -value DemoAppPool

New-Item IIS:\Sites\DemoSite\DemoVirtualDir1 -physicalPath C:\DemoSite\DemoVirtualDir1 -type VirtualDirectory
New-Item IIS:\Sites\DemoSite\DemoApp\DemoVirtualDir2 -physicalPath C:\DemoSite\DemoVirtualDir2 -type VirtualDirectory

Dir

  • IIS:\Sites
  • IIS:\AppPools

Start/Stop/Restart

Start-WebItem
Stop-WebItem
Restart-WebItem

Start-Website
Stop-Website
Restart-Website

Start-WebAppPool
Stop-WebAppPool
Restart-WebAppPool

Others

  • Set StartMode to "OnDemand" or "AlwaysRunning" #1 = AlwaysRunning, 0 = OnDemand
> Get-ChildItem Test_* | % { & Set-ItemProperty $_.Name -Name "startMode" -Value 0 }
> Get-ChildItem Test_* | Set-ItemProperty -Name startMode  -Value 0
Set-ItemProperty IIS:\Sites\$websiteName -Name applicationDefaults.serviceAutoStartEnabled -Value True  
Set-ItemProperty IIS:\Sites\$websiteName -Name applicationDefaults.serviceAutoStartProvider -Value 'ApplicationPreload'  
Set-ItemProperty IIS:\AppPools\$appPoolName -Name autoStart -Value True  
Set-ItemProperty IIS:\AppPools\$appPoolName -Name startMode -Value 1 #1 = AlwaysRunning, 0 = OnDemand  
Set-ItemProperty IIS:\AppPools\$appPoolName -Name processModel.idleTimeout -Value "00:00:00" #0 = No timeout  
$Servers = Get-Content C:\Users\Desktop\server.txt

$Servers | ForEach-Object {
            Invoke-Command -ComputerName $_ -ScriptBlock {
            Import-Module WebAdministration
            cd IIS:/Sites
            $Application = dir
foreach ($item in $Application)
{
    $ApplicationName = $item.Name
    $Website = Get-Item $ApplicationName
    $Website.serverAutoStart = 'true'
    $Website | Set-Item
}
            cd IIS:/AppPools
            $ApplicationPools = dir
foreach ($item in $ApplicationPools)
{
    $ApplicationPoolName = $item.Name
    $AppPool = Get-Item $ApplicationPoolName
    $AppPool.autoStart = 'true'
    $AppPool.startmode = 'alwaysrunning'
    $AppPool | Set-Item
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment