While trying to automate the creation of new IIS websites using PowerShell I needed a script to create IIS virtual directories with a specific login (i.e. Connect As)
After several hours of searching and trying various solutions I finally arrived at this:
$sitename = "My Website Name" $virtualdirectory = "virtual1" $virtualdirectorypath = "C:\My Virtual Path" $username = "username1" $password = "password1" New-WebVirtualDirectory -Site $sitename -Name $virtualdirectory -PhysicalPath $virtualdirectorypath Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='$sitename']/application[@path='/']/virtualDirectory[@path='$virtualdirectory']" -name userName -value $username Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='$sitename']/application[@path='/']/virtualDirectory[@path='$virtualdirectory']" -name password -value $password
I tried several path alternatives, however the XPath queries listed in the snippet are the only iteration that worked.
Hope this saves someone else a little time.
$siteName = 'Default Web Site'
$virtualDirectoryName = 'Test'
$physicalPath = '\\UNC-path'
$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)
Set-ItemProperty $virtualDirectoryPath -Name username -Value 'UserName'
Set-ItemProperty $virtualDirectoryPath -Name password -Value 'Password'
Get-Item -Path $virtualDirectoryPath | fl *
Thanks for the post, just needed to change this line in order to get it to work userName instead of username
Set-ItemProperty $virtualDirectoryPath -Name userName -Value 'UserName'
Thank you so much for sharing this post.
An information technology professional with twenty three years experience in systems administration, computer programming, requirements gathering, customer service, and technical support.