I work with a lot of IIS servers. Keeping track of what sites are present on which servers can sometimes be a daunting task. Heretofore I have been dealing with the rather obnoxious and time consuming process of checking sites and bindings by looking at each one in the IIS console. The other day I thought to myself, surely there must be a more effecient way to do this from the CLI via PowerShell… There is… I quickly found the following code on Stack Overflow after a Google search:
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }} ,
@{n="LogFile";e={ $_.logfile | select -expa directory}},
@{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ';' }} |
Export-Csv -NoTypeInformation -Path C:\my_list.csv
Thank you stack overflow: http://stackoverflow.com/questions/13126811/powershell-list-of-websites-using-get-website
One thing I will note is that this command failed the first time I ran it because you have to first import the Powershell Web Administration module. You can do so by running this command first:
Also, it may go without saying but all of the above needs to be run from an elevated shell.
At some point in the future I would like to construct a more complex script that can do this across a range of servers remotely and have each one email back the results as an attached CSV or perhaps feed the data back directly into a single CSV file with a section for each server. If someone out there has already gotten to this before me please point me to your solution and save me some time :).
Cheers!
UPDATE 8/26/2016:
This is an updated code snippet that includes importing the module and also naming the output file based on the name of the server it is running on:
$servername = $env:computername
get-website | select name,id,state,physicalpath,
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }} ,
@{n="LogFile";e={ $_.logfile | select -expa directory}},
@{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ';' }} |
Export-Csv -NoTypeInformation -Path C:\$servername-iis-sites.csv