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

假装没事ソ 提交于 2019-12-02 20:22:15

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

Doug

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!