Changing the physical path of an IIS website on a

2019-03-11 02:42发布

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.

3条回答
forever°为你锁心
2楼-- · 2019-03-11 03:15

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)

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-11 03:18

I'd like to build on top of @Kev's post.

You can use his method locally, but as he says there's no real way of providing credentials for his commented-out method of remotely connecting:

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

To change the physical path remotely, with credentials, use the following:

#configure your remote credentials
$computerName = "remotehostname"
$securePassword = ConvertTo-SecureString "password" -AsPlainText -force
$credential = New-Object System.Management.Automation.PsCredential("username", $securePassword)

#remove –SkipCACheck –SkipCNCheck –SkipRevocationCheck if you don't have any SSL problems when connecting
$options = New-PSSessionOption –SkipCACheck –SkipCNCheck –SkipRevocationCheck
$session = New-PSSession -ComputerName $computerName -Authentication Basic -Credential $credential -UseSSL -SessionOption $options

$block = {
    [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
    $site = $serverManager.Sites | where { $_.Name -eq $siteName }
    $rootApp = $site.Applications | where { $_.Path -eq "/" }
    $rootVdir = $rootApp.VirtualDirectories | where { $_.Path -eq "/" }
    $rootVdir.PhysicalPath = $newPath
    $serverManager.CommitChanges()
}

#run the code in $block on your remote server via the $session var
Invoke-Command -Session $session -ScriptBlock $block

Note: For remote PowerShell scripting, ensure TCP ports 5985 and 5986 are open outbound on your local network and inbound on your remote server.

查看更多
Luminary・发光体
4楼-- · 2019-03-11 03:32

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

查看更多
登录 后发表回答