Use PowerShell to Call the Deployment Web Service

[Applies to: Microsoft Dynamics CRM 2011]

Windows PowerShell enables you to easily run deployment commands without writing code using the Deployment Web service. To execute commands, you must first configure Microsoft Dynamics CRM 2011 with Windows PowerShell. This topic describes how to do this and explains each of the supported commands.

In This Topic

Configure the Microsoft Dynamics CRM PowerShell cmdlets

Supported PowerShell cmdlets

Example – Enable Tracing

Example – Disable Tracing

Example – Retrieve Validation Errors

Example – Create an Organization

Example – Disable or Enable an Organization

Example – Edit the Attributes of an Organization

Configure the Microsoft Dynamics CRM PowerShell cmdlets

Register the cmdlets

  1. Log into the administrator account on your Microsoft Dynamics CRM server.

  2. In a Windows PowerShell window, enter the following command:

    Add-PSSnapin Microsoft.Crm.PowerShell
    

    This command adds the Microsoft Dynamics CRM Windows PowerShell snap-in to the current session. The snap-in is registered during installation and setup of the Microsoft Dynamics CRM server.

Supported PowerShell cmdlets

The following table lists the cmdlets available for use with Windows PowerShell.

Cmdlet

Description

Disable-CrmServer

Disables the specified Microsoft Dynamics CRM server.

Disable-CrmOrganization

Disables the specified Microsoft Dynamics CRM organization.

Edit-CrmOrganization

Edits properties of the specified Microsoft Dynamics CRM organization.

Enable-CrmServer

Enables the specified Microsoft Dynamics CRM server.

Enable-CrmOrganization

Enables the specified Microsoft Dynamics CRM organization.

Get-CrmOrganization

Retrieves one or all of the organizations in the deployment of Microsoft Dynamics CRM.

Get-CrmOperationStatus

Retrieves the status on asynchronous operations sitting in the deferred operation queue for Microsoft Dynamics CRM.

Get-CrmSetting

Retrieves a Microsoft Dynamics CRM deployment setting object. For a list of the settings objects, see Deployment Entities and Deployment Configuration Settings. For sample scripts, refer to the See Also links later in this topic.

Get-CrmServer

Retrieves a server object for one or all Microsoft Dynamics CRM servers in a deployment.

Get-CrmLicenseProperty

Retrieves the license properties object for Microsoft Dynamics CRM.

Get-CrmAdvancedSetting

Gets an advanced setting value for Microsoft Dynamics CRM. The metadata specify that the setting is readable in order to retrieve the specified value. For more information, see Use PowerShell with Advanced Settings.

Get-CrmAccessLicense

Retrieves the server licensing and CAL licensing information for the deployment of Microsoft Dynamics CRM.

Get-CrmDeploymentAdministrator

Retrieves the deployment administrators for the Microsoft Dynamics CRM deployment.

Get-CrmCertificate

Retrieves the certificate information for Microsoft Dynamics CRM. The certificate object contains the public key of the service bus signing certificate.

Import-CrmOrganization

Initiates the process to import a Microsoft Dynamics CRM organization database into the deployment.

New-CrmOrganization

Initiates the process to create a new organization in the Microsoft Dynamics CRM deployment. For more information, see Sample: Create an Organization (PowerShell).

New-CrmDeploymentAdministrator

Creates a new deployment administrator for the Microsoft Dynamics CRM deployment. For more information, see Sample: Add a Deployment Administrator (PowerShell).

Remove-CrmOrganization

Deletes the specified organization from the Microsoft Dynamics CRM deployment.

Remove-CrmServer

Deletes the specified server from the Microsoft Dynamics CRM deployment.

Remove-CrmCertificate

Removes the specified certificate from Microsoft Dynamics CRM.

Remove-CrmDeploymentAdministrator

Removes the specified deployment administrator from the Microsoft Dynamics CRM deployment.

Set-CrmProductKey

Sets the product key for the Microsoft Dynamics CRM deployment.

Set-CrmSetting

Sets the specified Microsoft Dynamics CRM deployment wide settings. For a list of the settings objects, see Deployment Entities and Deployment Configuration Settings. For sample scripts, refer to the See Also links later in this topic.

Set-CrmAdvancedSetting

Sets an advanced setting value for Microsoft Dynamics CRM. The metadata specify that the setting is writable in order to set the specified value. For more information, see Use PowerShell with Advanced Settings.

Set-CrmCertificate

Creates a certificate record or sets the properties of an existing certificate record in the Microsoft Dynamics CRM configuration database.

Update-CrmOrganization

Updates the specified Microsoft Dynamics CRM organization with the latest software updates or upgrades the organization from Microsoft Dynamics CRM 4.0 to Microsoft Dynamics CRM 2011.

Example – Enable Tracing

The following example shows how to use Windows PowerShell to enable tracing. This pattern can be used for all settings. For a list of the settings objects, see Deployment Entities and Deployment Configuration Settings.

PS C:\Users\Administrator> Add-PSSnapin Microsoft.Crm.PowerShell
PS C:\Users\Administrator> Get-CrmSetting TraceSettings
CallStack     : True
Categories    : *:Error
Directory     : c:\crmdrop\logs
Enabled       : False
FileSize      : 10
ExtensionData : System.Runtime.Serialization.ExtensionDataObject
PS C:\Users\Administrator> $setting = Get-CrmSetting TraceSettings
PS C:\Users\Administrator> $setting.Enabled=$True
PS C:\Users\Administrator> Set-CrmSetting $setting
PS C:\Users\Administrator> Get-CrmSetting TraceSettings
CallStack     : True
Categories    : *:Error
Directory     : c:\crmdrop\logs
Enabled       : True
FileSize      : 10
ExtensionData : System.Runtime.Serialization.ExtensionDataObject

Example – Disable Tracing

The following example shows how to use Windows PowerShell to disable tracing. This pattern can be used for all settings. For a list of the settings objects, see Deployment Entities and Deployment Configuration Settings.

PS C:\Users\Administrator> Add-PSSnapin Microsoft.Crm.PowerShell
PS C:\Users\Administrator> Get-CrmSetting TraceSettings
PS C:\Users\Administrator> $setting = Get-CrmSetting TraceSettings
PS C:\Users\Administrator> $setting.Enabled=$False
PS C:\Users\Administrator> Set-CrmSetting $setting
PS C:\Users\Administrator> Get-CrmSetting TraceSettings

Example – Retrieve Validation Errors

A recommended best practice when using Windows PowerShell commands is to add error handling at the end of each Windows PowerShell cmdlet. The following example shows how to do this.

trap [Exception]
{
    echo $("Error| " + $_.Exception)
    
    echo $("Error| Stacktrace: " + $_.Exception.Stacktrace)
    
    if($_.Exception.GetType() -eq [System.ServiceModel.FaultException[Microsoft.Xrm.Sdk.Deployment.DeploymentServiceFault]])
    {
        echo $("Error| Details: ")
        foreach ($edwError in $_.Exception.Detail.ErrorDetails)
        {
            echo $($edwError.Key + ": " + $edwError.Value)
        }
    }
    
    break
}

Alternatively, you can access the built-in Windows PowerShell $error variable. However, you should know that errors are placed on a stack. If any other command (Microsoft Dynamics CRM or otherwise) runs immediately after the Microsoft Dynamics CRM cmdlet and throws an exception, the top item in $error will be pushed down. If the Microsoft Dynamics CRM cmdlet exception was the last one thrown in the shell you can access the error details as shown below.

$error[0].Exception.Detail.ErrorDetails

Example – Create an Organization

The following example shows how to use Windows PowerShell to create an organization. Although the GUID of the job is returned upon successful submission of the request, the actual creation process may take a significant amount of time.

PS C:\Users\Administrator> Add-PSSnapin Microsoft.Crm.PowerShell
PS C:\Users\Administrator> New-CrmOrganization -DisplayName "Alpine Ski House" -SQLServerName "CRMSQL" -SrsUrl "http://CRMSQL/ReportServer" -Name "alpineskihouse" -BaseCurrencyCode "USD" -BaseCurrencyName "US Dollar" -BaseCurrencySymbol "$" -BaseCurrencyPrecision "2" -BaseLanguageCode 1033 -SqlCollation "Latin1_General_CI_AI" -SQMOptIn false

Example – Disable or Enable an Organization

The following example shows how to use Windows PowerShell to disable or enable an organization. It is a best practice to disable an organization when you perform database maintenance. When you disable an organization, users will no longer be able to access the Microsoft Dynamics CRM application for the organization. To make it available to users again, you must enable it.

PS C:\Users\Administrator> Add-PSSnapin Microsoft.Crm.PowerShell
PS C:\Users\Administrator> Disable-CrmOrganization -Name alpineskihouse
PS C:\Users\Administrator> Enable-CrmOrganization -Name alpineskihouse

Example – Edit the Attributes of an Organization

The following example shows how to use Windows PowerShell to edit the attributes for an organization.

PS C:\Users\Administrator> Add-PSSnapin Microsoft.Crm.PowerShell
PS C:\Users\Administrator> Disable-CrmOrganization -Name alpineskihouse
PS C:\Users\Administrator> Edit-CrmOrganization -Name alpineskihouse -DisplayName “Alpine Ski House Inc”
PS C:\Users\Administrator> Enable-CrmOrganization -Name alpineskihouse

See Also

Tasks

Sample: Add a Deployment Administrator (PowerShell)
Sample: Configure Claims (PowerShell)
Sample: Configure IFD (PowerShell)
Sample: Configure Web Address Settings (PowerShell)
Sample: Create an Organization (PowerShell)

Reference

Enable or Disable Custom Code Execution
Enable or Disable XAML Workflows

Concepts

Introduction to the Deployment Web Service

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.