PSWebDeploy: Website deployment with Web Deploy and PowerShell

The PowerShell module PSWebDeploy is a wrapper on top of Web Deploy (formerly MSDeploy) that allows you to automate website deployment.

One of the most frequent tasks a DevOps professional must perform is website deployment. Reliably being able to take a package or a set of files from a source (probably a software-build location) and send them up to a web server for use is a required set of tasks. Developers perform multiple builds per day, and they're going to want to test every build. The only way to perform that testing is to deliver the package to the web server.

Website deployments are great candidates for automation because they're tasks typically done frequently and done the same way every time. In the Windows/IIS world, a familiar tool to many developers is MSDeploy or what's now called Web Deploy.

Web Deploy is a tool Microsoft built to perform deployments, backups, and synchronizations to websites hosted on IIS. Web Deploy has been a de facto tool for website deployment for many years, but it's also been one of the most convoluted command-line tools to use because of its hard-to-understand syntax.

For example, here's a typical Web Deploy command to transfer the contents of C:\Folder to the acmewebsite web site hosted in Azure. This isn't too intuitive to say the least.

The syntax gets even more complex if you need to perform other actions. The perfect candidate for a PowerShell tool is when such a usual command-line tool like Web Deploy exists but uses a complex command-line syntax. This is where the PSWebDeploy tool came from. This PowerShell module serves as a wrapper on top of the Web Deploy command-line tool that makes it easier to work with Web Deploy.

To download it, run Install-Module PSWebDeploy. It's available in the PowerShell Gallery. Once installed on your system, this module, at this time, has one main command: Sync-WebSite. This command takes a source folder, a target path, and a computer name. After receiving all parameters, it will then use the Web Deploy tool to sync all local files with a remote web server.

To demonstrate, I've set up an Azure web app. This web app has deployment credentials set already and is ready to go. However, you don't need to use an Azure web app. You are free to use whatever IIS instance you'd like.

At its most basic level, the same task above will now look like this:

Sync Website output

Sync Website output

Now that Microsoft has wrapped up Web Deploy with PowerShell, you can easily introduce this command in various scripts and also pass objects directly to it.

If you look inside the Sync-Website cmdlet, you'll see the NewMsDeployCliArgumentString command does the hard work. This helper function parses through all of that text to provide a more PowerShell-like interface. For example, here's part of the function. It's not pretty, but it's necessary.

One important aspect of automating anything is building a layer of abstraction as we've done here. I've figured out Web Deploy's "rules" and how it takes parameters. I've then codified these rules. After this, we'll never have to worry about that ugly Web Deploy syntax again!

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
2 Comments
  1. simone 3 years ago

    wonderful! Unfortunately the $credential param is always required, even in envs in which the current user has enough permissions to access the webservice

     

    1+

  2. Author

    I'd love a pull request on Github to make improvements. https://github.com/adbertram/PSWebDeploy

    2+

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account