Using MSDeploy In Powershell for Beta

Hello, my names is James and for my first blog I would like to answer some of the common questions I have seen when using MSDeploy in Powershell for the first time.  Also, I will try to introduce some tricks to help gather information about your server using the MSDeploy Powershell environment.

Q. What is the name of the Powershell snap-in, how do I see if it installed ?

A.  The name of the snap-in is "MSDeploysnapin".  At the PS command prompt you can use the Get-pssnapin -registered cmdlet to verify the snap-in is installed.  You can use add-pssnapin MSDeploysnapin at the PS command prompt to include the snap-in.

Q. MSDeploy installed on the machine and then I installed Powershell, but I don't see the snap-in as being installed?

A.  You have to install MS Deploy after you have installed Powershell on your server, the snap-in cannot be installed into the registry properly until Powershell is installed so it has to be done in this order.

Q.  What cmdlets are available in the MSDeploysnapin?

A.  The cmdlets have been finalized to Get-deploymentObject and Update-deploymentObject.

Q.  Are there any shortcuts I can use when typing cmdlets?

A.  If you use Get-help Get-deploymentObject -full or Get-help Update-deploymentObject -full you can see that the certain parameters have assigned default positions.  This allows shortcuts for the following two scenarios.

Get-deploymentObject -provider apphostconfig -path "Default Web Site"

to

Get-deploymentObject apphostconfig "Default Web Site"

And

(** The following has been updated as of 3/4/09) 

Update-deploymentObject $source <destinationProvider> <destinationPath>

 

Q.  I want to perform an Update but it isn't working, what's wrong?

(** The following has been updated as of 3/4/09)

A.  A common mistake here is to use dashes before the $source and $dest variables since this is how MSDeploy works on command-line.

Update-deploymentObject -$source <destinationProvider> <destinationPath>  ( incorrect use )

You can remove these dashes as they are incorrect syntax.

Update-deploymentObject $source <destinationProvider> <destinationPath>     ( correct use )

Another technique to troubleshoot these situations is to check your $source data first for validity and then perform the update again ( this can be done just by removing the $source =  and verifying an object is returned first ) .  In some scenarios checking the $dest object will only show an invalid object since it is null initially but MSDeploy allows this since it is necessary for updates.

Q.  Do I need to use the -migrate switch when migrating my server from IIS6 to IIS7?

A.  Short answer is no, you do not.  The migrate switch adds some additional rules that may be useful in a migration but it is not required.  Unlike the command line tool, you do not need to distinguish between migrations and sync when using Update-deploymentObject.

Q.  Do I need to install Powershell on every web server to use cmdlets on multiple servers remotely ?

A.  You only need to have the MS Deploy agent service called "msdepsvc" started on all other web servers.  Powershell and the MSDeploysnapin need only be installed on the operator machine. 

Q.  How can I get dependencies of my web server in a Powershell session ?

A.  A quick one-line for this would be:

( Get-deploymentObject apphostconfig ).Invoke("GetDependencies").Dependencies

Replace apphostconfig provider with webserver60 provider as needed for IIS6 servers.

Q.  How can I get general system information about my web server ?

A.  Another one-line for this would be:

( Get-DeploymentObject apphostconfig ).Invoke("GetSystemInfo").OuterXml

Replace apphostconfig provider with webserver60 provider as needed for IIS6 servers.

Q. Is there a way to get a list of application pools on a web server?

A. This is less obvious but there is a way.

( Get-deploymentObject apphostconfig ).Invoke("GetDependencies").AppPools

Replace apphostconfig provider with webserver60 provider as needed for IIS6 servers.

Q.  How do I send credentials to remote machines in Powershell?

A.  Credentials in Powershell use the -credential switch ( the direct input for this switch can be a credentials object or just the username ) and there is a way to pass both the username and password remotely by creating a credential object first and passing it as a parameter.  If your credential object only contains a username you will be prompted for a password.  The following example creates a deployment object from an IIS7 web-server remotely and stores it in the $server variable.  In this example, you would be prompted for a password directly after executing this cmdlet.

$server = Get-deploymentObject apphostconfig -computername <computername> -credential <domain\username>

Q.  I want to create a credential object with my password included, is there anyway to do this?

A.  The first step is to store your password to file, and typically you want to encrypt your password at the same time.  The easy way is at the Powershell command prompt use the following cmdlet.

Read-host -prompt password -assecurestring | convertfrom-securestring | out-file c:\encryptedpassword.txt

You will then be prompted with a "password:" prompt that you can enter your password.  This creates the file c:\encryptedpassword.txt.  Whenever you want to script your password and avoid prompts just have this file handy on the server submitting the credentials.  The next step is to create a secure string object with your password using the newly created file.

$secureStringObject = Get-content c:\encryptedpassword.txt | convertto-securestring

Finally, create the credentials object that you can use for the remainder of your Powershell session.

$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "<username>",$secureStringObject

Now use this credential object in your Get-deploymentObject and voila, you no longer have to type out your password to do remote operationsSimply use the $credentials object as the parameter for credential switch.

Get-deploymentObject apphostconfig -computername <computer> -credential $credentials

Q.  The command line tool for MSDeploy has a configuration file which I can add and configure rules I use for my web server, how can I do this in Powershell?

A.  While it is possible to create rules on the PS command prompt, there is currently no configuration file used in Powershell sessions.  This can be worked around by scripting common operations to script files and placing the rules there.

Q.  I stored my web server to a deployment object ($server = Get-deploymentObject apphostconfig ) and then made changes to my web server.  Why are my changes not showing up in deployment object?

(** The following has been updated as of 3/4/09)

A.  This has changed slightly from previous versions.  Your $server object will be more of a placeholder until it is used.  That is to say until an operation is performed such as an Update, GetDependencies or GetSystemInfo it is a hollow object.  Upon making the previously mentioned calls the object will then be created and validated at that time.  To force this validation simply do something like $server.Invoke("GetSystemInfo").OuterXml, if you get system information back then the object is valid in this case.

Q.  Is there a way I don't have to add the MSDeploysnapin manually to every Powershell session? 

A.  You can do this by starting a Powershell session and adding the MSDeploysnapin.  Then, export your console with the following cmdlet ( this works for Vista Powershell version 1.0 ).

Export-Console -path <directory\filename>

This will create a file with a psc1 extension ( might be different for other Powershell versions ).  Whatever shortcut you use to start Powershell, now add the following switch and argument.

Powershell.exe -PSConsoleFile c:\<filename>.psc1

This will load the MSDeploysnapin automatically and start your Powershell session.  You can also create the psc1 file manually if you only want the MSDeploysnapin by saving the below xml to a psc1 file.

<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
  <PSVersion>1.0</PSVersion>
  <PSSnapIns>
    <PSSnapIn Name="msdeploysnapin" />
  </PSSnapIns>
</PSConsoleFile>

That's it for my first Powershell FAQ of MSDeploy.  If there is anything else you would like to see in this blog, feel free to respond below and let me know.  In the future, I hope to provide a deeper look into deployment objects and the MSDeploysnapin cmdlets.

 

 

6 Comments

  • Great post! How do I use Powershell to synchronize all websites from a host machine to mulitple destination machines at the same time, similiar to what the following cmd lines in MSDeploy will do:
    msdeploy -verb:sync -source:webserver60 -dest:webserver60,computerName=xx.xxx.xx.01
    msdeploy -verb:sync -source:webserver60 -dest:webserver60,computerName=xx.xxx.xx.02
    msdeploy -verb:sync -source:webserver60 -dest:webserver60,computerName=xx.xxx.xx.03
    msdeploy -verb:sync -source:webserver60 -dest:webserver60,computerName=xx.xxx.xx.04

  • Unfortunately there isn't any extra functionality here compared to command line, it would be almost the same with maybe one exception being you can create the source object ahead of time and reuse it, so something like:

    $source = ( Get-deploymentobject webserver60 )
    Update-deploymentobject $source webserver60 -computername xx.xxx.xx.01
    Update-deploymentobject $source webserver60 -computername xx.xxx.xx.02
    Update-deploymentobject $source webserver60 -computername xx.xxx.xx.03
    Update-deploymentobject $source webserver60 -computername xx.xxx.xx.04

  • Works great!
    What is the proper syntax for using the -SKIP parameter in the Update-DeploymentObject statement? I do not want any .scc files to be copied to my destination servers.

  • I answered my own question and was able to skip files using -skip:objectName=dirpath,absolutePath=.*\.scc

    However, when I try to run this through a windows command prompt using powershell.exe -noexit d:\path\test.ps1, I get the following error message:

    The term 'Get-DeploymentObject' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

    I need to be able to schedule this .ps1 script through scheduled tasks. Please help!

    Thanks!

  • can you try just putting a add-pssnapin msdeploysnapin at the beginning of your ps1 script?

  • Hey Anonymous, yes the cmdlets got yanked from RTW, this blog is pretty much obsolete if you are using RTW. &nbsp;I started some blogs about making functions in powershell to make use of MSDeploy through APIs, but I hope to see MSDeploy snapin make a come back for future releases of MSDeploy.
    http://blogs.iis.net/jamescoo/archive/2009/09/09/cool-msdeploy-powershell-scripts.aspx
    http://blogs.iis.net/jamescoo/archive/2009/10/24/msdeploy-powershell-scripts-part-ii-exceptions-and-remote-server-syncs.aspx
    &nbsp;
    &nbsp;

Comments have been disabled for this content.