powershelliis-7.5

IIS7.5 PowerShell preloadEnabled


Is it possible I can use PowerShell command (e,g, New-WebSite) to create a web site and set site's preloadEnabled="true"?


Solution

  • I've been looking for this too, but couldn't find anything in WebAdministration to set this option. Presumably the approach would be to call New-ItemProperty on the correct WebApplication. Unfortunately, I was unable to get the "default" application for a given website, or to set this property on it. It kinda seems like the WebAdministration module (which enables cmdlets like New-WebSite) was written with earlier versions of IIS in mind, and certainly before the Application Initialization module.

    This is a workaround, which forces the setting of these properties by editing the underlying applicationHost.config file. This is a slightly simplified version of a script we're now using. You'll need to run this script as an administrator.

    # Copy applicationHost.config to the temp directory,
    # Edit the file using xml parsing,
    # copy the file back, updating the original
    
    $file = "applicationhost.config"
    $source = Join-Path "$env:windir" "\system32\inetsrv\config\$file"
    $temp = Join-Path "$env:temp" "$([Guid]::NewGuid().ToString())"
    $tempFile = Join-Path "$temp" "$file"
    
    #update all applications in websites whose name matches this search term
    $search = "website name to search for"
    
    #copy applicationHost.config to  temp directory for edits
    #assignments to $null simply silence output
    $null = New-Item -itemType Directory -path $temp
    $null = Copy-Item "$source" "$temp"
    
    # Load the config file for edits
    [Xml]$xml = Get-Content $tempFile
    
    # find sites matching the $search string, enable preload on all applications therein
    $applications = $xml.SelectNodes("//sites/site[contains(@name, `"$search`")]/application") 
    $applications | % { 
        $_.SetAttribute("preloadEnabled", "true") 
    }
    
    #save the updated xml
    $xml.Save("$tempFile.warmed")
    
    #overwrite the source with updated xml
    Copy-Item "$tempfile.warmed" "$source"
    
    #cleanup temp directory
    Remove-Item -recurse $temp