Domanda

Utilizzo di Powershell 1.0 in Windows Server 2003 con IIS 6.

Ho circa 200 siti per i quali vorrei modificare l'indirizzo IP (come indicato nelle proprietà del sito Web nella scheda "Sito Web" nel campo "Identificazione sito Web" sezione "Indirizzo IP".

Ho trovato questo codice:

$site = [adsi]"IIS://localhost/w3svc/$siteid"
$site.ServerBindings.Insert($site.ServerBindings.Count, ":80:$hostheader")
$site.SetInfo()

Come posso fare una cosa del genere ma:

  1. Scorri tutti i siti in IIS
  2. Non inserire un valore di intestazione host, ma modificarne uno esistente.
È stato utile?

Soluzione

Il seguente script di PowerShell dovrebbe aiutare:

$oldIp = "172.16.3.214"
$newIp = "172.16.3.215"

# Get all objects at IIS://Localhost/W3SVC
$iisObjects = new-object `
    System.DirectoryServices.DirectoryEntry("IIS://Localhost/W3SVC")

foreach($site in $iisObjects.psbase.Children)
{
    # Is object a website?
    if($site.psbase.SchemaClassName -eq "IIsWebServer")
    {
        $siteID = $site.psbase.Name

        # Grab bindings and cast to array
        $bindings = [array]$site.psbase.Properties["ServerBindings"].Value

        $hasChanged = $false
        $c = 0

        foreach($binding in $bindings)
        {
            # Only change if IP address is one we're interested in
            if($binding.IndexOf($oldIp) -gt -1)
            {
                $newBinding = $binding.Replace($oldIp, $newIp)
                Write-Output "$siteID: $binding -> $newBinding"

                $bindings[$c] = $newBinding
                $hasChanged = $true
            }
            $c++
        }

        if($hasChanged)
        {
            # Only update if something changed
            $site.psbase.Properties["ServerBindings"].Value = $bindings

            # Comment out this line to simulate updates.
            $site.psbase.CommitChanges()

            Write-Output "Committed change for $siteID"
            Write-Output "========================="
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top