Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

IIS: Change Physical Path for each Application under all the Sites

I am trying to change the Physical Path for each Virtual Directory under each Application and each Site in IIS through PowerShell.

For example, this command changes Physical Path (/) for one of the Virtual Directories (/) under Applications (MSMQ) under Site (Default Web Site).

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/MSMQ']/virtualDirectory[@path='/']" -name "physicalPath" -value "F:\inetpub\wwwroot\msmq"

Similarly, I want to change for all applications under all sites.

Referred to following links but, no luck.

https://serverfault.com/questions/610953/how-to-list-out-virtual-directories-in-iis-from-the-all-the-websites-in-powershell

https://community.idera.com/database-tools/powershell/ask_the_experts/f/learn_powershell_from_don_jones-24/22179/getting-list-of-all-app-pools-for-site-and-web-applications

Update:

I got it working with following code. Can this be done in a cleaner/optimized way?

C:\windows\system32\inetsrv\appcmd.exe list vdirs | ForEach-Object {
    $vDir =  $_|%{$_.split('"')[1]}
    $_ -match ".?\((.*?)\).*" | Out-Null
    $phyPath = $matches[1].Replace("physicalPath:","")
    Write-Output "VDIR is $vDir and Physical Path is $phyPath"
    $newPath=$phyPath.Replace("C:","F:")
    Write-Output "New Physical Path is $newPath"
    C:\windows\system32\inetsrv\appcmd.exe set vdir $vDir -physicalPath:$newPath
}

Comments