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

后端 未结 3 500
慢半拍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:10

    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.

提交回复
热议问题