web-dev-qa-db-fra.com

Afficher tous les sites et les liaisons dans PowerShell

Je documente tous les sites et les liens liés au site à partir d'IIS. Existe-t-il un moyen simple d’obtenir cette liste par le biais d’un script PowerShell plutôt que de taper manuellement en regardant IIS?

Je veux que la sortie ressemble à ceci:

Site                          Bindings
TestSite                     www.hello.com
                             www.test.com
JonDoeSite                   www.johndoe.site
36
sanjeev40084

Essayez quelque chose comme ceci pour obtenir le format souhaité:

Get-WebBinding | % {
    $name = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1'
    New-Object psobject -Property @{
        Name = $name
        Binding = $_.bindinginformation.Split(":")[-1]
    }
} | Group-Object -Property Name | 
Format-Table Name, @{n="Bindings";e={$_.Group.Binding -join "`n"}} -Wrap
41
Frode F.

Essaye ça:

Import-Module Webadministration
Get-ChildItem -Path IIS:\Sites

Il devrait retourner quelque chose qui ressemble à ceci:

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
ChristophersWeb 22   Started    C:\temp             http *:8080:ChristophersWebsite.ChDom.com

De là, vous pouvez affiner les résultats, mais soyez prudent. Un tuyau vers l'instruction select ne vous donnera pas ce dont vous avez besoin. En fonction de vos besoins, je construirais un objet personnalisé ou une table de hachage.

74

Le moyen le plus simple que j'ai vu:

Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) {[pscustomobject]@{name=$Site.name;Protocol=$Bind.Protocol;Bindings=$Bind.BindingInformation}}}
19
Alexander Shapkin

Si vous voulez juste lister tous les sites (ie. Trouver une liaison)

Changez le répertoire de travail en "C:\Windows\system32\inetsrv"

cd c:\Windows\system32\inetsrv

Exécutez ensuite "appcmd list sites" (pluriel) et exportez-le dans un fichier. par exemple c:\IISSiteBindings.txt

appcmd list sites> c:\IISSiteBindings.txt

Ouvrez maintenant avec le bloc-notes à partir de votre invite de commande.

Bloc-notes c:\IISSiteBindings.txt

14
Armand G.

Essaye ça

function DisplayLocalSites
{

try{

Set-ExecutionPolicy unrestricted

$list = @()
foreach ($webapp in get-childitem IIS:\Sites\)
{
    $name = "IIS:\Sites\" + $webapp.name
    $item = @{}

$item.WebAppName = $webapp.name

foreach($Bind in $webapp.Bindings.collection)
{
    $item.SiteUrl = $Bind.Protocol +'://'+         $Bind.BindingInformation.Split(":")[-1]
}


$obj = New-Object PSObject -Property $item
$list += $obj
}

$list | Format-Table -a -Property "WebAppName","SiteUrl"

$list | Out-File -filepath C:\websites.txt

Set-ExecutionPolicy restricted

}
catch
{
$ExceptionMessage = "Error in Line: " + $_.Exception.Line + ". " +     $_.Exception.GetType().FullName + ": " + $_.Exception.Message + " Stacktrace: "    + $_.Exception.StackTrace
$ExceptionMessage
}
}
2
snimakom
function Get-ADDWebBindings {
param([string]$Name="*",[switch]$http,[switch]$https)
    try {
    if (-not (Get-Module WebAdministration)) { Import-Module WebAdministration }
    Get-WebBinding | ForEach-Object { $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1' } | Sort | Get-Unique | Where-Object {$_ -like $Name} | ForEach-Object {
        $n=$_
        Get-WebBinding | Where-Object { ($_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1') -like $n } | ForEach-Object {
            if ($http -or $https) {
                if ( ($http -and ($_.protocol -like "http")) -or ($https -and ($_.protocol -like "https")) ) {
                    New-Object psobject -Property @{Name = $n;Protocol=$_.protocol;Binding = $_.bindinginformation}
                }
            } else {
                New-Object psobject -Property @{Name = $n;Protocol=$_.protocol;Binding = $_.bindinginformation}
            }
        }
    }
    }
    catch {
       $false
    }
}
2
user6804160

J'ai trouvé cette question parce que je souhaitais générer une page Web contenant des liens vers tous les sites Web exécutés sur mon instance IIS. J'ai utilisé la réponse d'Alexander Shapkin pour trouver ce qui suit afin de générer un tas de liens.

$hostname = "localhost"

Foreach ($Site in get-website) {
    Foreach ($Bind in $Site.bindings.collection) {
        $data = [PSCustomObject]@{
            name=$Site.name;
            Protocol=$Bind.Protocol;
            Bindings=$Bind.BindingInformation
        }
        $data.Bindings = $data.Bindings -replace '(:$)', ''
        $html = "<a href=""" + $data.Protocol + "://" + $data.Bindings + """>" + $data.name + "</a>"
        $html.Replace("*", $hostname);
    }
}

Ensuite, je colle les résultats dans ce code HTML rédigé à la hâte:

<html>
<style>
    a { display: block; }
</style>
{paste PowerShell results here}
</body>
</html>
0
Walter Stabosz