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

后端 未结 3 573
慢半拍i
慢半拍i 2021-02-01 16:38

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 remot

3条回答
  •  感动是毒
    2021-02-01 17:11

    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

提交回复
热议问题