Any time I use the Import-CrmDataFile cmdlet, I get an "Object reference not set to an instance of an object." error message (NullReferenceException). This includes PowerShell on my own machine as well inside of Azure DevOps Pipelines. However, the exact same code runs successfully in PowerShell ISE.
Btw, Export-CrmDataFile works in all environments without issue.
Wondering if anybody else is facing this issue. I put together this test script that can be tested by anyone in any environment.
$VerbosePreference = 'Continue'
$InformationPreference = 'Continue'
# check for required variables
if (-not ($env:CrmUrl -and $env:CrmUser -and $env:CrmPassword -and $env:CrmDataFile)) {
throw 'Must set environment variables: CrmUrl, CrmUser, CrmPassword, CrmDataFile'
}
# for check to see if current user is assigned the System Administrator security role
$sysAdminQuery = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="role">
<filter type="and">
<condition attribute="name" operator="eq" value="System Administrator" />
</filter>
<link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true">
<link-entity name="systemuser" from="systemuserid" to="systemuserid">
<filter type="and">
<condition attribute="systemuserid" operator="eq-userid" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>'
# install ps modules
(Get-Module -Name 'Microsoft.Xrm.Tooling.CrmConnector.PowerShell' -ListAvailable) -or (Install-Module -Name 'Microsoft.Xrm.Tooling.CrmConnector.PowerShell' -Force) | Out-Null
(Get-Module -Name 'Microsoft.Xrm.Tooling.ConfigurationMigration' -ListAvailable) -or (Install-Module -Name 'Microsoft.Xrm.Tooling.ConfigurationMigration' -Force) | Out-Null
Import-Module -Name 'Microsoft.Xrm.Tooling.CrmConnector.PowerShell'
Import-Module -Name 'Microsoft.Xrm.Tooling.ConfigurationMigration'
# test connection
$Connection = Get-CrmConnection -ConnectionString "AuthType=ClientSecret; Url=$env:CrmUrl; ClientId=$env:CrmUser; ClientSecret=$env:CrmPassword"
Write-Host "My user ID is: $($Connection.GetMyCrmUserId())"
Write-Host "I am system administrator: $($Connection.GetEntityDataByFetchSearch($sysAdminQuery) -ne $null)"
Write-Host "Data file: $([System.IO.FileInfo]::new($env:CrmDataFile).Length) bytes"
# import data
Write-Host 'Importing data file...'
Import-CrmDataFile -CrmConnection $Connection -DataFile $env:CrmDataFile -EmitLogToConsole
The output looks like this: