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

发布于 2024-09-28 05:16:55 字数 424 浏览 4 评论 0原文

我目前正在开发一个部署脚本,它将获取我的网站,从 svn 导出它,删除其中的任何测试文件等,缩小 javascript/css,将代码复制到远程 Web 服务器,然后切换将现有站点复制到新目录。

到目前为止,除了切换 IIS 中的物理目录之外,一切都正常。

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

当我查看我拥有的值时,我找不到物理路径属性。

任何建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-10-05 05:16:55

这也有效:(

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

注意使用反引号来继续行)

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)

回眸一遍 2024-10-05 05:16:55

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

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

[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()

您会注意到,如果您需要远程执行此操作,有一条注释掉的行可能对您有用:

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

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

脚本本身将更新站点根物理路径 (/)。

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

IIS7 配置参考>系统.applicationHost

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

花开浅夏 2024-10-05 05:16:55

我想以@Kev 的帖子为基础。

您可以本地使用他的方法,但正如他所说,没有真正的方法可以为他的注释掉的远程连接方法提供凭据:

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

要使用凭据远程更改物理路径,请使用以下命令:

#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

注意:对于远程 PowerShell 脚本编写,请确保 TCP 端口 5985 和
5986 在您的本地网络上开放
出站,并在您的网络上开放入站
远程服务器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文