且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Powershell更改远程计算机上IIS网站的物理路径

更新时间:2022-12-24 21:42:35

root / WebAdministration WMI 提供程序的问题在于它的功能不是很丰富。

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

您可以做的是使用 Microsoft.Web.Administration 托管API。如果在服务器本身上运行,该脚本将起作用。

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)

不幸的是,MS没有想到提供提供凭证的方法。这意味着运行脚本的帐户将需要在远程服务器上授予的所有正确权限。我现在无法尝试这个,因为我不在AD环境附近。

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 (/).

有关IIS7配置的更多信息,请参阅以下链接:

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


IIS7配置参考> system.applicationHost