This only finds sites which have applications; we won't even see any sites which don't have apps.
Import-Module
WebAdministration
$result
=
@()
dir IIS:\Sites
|
ForEach-Object
{$result
=
Get-WebApplication
-Site $_.Name}
$result
|
ogv
This lists all sites along with any applications they might have. But it also lists the same site repeatedly if a site has more than one app.
Import-Module
WebAdministration
$result
=
@()
$sites
= dir IIS:\Sites
foreach
($site
in
$sites) {
$applications
=
Get-WebApplication
-Site $site.Name
if
($applications) {
foreach
($app
in
$applications) {
$result
+=
New-Object
PSObject -property
@{
SiteName
=
$site.Name
SiteID
=
$site.ID
PhysicalPathSite
=
$site.PhysicalPath
AppName
=
$app.Path
AppPool
=
$app.ApplicationPool
PhysicalPathApp
=
$app.PhysicalPath}
}
}
else {
$result
+=
New-Object
PSObject -property
@{
SiteName
=
$site.Name
SiteID
=
$site.ID
PhysicalPathSite
=
$site.PhysicalPath
AppName
=
$null
AppPool
=
$null
PhysicalPathApp
=
$null}
}
}
$result
=
$result
|
select SiteName,
SiteID,
PhysicalPathSite,
AppName,
AppPool, PhysicalPathApp
$result
|
ogv
This suppresses repeating the site attributes when a site has several apps. Maybe not best choice if you need to sort results, but looks better if you don't have to sort. Or if you do want to sort on either sites or apps, better sort them before display/export.
Import-Module
WebAdministration
$result
=
@()
$sites
= dir IIS:\Sites
foreach
($site
in
$sites) {
$applications
=
Get-WebApplication
-Site $site.Name
if
($applications) {
$i=0
foreach
($app
in
$applications) {
$i++
if
($i
-le
1) {
# show site info first time around ...
$SiteName
=
$site.Name
$SiteID
=
$site.ID
$PhysicalPathSite
=
$site.PhysicalPath
}
else
{
# ... but don't keep showing the
same site info for every app of that site
$SiteName
=
$null
$SiteID
=
$null
$PhysicalPathSite
=
$null
}
$result
+=
New-Object
PSObject
-property
@{
SiteName
=
$SiteName
SiteID
=
$SiteID
PhysicalPathSite
=
$PhysicalPathSite
AppName
=
$app.Path
AppPool
=
$app.ApplicationPool
PhysicalPathApp
=
$app.PhysicalPath}
}
}
else
{
$result
+=
New-Object
PSObject
-property
@{
SiteName
=
$site.Name
SiteID
=
$site.ID
PhysicalPathSite
=
$site.PhysicalPath
AppName
=
$null
AppPool
=
$null
PhysicalPathApp
=
$null}
}
}
$result
=
$result
|
select SiteName,
SiteID,
PhysicalPathSite,
AppName,
AppPool, PhysicalPathApp
$result
|
ogv
bindings, find all IPs for a server and sort by IP address
This works OK if you happen to already be on the web server in question
Import-Module
WebAdministration
$sites
=
Get-ChildItem
IIS:\sites
$bindings
=
$sites
|
foreach-object
{$_.Bindings} |
foreach-object
{$_.Collection}
| foreach-object
{$_.BindingInformation}
$bindings
| Sort-Object
But if you're not already on the web server in question, starting a remote session can speed things up. I also like to split out the various parts of the returned object into separate columns.
$serverName
=
"someWebServer"
$Session
=
New-PSSession
-ComputerName
$serverName
$block
= {Import-Module
WebAdministration
$sites
=
Get-ChildItem
-path IIS:\Sites
$bindings = $sites | foreach-object {$_.Bindings} | foreach-object {$_.Collection} | foreach-object {$_.BindingInformation}
$bindings
| Select
@{name="IP";
expression={$_.Split(":")[0]}},
@{name="port";
expression={$_.Split(":")[1]}},
@{name="domain"
;
expression={$_.Split(":")[2]}} | sort domain | ft
}
Invoke-Command
-Session
$Session
-ScriptBlock
$block
In this case, I was most interested in sorting by domain. But Often I want to know if an IP address is being used, so I'll sort by that instead. Unfortunately, you can't pipe to an Out-GridView in a remote session and I haven't figured out yet how to return a variable's result to the local session so I can pipe that to an Out-GridView. So, I content myself with Format-Table.
This alternate command which throws in a Group-Object
(assuming you use the same $Session
variable you created above).
But if you want IP addresses, well, it doesn't include those.
$block
= {Import-Module WebAdministration
Get-WebBinding
|
% {
$name
=
$_.ItemXPath
-replace
'(?:.*?)name=''([^'']*)(?:.*)', '$1'
New-Object
psobject -Property
@{
Name
=
$name
Binding
=
$_.bindinginformation.Split(":")[-1]
}
} |
Group-Object
-Property Name | sort Name |
Format-Table
Name, @{n="Bindings";e={$_.Group.Binding -join "`n"}} -Wrap
}
Invoke-Command
-Session
$Session
-ScriptBlock
$block
display names of website along with each site's binding
(same as above without $block
)
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
features enabled
Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "IIS*"} | ogv
Is IIS installed?
if
((Get-WindowsFeature
Web-Server).InstallState
-eq
"Installed") {Write-Host
"IIS is
installed."}
else
{Write-Host
"IIS is not installed."}
IP addresses, list - see bindings, find all IPs for a server and sort by IP address
memory leak, monitor progress for
One of your site's app pools might have to be restarted periodically because it runs out of memory due to a memory leak. If you want more time-based granular detail, schedule this code to run periodically in the task scheduler.
$memorySCC
=
Get-Process
-includeUserName |
?
{$_.UserName
-eq
"IIS
APPPOOL\yourDomain.com"} |
Select-Object
-property workingSet64
$time
=
Get-Date
-Format g
$obj
=
New-Object
psObject
$obj
|
Add-Member
-MemberType noteProperty -Name time -Value
$time
$obj
|
Add-Member
-MemberType noteProperty -Name memory -Value
$memorySCC.workingSet64
$obj
|
Export-Csv
c:\scripts\output.csv -noType -Append