Search code examples
powershellpowershell-3.0

using MsDeploy-Sync to copy only one file locally


Currently i am copying all the $DeploymentPath (folder) locally, and then deleting all the files in it, except 1 file that i want.

MsDeploy-Sync `
       -SourceContentPath:"$DeploymentPath" `
       -DestinationContentPath:"$SupportFolder/WebPages" `

Get-ChildItem "$SupportFolder\WebPages" -Exclude "web.config.js" |
  Remove-Item

What i want to do:

copy only 1 file web.config.js locally, if it doesn't exist there, return false.

  • i am writing this piece of code in ps1 file, but i have to use MsDeploy commands.

Is it possible?


Solution

  • First of all, you should use the Join-Path cmdlet to combine a path in PowerShell.

    To check whether the file exist, just use the Test-Path cmdlet:

    $webConfigPath = Join-Path $SupportFolder '\WebPages\web.config.js'
    if (Test-Path $webConfigPath)
    {
         MsDeploy-Sync `
            -SourceContentPath (Join-Path $DeploymentPath 'web.config.js') `
            -DestinationContentPath (Join-Path $SupportFolder 'fromServer_web.config.js')
    }
    else
    {
        $false # return $false
    }