How to update existing IIS 6 Web Site using PowerShell

前端 未结 3 855
我在风中等你
我在风中等你 2021-02-06 11:50

I am trying to create a PowerShell script that creates a new IIS 6 web site and sets things like App Pool, Wildcard application maps, ASP.NET version, etc.

After extensi

相关标签:
3条回答
  • 2021-02-06 11:52

    This is a useful PowerShell snippet.

    I tried running this and I get problems with the delete tests. Delete does not work against the app pool when the site still exists. Surely you should run website delete test first.

    # check if web site exists and delete it - for testing purposes
    $tempWebsite  = gwmi -namespace "root\MicrosoftIISv2" 
                         -class "IISWebServerSetting" 
                         -filter "ServerComment like '%$name%'"
    if (!($tempWebsite -eq $NULL)) {$tempWebsite.delete()}
    

    Run this first, then run the app pool deletion test.
    I realise you have marked these as tests but surely it is useful to exit or delete if webs site exists.

    0 讨论(0)
  • 2021-02-06 12:03

    First of all, big thanks to jrista for pointing me in the right direction.

    I also found this article very useful.

    What follows here is a powershell script to create Application pool, Website and a SelfSsl certificate:

    
    function CreateAppPool ([string]$name, [string]$user, [string]$password)
    {
        # check if pool exists and delete it - for testing purposes
        $tempPool  = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'"
        if (!($tempPool -eq $NULL)) {$tempPool.delete()}
    
        # create Application Pool
        $appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting"
        $newPool = $appPoolSettings.CreateInstance()
    
        $newPool.Name = "W3SVC/AppPools/" + $name
        $newPool.WAMUsername = $user
        $newPool.WAMUserPass = $password
    
        $newPool.PeriodicRestartTime = 1740
        $newPool.IdleTimeout = 20
        $newPool.MaxProcesses = 1
        $newPool.AppPoolIdentityType = 3
    
        $newPool.Put()
    }
    
    function CreateWebSite ([string]$name, [string]$ipAddress, [string]$localPath, [string] $appPoolName, [string] $applicationName)
    {
        # check if web site exists and delete it - for testing purposes
        $tempWebsite  = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
        if (!($tempWebsite -eq $NULL)) {$tempWebsite.delete()}
    
        # Switch the Website to .NET 2.0
        C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -sn W3SVC/
    
        $iisWebService  = gwmi -namespace "root\MicrosoftIISv2" -class "IIsWebService"
    
        $bindingClass = [wmiclass]'root\MicrosoftIISv2:ServerBinding'
        $bindings = $bindingClass.CreateInstance()
        $bindings.IP = $ipAddress
        $bindings.Port = "80"
        $bindings.Hostname = ""
    
        $iisWebService.CreateNewSite($name, $bindings, $localPath)
    
        # Assign App Pool
        $webServerSettings  = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
        $webServerSettings.AppPoolId = $appPoolName
        $webServerSettings.put()
    
        # Add wildcard map
        $wildcardMap = "*, c:\somewildcardfile.dll, 0, All"
        $iis = [ADSI]"IIS://localhost/W3SVC"
        $webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name }
        $webVirtualDir = $webServer.children | where { $_.keyType -eq "IIsWebVirtualDir" }
        $webVirtualDir.ScriptMaps.Add($wildcardMap)
    
        # Set Application name
        $webVirtualDir.AppFriendlyName = $applicationName
    
        # Save changes
        $webVirtualDir.CommitChanges()
    
        # Start the newly create web site
        if (!($webServer -eq $NULL)) {$webServer.start()}
    }
    
    function AddSslCertificate ([string] $websiteName, [string] $certificateCommonName)
    {
        # This method requires for you to have selfssl on your machine
        $selfSslPath = "\program files\iis resources\selfssl"
    
        $certificateCommonName = "/N:cn=" + $certificateCommonName
    
        $certificateValidityDays = "/V:3650"
        $websitePort = "/P:443"
        $addToTrusted = "/T"
        $quietMode = "/Q"
    
    
        $webServerSetting = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '$websiteName'"
        $websiteId ="/S:" + $webServerSetting.name.substring($webServerSetting.name.lastindexof('/')+1)
    
        cd -path $selfSslPath
        .\selfssl.exe $addToTrusted $certificateCommonName $certificateValidityDays $websitePort $websiteId $quietMode
    }
    
    $myNewWebsiteName = "TestWebsite"
    $myNewWebsiteIp = "192.168.0.1"
    $myNewWebsiteLocalPath = "c:\inetpub\wwwroot\"+$myNewWebsiteName
    $appPoolName = $myNewWebsiteName + "AppPool"
    $myNewWebsiteApplicationName = "/"
    $myNewWebsiteCertificateCommonName = "mynewwebsite.dev"
    
    CreateAppPool $appPoolName "Administrator" "password"
    CreateWebSite $myNewWebsiteName $myNewWebsiteIp $myNewWebsiteLocalPath $appPoolName $myNewWebsiteApplicationName
    AddSslCertificate $myNewWebsiteName $myNewWebsiteCertificateCommonName
    
    0 讨论(0)
  • 2021-02-06 12:04

    The $result object contains the path to the newly created IIsWebServer object. You can get access to the virtual directory, where you can configure more properties, by doing the following:

    $w3svcID = $result.ReturnValue -replace "IIsWebServer=", ""
    $w3svcID = $w3svcID -replace "'", ""
    $vdirName = $w3svcID + "/ROOT";
    
    $vdir = gwmi -namespace "root\MicrosoftIISv2" 
                 -class "IISWebVirtualDir" 
                 -filter "Name = '$vdirName'";
    # do stuff with $vdir
    $vdir.Put();
    
    0 讨论(0)
提交回复
热议问题