Powershell – Howto create an IIS Virtual Directory using a network share with WMI

iisiis-6powershellwmi

I need to create a virtual directory within an IIS Site pointing at a network share \\servername\sharename\directory and I need to specify a specific user for the Pass-through authentication.

I am after the WMI script to do this which I intend to call from a Powershell script.

Although the target IIS environment is IIS7 (WMI namespace root/WebAdministration) I would prefer to use WMI classes that are IIS6 compatible (root\MicrosoftIISv2) as the rest of the script already works against IIS6.

I know I can probably do this with the IIS7 powershell cmdlets or appcmd but I am trying to maintain the IIS6 compatibility.

Best Answer

## Settings
$siteName = 'Default Web Site'
$virtualDirectoryName = 'Test'
$physicalPath = '\\UNC-path'

## Init
$virtualDirectoryPath = "IIS:\Sites\$siteName\$virtualDirectoryName"

## Create Virtual Directory where physicalpath is an UNC-path (New-WebVirtualDirectory wont do)
New-Item $virtualDirectoryPath -type VirtualDirectory -physicalPath $physicalPath

## Change 'Connect As' settings (New-WebVirtualDirectory don't include Username and Password)
##userName must have the N capitalized
Set-ItemProperty $virtualDirectoryPath -Name userName -Value 'UserName'
Set-ItemProperty $virtualDirectoryPath -Name password -Value 'Password'

## Status
Get-Item -Path $virtualDirectoryPath | fl *
Related Topic