代码之家  ›  专栏  ›  技术社区  ›  Ryan French

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

  •  23
  • Ryan French  · 技术社区  · 14 年前

    到目前为止,除了在IIS中切换物理目录之外,其他一切都正常工作。

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

    如有任何建议,将不胜感激。

    3 回复  |  直到 14 年前
        1
  •  23
  •   erlando    13 年前

    问题在于 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)
    

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

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

    IIS7 Configuration Reference > system.applicationHost

        2
  •  48
  •   Roger Lipscombe    12 年前

    这也适用于:

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

    (请注意,行连续性使用反勾号)

        3
  •  2
  •   theyetiman    9 年前

    我想建在@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和 本地网络上的出站和 远程服务器。