Sunday, February 05, 2012

Categories

Archive

Tag Cloud

Recent Comments

"FYI - This does not work with "set-psdebug -strict". This setting requires all variables be defined before they can be referenced, the equivalent of "option explicit" from vbscript. " Read more
by Bill Faulk on Export CRM customizations using PowerShell

"Hi Molly -- this kind of stuff can be dicey to troubleshoot. Make sure the workflow scope is set to Organization and not User and that it is published. Sharing is not required. Then make sure their role can execute workflows. Hope this helps, Phil" Read more
by Phil Edry on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"Phil, I spoke too soon. I was able to get everything set up. I was testing this with out inside sales group and when they try to run the workflow they receive an error saying they do not have permission to run the workflow. I went back and made sure they had rights to the sync entity and the workflow. I shared the workflow with them (I believe that is what I should do), but they still cannot run it. What else could I be missing? Thanks in advance. Molly" Read more
by Molly McGill on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"Glad I could help!" Read more
by Phil Edry on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"Thanks Phil! That worked. I knew it was something simple that I was just overlooking." Read more
by Molly McGill on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"Hi Molly -- open a role that the salesman has and click on the very last tab "Custom Entities". Make sure he has rights on the Sync User entity. Best, Phil" Read more
by Phil Edry on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"I think this is really going to help us. I am having a problem configuring outlook for one of our salesman and I cannot get "Sync Users (contact)" to even show up. I'm sure I'm missing something in their role. Do you know what should be changed to allow them to see that?" Read more
by Molly McGill on Allow Multiple Users to Sync the Same Contact to Outlook Effortlessly with Microsoft CRM

"hi, good staff.I think calender is the most important aspects of any crm solution.It help to update the information about the events and also helpful to for modification of transactions and easily approachable medium " Read more
by real estate crm on Adding a Custom Calendar to the CRM UI

Altriva Team Blog

Export CRM customizations using PowerShell

Posted by: Tim Dutcher on 6/10/2009
  • Categories:
  • CRM

Microsoft’s “PowerShell” is a command-line shell and scripting language designed for system administration and automation. Its potential benefits to developers and administrators of CRM installations are endless. Since PowerShell can utilize .NET assemblies and web services, it can be used with CRM to automate just about any administration tasks or any other type of routine or tedious tasks.

This blog post provides one example of how PowerShell can make the lives of Microsoft Dynamics CRM 4.0 administrators easier. The script provided below fully automates the process of exporting CRM system customizations to file, which can be scheduled to run at any time interval. It also takes care of creating a unique file name for each export and only keeping customization files that differ from the previous export – thus saving hard drive space.
There are several books and websites dedicated to PowerShell. One site that we’ve found useful is PowerShell.com. Or Bing “powershell” and you’ll find millions of hits. Enjoy!
#######################################################################
##
## PowerShell v1.0 Script: CrmCustomizationExporter.ps1
##
## Purpose: Exports (to an XML file) all CRM 4.0 customizations for the specified organization.
##
## Features:
##   1) Automatically maintains unique customization files. This reduces storage space and makes it easier to
##      determine if and when customization were made between exports.
##   2) Reads directly from the CRM WSDL so no local storage of the WSDL (or compiled DLL) is necessary.
##
## Requirements:
##   1) .NET Framework 2.0 SDK
##   2) Run script with an account that has rights to export CRM customizations
##   3) Set the PowerShell execution policy: Set-ExecutionPolicy Unrestricted     (change as appropriate)
##
## Creating a scheduled task:
##   - Command to run (example): PowerShell "& c:\ps\CrmCustomizationExporter.ps1"
##
#######################################################################
 
## Modify these variables as needed.
$targetDir = "c:\customizations\"
$filePrefix = "customizations_all_"
$crmOrgName = "altriva"
$crmServiceUrl = "http://localhost:5555/mscrmservices/2007/crmservice.asmx"
$wsdlLocation = "$crmServiceUrl" + "?wsdl&uniquename=$crmOrgName"   # This could also be a fixed file name.
$crmServiceUrl = "$crmServiceUrl" + "?uniquename=$crmOrgName"
 
write-host $wsdlLocation
 
## Open the WSDL.
[void] [Reflection.Assembly]::LoadWithPartialName("System.Web.Services")
$wc = New-Object System.Net.WebClient
$wc.UseDefaultCredentials = $true
$wsdlStream = $wc.OpenRead($wsdlLocation)
$serviceDescription = [Web.Services.Description.ServiceDescription]::Read($wsdlStream)
$wsdlStream.Close()
 
## Import the web service into a CodeDom.
$serviceNamespace = New-Object System.CodeDom.CodeNamespace
$codeCompileUnit = New-Object System.CodeDom.CodeCompileUnit
$serviceDescriptionImporter = New-Object Web.Services.Description.ServiceDescriptionImporter
$serviceDescriptionImporter.AddServiceDescription($serviceDescription, $null, $null)
[void] $codeCompileUnit.Namespaces.Add($serviceNamespace)
[void] $serviceDescriptionImporter.Import($serviceNamespace, $codeCompileUnit)
 
## Generate the code from that CodeDom into a string.
$generatedCode = New-Object Text.StringBuilder
$stringWriter = New-Object IO.StringWriter $generatedCode
$provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$provider.GenerateCodeFromCompileUnit($codeCompileUnit, $stringWriter, $null)
 
## Compile the source code.
$references = @("System.dll", "System.Web.Services.dll", "System.Xml.dll")
$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
$compilerParameters.ReferencedAssemblies.AddRange($references)
$compilerParameters.GenerateInMemory = $true
$compilerResults = $provider.CompileAssemblyFromSource($compilerParameters, $generatedCode)
 
## Get the assembly that we just compiled.
$assembly = $compilerResults.CompiledAssembly
 
## Create an instance of the CrmService type.
$type = "CrmService"
$instance = $assembly.CreateInstance($type)
 
## Set instance properties.
$instance.UseDefaultCredentials = $true
$token = new-object CrmAuthenticationToken
$tokenAuthenticationType=0
$Token.OrganizationName = $crmOrgName
$instance.Url = $crmServiceUrl
$instance.CrmAuthenticationTokenValue = $token
$CrmCredentials = [System.Net.CredentialCache].DefaultCredentials
 
## Execute the customization export request
$request = new-object "ExportAllXmlRequest"
$response = $instance.Execute($request) -as [ExportAllXmlResponse]
$responseXml = new-object XML
$responseXml.LoadXml($response.ExportXml)
 
## Write the exported customizations XML to a date-stamped file.
$date = (get-date).ToString('yyyyMMdd_HHmmss')
$xmlfile = "$targetDir$filePrefix$date.xml"
$responseXml.Save("$xmlfile")
 
## If the previous export file is the same size as the file just created then delete the previous version.
$XmlFiles = Get-ChildItem "$targetDir*.xml" | Sort-Object -Descending
if ($XmlFiles.count -gt 1)
{
 $previousXmlFile = $XmlFiles[1]
 $previousXmlFileSize = (Get-Item $previousXmlFile).length
 write-host $previousXmlFileSize
 if ($previousXmlFileSize -eq (Get-Item $xmlfile).length) { Remove-Item $previousXmlFile }
}
 
- Tim Dutcher

 

Create a trackback from your own site.

2 Comments


    • Apr 26 2011, 2:16 PM Bill Faulk
    • FYI - This does not work with "set-psdebug -strict". This setting requires all variables be defined before they can be referenced, the equivalent of "option explicit" from vbscript.

Leave A Comment



Please enter the CAPTCHA phrase above.



Copyright 2010 by Altriva LLC