Sample: Create an Organization (PowerShell)

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download:

SampleCode\PS1\CreateOrg.ps1

Requirements

To set up the Microsoft Dynamics CRM PowerShell cmdlets, see Use PowerShell to Call the Deployment Web Service.

Demonstrates

The following script will create an organization - partner-hosted (IFD). You must specify the following parameters:

DisplayName

SqlServerName

SrsUrl

For information about these parameters, see the BeginCreateOrganizationRequest message.

Example

param
(
    #required params
    [string]$DisplayName = $(throw "DisplayName parameter not specified"),
    [string]$SqlServerName = $env:COMPUTERNAME,
    [string]$SrsUrl = "http://$SqlServerName/reportserver",
    
    #optional params (can accept nulls)
    [string]$Name,
    [string]$BaseCurrencyCode,
    [string]$BaseCurrencyName,
    [string]$BaseCurrencySymbol,
    [string]$BaseCurrencyPrecision,
    [string]$BaseLanguageCode,
    [string]$SqlCollation,
    [string]$SQMOptIn,
    [string]$SysAdminName,
    [switch]$WaitForJob
)

$RemoveSnapInWhenDone = $False

if (-not (Get-PSSnapin -Name Microsoft.Crm.PowerShell -ErrorAction SilentlyContinue))
{
    Add-PSSnapin Microsoft.Crm.PowerShell
    $RemoveSnapInWhenDone = $True
}


$opId = New-CrmOrganization -DisplayName $DisplayName -SqlServerName $SqlServerName -SrsUrl $SrsUrl -Name $Name -BaseCurrencyCode $BaseCurrencyCode -BaseCurrencyName $BaseCurrencyName -BaseCurrencySymbol $BaseCurrencySymbol -BaseCurrencyPrecision $BaseCurrencyPrecision -BaseLanguageCode $BaseLanguageCode -SqlCollation $SqlCollation -SQMOptIn $SQMOptIn -SysAdminName $SysAdminName
$opId

if($WaitForJob)
{
    $opstatus = Get-CrmOperationStatus -OperationId $opid
    while($opstatus.State -eq "Processing")
    {
        Write-Host [(Get-Date)] Processing...
        Start-Sleep -s 30
        $opstatus = Get-CrmOperationStatus -OperationId $opid
    }

    if($opstatus.State -eq "Failed")
    {
        Throw ($opstatus.ProcessingError.Message)
    }

    Write-Host [(Get-Date)] Create org completed successfully.
}
else
{
    Write-Host [(Get-Date)] Create org async job requested.
}

if($RemoveSnapInWhenDone)
{
    Remove-PSSnapin Microsoft.Crm.PowerShell
}

See Also

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

Community Additions

ADD
Show: