Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active August 17, 2021 19:31
Show Gist options
  • Select an option

  • Save Rugby-Ball/1e7d8fd5cb223819f47fa784e83e4575 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/1e7d8fd5cb223819f47fa784e83e4575 to your computer and use it in GitHub Desktop.
Pulls IIS Website and Web Application details from a list of servers. #Inventory #IIS #Public #WebServer
#IIS-web_site-and-app-details.ps1
#Pulls IIS Website and Web Application details from a list of servers.
#Version 1.0
#Created: 8/17/2021
#Modified: 8/17/2021
#Created by: Ed Walsh
#Sets up file path depending on OS this is run on. Should save to Documents or Home folder. And should run on PowerShell Core.
$timestamp = get-date -format yyyyMMddHHmmss
$subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else { "\Documents\" }
$mydocuments = $home + $subfolder
$fileName = "IIS-Website-WebApp-details-" + [string]$timestamp + ".csv"
$filePath = Join-Path $mydocuments $fileName
$results = @()
#Enter in list of WebServers to run against. You can also alter this to read a csv file or run against other lists from an AD.
$Servers = "WebServer1", "WebServer2"
## This is written so it will work with Powershell 3.0 and Windows 2008 R2, the Module IISAdministration only works on Windows 2012 and higher. Comments below show which lines are for 2008 r2 and those for anything 2012 and older.
## NOTE: The 2008 R2 version compatible version of this script will run on all new Windows OS.
#This is the script block that get run on each of the servers in the list of $Servers
$ScriptBlock = {
Import-Module WebAdministration
#Import-Module IISAdministration #This would be used if you have no servers older than Windows 2012.
$out = @()
$Websites = Get-Website
foreach ($Website in $Websites) {
#$AppPool = Get-IISAppPool -Name $Website.ApplicationPool #This would be used if you have no servers older than Windows 2012.
$AppPool = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $Website.ApplicationPool) #This is used to be compatable with Windows 2008 R2
$WebApplications = Get-WebApplication -site $Website.Name
$IdentityType = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $apppool.name | Select @{n = "identityType"; e = { $_.processModel.identityType } } ).identityType
$Identity = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $apppool.name | Select @{n = "username"; e = { $_.processModel.username } }).username
$o = [PSCustomObject]@{
HostName = Hostname
Website_Name = $Website.Name -join ';'
Website_Id = $Website.Id -join ';'
Website_State = $Website.State -join ';'
Application_Name = "" -join ';'
Type = $website.ElementTagName -join ';'
PhysicalPath = $Website.PhysicalPath -join ';'
Website_Bindings = $Website.Bindings.Collection -join ';'
Website_Attributes = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
AppPool_Name = $AppPool.Name -join ';'
AppPool_State = $AppPool.State -join ';'
AppPool_ManagedPipelineMode = $AppPool.ManagedPipelineMode -join ';'
AppPool_StartMode = $AppPool.StartMode -join ';'
AppPool_UserName = $Identity -join ';'
Identity_Type = $IdentityType -join ';'
}
$out += $o
foreach ($webapp in $webapplications) {
#$AppPool = Get-IISAppPool -Name $webapp.ApplicationPool #This would be used if you have no servers older than Windows 2012.
$AppPool = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $webapp.ApplicationPool) #This is used to be compatable with Windows 2008 R2
$IdentityType = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $apppool.name | Select @{n = "identityType"; e = { $_.processModel.identityType } } ).identityType
$Identity = (Get-ChildItem -Path IIS:\AppPools\ | Where name -eq $apppool.name | Select @{n = "username"; e = { $_.processModel.username } }).username
$o = [PSCustomObject]@{
HostName = Hostname
Website_Name = $Website.Name -join ';'
Website_Id = $Website.Id -join ';'
Website_State = $Website.State -join ';'
Application_Name = ($webapp.path).TrimStart("/") -join ';'
Type = $webapp.ElementTagName -join ';'
PhysicalPath = $webapp.PhysicalPath -join ';'
Website_Bindings = $Website.Bindings.Collection -join ';'
Website_Attributes = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
AppPool_Name = $AppPool.name -join ';'
AppPool_State = $AppPool.State -join ';'
AppPool_ManagedPipelineMode = $AppPool.ManagedPipelineMode -join ';'
AppPool_StartMode = $AppPool.StartMode -join ';'
AppPool_UserName = $Identity -join ';'
Identity_Type = $IdentityType -join ';'
}
$out += $o
}
}
$out
}
foreach ($server in $servers) {
$result = invoke-command $server -scriptblock $scriptblock
$results += $result
}
#Used to clean up some columns not needed in output.
$output = $results | Select-Object Hostname, Website_name, Website_ID, Website_State, Application_Name, Type, PhysicalPath, Website_Bindings, Website_Attributes, AppPool_Name, AppPool_State, AppPool_ManagedPipelineMode, AppPool_StartMode, AppPool_UserName, identity_Type
#Export to a CSV file in your Home or Documents folder, depending on what OS you are running this on.
$Output | Export-Csv -NoTypeInformation -Append -Path $filepath
#Show in Gridview : Warning, may not work on Non-Windows machines. At the time this was written Gridview didnt work in PowerShell Core.
$Output | Out-GridView
Write-Output "Exported to: $filePath"
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment