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.

 1#############################################################
 2#
 3# This script creates a Shortcuts directory, then creates
 4# shortcuts to IIS Log file directories, based off of their
 5# IIS website name.
 6# 
 7# ex. C:\\inetpub\\logs\\LogFiles\\Shortcuts\\Default Web Site ->
 8#     C:\\inetpub\\logs\\LogFiles\\W3SVC1
 9#
10# Must be run on the local web server, and you must have
11# access to IIS and the shortcuts directory.
12# 
13#############################################################
14
15# Set the folder where the Shortcuts folder will be 
16# created and populated. If left at default, it will use IIS's 
17# global default log directory. Usually C:\\inetpub\\logs\\LogFiles
18
19$shortcutbasedir = "default"
20
21#############################################################
22# 
23# No additional changes are required below
24# 
25#############################################################
26
27Import-Module "WebAdministration" -ErrorAction Stop
28
29# Get global log file directory from IIS
30if ($shortcutbasedir -eq "default") {
31	$xml = \[xml\](Get-Content C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config)
32	$shortcutbasedir = $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory
33}
34
35# Convert old school env variables to PowerShell env variable if needed
36if ($shortcutbasedir -match "%") {
37	$sc = ($shortcutbasedir -split "%")
38	$sysvar = $sc\[1\]
39	$sysvar = (get-item env:$sysvar).Value
40	$shortcutbasedir = $sysvar + $sc\[2\]
41}
42
43$shortcutsdir = "$shortcutbasedir\\Shortcuts"
44
45# If shortcuts directory doesn't exist, create it. Otherwise, empty it out.
46if (!(Test-path $shortcutsdir)) { 
47	$null = New-Item -ItemType Directory -Path $shortcutsdir 
48	} else { 
49	$null = Remove-Item "$shortcutsdir\\\*"
50}
51
52# Get websites from IIS
53$sites = Get-ChildItem IIS:\\Sites
54
55	foreach($site in $sites)
56	{
57		# Get website info
58		$sitename = $site.Name
59		$siteid = $site.id
60		$basedir = (Get-ItemProperty IIS:\\Sites\\$sitename -name logFile.directory).Value
61		$folder = "$basedir\\W3SVC$siteid"
62
63		# Create the shortcut
64		$wshshell = New-Object -ComObject WScript.Shell
65		$shortcut = $wshshell.Createshortcut("$shortcutsdir\\$sitename.lnk")
66		$shortcut.TargetPath = "$basedir\\W3SVC$siteid"
67		$shortcut.Save()
68	}