Examples of Windows PowerShell commands

 

Applies To: Dynamics CRM 2013

Before you can try these examples, you must set up the Microsoft Dynamics CRM Windows PowerShell cmdlets. More information: Administer the deployment using Windows PowerShell

In This Topic

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

Example: Disable product updates for new organizations

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> Get-CrmSetting TraceSettings
CallStack     : TrueCategories    : *:ErrorDirectory     : c:\crmdrop\logsEnabled       : FalseFileSize      : 10ExtensionData : 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     : TrueCategories    : *:ErrorDirectory     : c:\crmdrop\logsEnabled       : TrueFileSize      : 10ExtensionData : 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> $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
CallStack     : TrueCategories    : *:ErrorDirectory     : c:\crmdrop\logsEnabled       : FalseFileSize      : 10ExtensionData : System.Runtime.Serialization.ExtensionDataObject

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> 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> 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> Edit-CrmOrganization -Name alpineskihouse -DisplayName “Alpine Ski House Inc”

Example: Disable product updates for new organizations

The products updates are installed by default in all new organizations. For on-premises deployments, you can disable this behavior. Any new organizations created after the OptinProvisioningEnabled setting is set to 0 won’t have product updates installed. The following example shows how to use Windows PowerShell to accomplish this. You can modify this script to set the OptinProvisioningEnabled value to 1 and then run it again to re-enable product updates for new organizations.

$itemSetting = new-object 'System.Collections.Generic.KeyValuePair[String,Object]'("OptinProvisioningEnabled",0) 
$configEntity = New-Object "Microsoft.Xrm.Sdk.Deployment.ConfigurationEntity" 
$configEntity.LogicalName="Deployment" 
$configEntity.Attributes = New-Object "Microsoft.Xrm.Sdk.Deployment.AttributeCollection" 
$configEntity.Attributes.Add($itemSetting) 
Set-CrmAdvancedSetting -Entity $configEntity

See Also

Administer the deployment using Windows PowerShell
Get and set deployment configuration settings
Add a deployment administrator (PowerShell)
Create an organization (PowerShell)
Configure web address settings (PowerShell)
Configure IFD settings (PowerShell)
Configure claims settings (PowerShell)
Delete a deployment administrator

© 2016 Microsoft Corporation. All rights reserved. Copyright