Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the physical path of an IIS website on a remote machine via Powershell

I'm currently working on a deployment script that will take my site, export it from svn, remove any testing files etc in it, minify the javascript/css, copy the code to a remote web server, and then switch the physical path of the existing site to the new directory.

So far I have everything working except for switching the physical directory in IIS.

$IIsServer = Get-WmiObject Site -Namespace "root/WebAdministration" -ComputerName $serverIP -Credential $credentials -Authentication PacketPrivacy $site = $IIsServer | Where-Object {$_.Name -eq $siteName} 

When I look into the values I have I cant find the physical path property.

Any suggestions would be greatly appreciated.

like image 723
Ryan French Avatar asked Oct 14 '10 22:10

Ryan French


People also ask

What is the physical path of a website in IIS?

Website1 is just a site, without any applications or virtual directories, but that’s just IIS Manager making life easy for us again. There’s actually an application and a virtual directory, they just use "/" as the path. The physical path for all these things is actually defined by the virtual directory.

Does changing the physical path from IIS Manager resolve errorneous path?

EDIT: We have identified that a start/stop of the site (NOT the application pool) in IIS Manager resolves the errorneous physical path, but if I stop the site using appcmd, change physical path, and then start it, I still suffer from the same issues. I'm at a blank... Does changing the physical path from IIS Manager work correctly and immediately?

How to remove a site from IIS using PowerShell?

To remove a site, use the Remove-IISSite cmdlet. Removing an IIS site can be a dangerous undertaking especially if you’re removing a site on IIS that’s hosting other production websites. To verify you’re removing the right site, it’s recommend to use the common WhatIf PowerShell parameter.

How do I install the IIS administration module using PowerShell?

Go ahead and open up an elevated PowerShell console on your web server and install the module using the command below. PS> Install-Module -Name 'IISAdministration' If you do not have internet access, you can download the module to another host and copy the module to your modules directory.


2 Answers

This also works:

PS IIS:\Sites> Set-ItemProperty IIS:\Sites\Staging `                    -name physicalPath `                    -value "C:\blah\Web" 

(Note the use of backticks for line continuations)

like image 147
Doug Avatar answered Sep 21 '22 23:09

Doug


The problem with the root/WebAdministration WMI provider is that it's not very feature rich.

What you can do is use the Microsoft.Web.Administration managed API instead. This script will work if run on the server itself.

[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")  $siteName = "Default Web Site" $serverIP = "your ip address" $newPath = "your new path"  $serverManager = New-Object Microsoft.Web.Administration.ServerManager ## $serverManager = [Microsoft.Web.Administration.ServerManager]::OpenRemote($serverIP) $site = $serverManager.Sites | where { $_.Name -eq $siteName } $rootApp = $site.Applications | where { $_.Path -eq "/" } $rootVdir = $rootApp.VirtualDirectories | where { $_.Path -eq "/" } $rootVdir.PhysicalPath = $newPath $serverManager.CommitChanges() 

You'll notice there's a commented out line which might work for you if you need to do this remotely:

## $serverManager = [Microsoft.Web.Administration.ServerManager]::OpenRemote($serverIP) 

Unfortunately MS didn't think to provide a way to supply credentials. This would mean that the account running the script would need all the right permissions granted on the remote server. I can't try this right now because I'm not near an AD environment.

The script itself will update the site root physical path (/).

For more info about IIS7's configuration see the following link:

IIS7 Configuration Reference > system.applicationHost

like image 42
Kev Avatar answered Sep 18 '22 23:09

Kev