Created
December 17, 2014 21:48
-
-
Save ducas/70aabf7b31a0490b45af to your computer and use it in GitHub Desktop.
Create a virtual directory that is mapped to a UNC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $virtualPath = "vdir" | |
| $physicalPath = "\\server\share" | |
| $parentSite = "Default Website" | |
| $username = "share_user" | |
| $password = "share_password" | |
| Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue | |
| Import-Module WebAdministration -ErrorAction SilentlyContinue | |
| Write-Host "Getting web site $parentSite." | |
| $site = Get-Website -name $parentSite | |
| if (!$site) { | |
| throw "The web site '$parentSite' does not exist. Please create the site first." | |
| } | |
| Write-Host "Determining whether virtual directory already exists." | |
| $existing = Get-WebVirtualDirectory -site $parentSite -Name $virtualPath | |
| if (-not $existing) { | |
| Write-Host "Creating virtual directory '$virtualPath'" | |
| New-Item IIS:\Sites\$parentSite\$virtualPath -type VirtualDirectory -physicalPath $physicalPath | |
| Write-Host "Virtual directory created successfully." | |
| } | |
| else { | |
| Write-Host "Virtual directory '$virtualPath' already exists. Moving on..." | |
| } | |
| if ($username -ne $null) { | |
| Write-Host "Setting credentials on virtual directory for $username" | |
| Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='$parentSite']/application[@path='/']/virtualDirectory[@path='$virtualPath']" -name username -Value $username | |
| Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='$parentSite']/application[@path='/']/virtualDirectory[@path='$virtualPath']" -Name password -Value $password | |
| Write-Host "Virtual directory credentials set successfully." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment