PowerShell: Create Human-readable Shortcuts to IIS Log File Directories

About 8 years ago, I wrote a blog post that provided a VBScript to create easy-to-read shortcuts to IIS Log directories. W3SVC56 just wasn't helpful enough to identify which website logs to which directory.

Today, I ported that same script to PowerShell. I'm too lazy to explain, so this is what it does:

iis-big

Easy peasy. Here's the script. Note that you have to run this on the IIS server itself, and you have to have adequate permissions. I ran this with admin privs.

#############################################################

This script creates a Shortcuts directory, then creates

shortcuts to IIS Log file directories, based off of their

IIS website name.

ex. C:\inetpub\logs\LogFiles\Shortcuts\Default Web Site ->

C:\inetpub\logs\LogFiles\W3SVC1

Must be run on the local web server, and you must have

access to IIS and the shortcuts directory.

#############################################################

Set the folder where the Shortcuts folder will be

created and populated. If left at default, it will use IIS's

global default log directory. Usually C:\inetpub\logs\LogFiles

$shortcutbasedir = "default"

#############################################################

No additional changes are required below

#############################################################

Import-Module "WebAdministration" -ErrorAction Stop

Get global log file directory from IIS

if ($shortcutbasedir -eq "default") { $xml = [xml](Get-Content C:\Windows\System32\inetsrv\config\applicationHost.config) $shortcutbasedir = $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory }

Convert old school env variables to PowerShell env variable if needed

if ($shortcutbasedir -match "%") { $sc = ($shortcutbasedir -split "%") $sysvar = $sc[1] $sysvar = (get-item env:$sysvar).Value $shortcutbasedir = $sysvar + $sc[2] }

$shortcutsdir = "$shortcutbasedir\Shortcuts"

If shortcuts directory doesn't exist, create it. Otherwise, empty it out.

if (!(Test-path $shortcutsdir)) { $null = New-Item -ItemType Directory -Path $shortcutsdir } else { $null = Remove-Item "$shortcutsdir\*" }

Get websites from IIS

$sites = Get-ChildItem IIS:\Sites

foreach($site in $sites)
{
	# Get website info
	$sitename = $site.Name
	$siteid = $site.id
	$basedir = (Get-ItemProperty IIS:\\Sites\\$sitename -name logFile.directory).Value
	$folder = "$basedir\\W3SVC$siteid"

	# Create the shortcut
	$wshshell = New-Object -ComObject WScript.Shell
	$shortcut = $wshshell.Createshortcut("$shortcutsdir\\$sitename.lnk")
	$shortcut.TargetPath = "$basedir\\W3SVC$siteid"
	$shortcut.Save()
}