seanmcne / Microsoft.Xrm.Data.PowerShell

This module uses the CRM connection from Microsoft.Xrm.Tooling.CrmConnector.Powershell and provides common functions to create, delete, query, and update data as well as functions for common tasks such as publishing, and manipulating System & CRM User Settings, etc. The module should function for both Dynamics CRM Online and On-Premise environment.
203 stars 65 forks source link

Using this module inside an Powershell Job (Start-Job) #50

Closed thuld closed 8 years ago

thuld commented 8 years ago

I having an hard time getting this module work with PowerShell jobs. This problem is also mentioned here: http://stackoverflow.com/questions/35204785/start-job-and-psversion-error

I followed the advis to comment out this line in file "Microsoft.Xrm.Data.PowerShell.psd1"

# Minimum version of the Windows PowerShell host required by this module
#PowerShellHostVersion = '4.0'

But now I receive this error

Die Argumenttransformation für den Parameter "conn" kann nicht verarbeitet werden. 
Der Wert "Microsoft.Xrm.Tooling.Connector.CrmServiceClient" vom Typ 
"Deserialized.Microsoft.Xrm.Tooling.Connector.CrmServiceClient" kann nicht in den 
Typ "Microsoft.Xrm.Tooling.Connector.CrmServiceClient" konvertiert werden.
    + CategoryInfo          : InvalidData: (:) [Remove-CrmRecord], ParameterBindin. 
   ..mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Remove-CrmRecord
    + PSComputerName        : localhost

Does anyone know how to make this module work with jobs

kenakamu commented 8 years ago

Could you try to run in ScriptBlock?

thuld commented 8 years ago

The Code actually uses -ScriptBlock. Take a look at the code posted in the stack overflow question.

Von meinem Windows Phone gesendet


Von: Kenichiro Nakamuramailto:notifications@github.com Gesendet: ‎04.‎02.‎2016 19:57 An: seanmcne/Microsoft.Xrm.Data.PowerShellmailto:Microsoft.Xrm.Data.PowerShell@noreply.github.com Cc: Daniel Thulmailto:thuld@outlook.com Betreff: Re: [Microsoft.Xrm.Data.PowerShell] Using this module inside an Powershell Job (Start-Job) (#50)

Could you try to run in ScriptBlock?


Reply to this email directly or view it on GitHub: https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell/issues/50#issuecomment-180000858

kenakamu commented 8 years ago

Ah sorry about that. Does it make any difference if you create connection inside the block rather than passing as parameter? I had similar issue when I ran this in azure automation before

RedsGT commented 8 years ago

Hi @thuld. Going back your original issue, I'm trying to reproduce it to see if something needs to be modified in the module since it doesn't seem right that you'd have to comment out something for it to work.

The line you posted on SO to start the job isn't valid because it's missing the -PSVersion argument:

Start-job -PSVersion <missing arg> -Name $jobname -ScriptBlock $packageCmd -ArgumentList $jobName, $crmConnection, $p

Can you post a complete script where you can reproduce the issue so I we can look in to it?

thuld commented 8 years ago

Hell @RedsGT, I update the code on SO, the -PSVersion was definitly not correct. This is the code use:

`# this script is executed for each package $packageCmd = {

param($jobName, $crmConnection, $package)

# Xrm is not a module that is system-wide loaded 
Import-Module Microsoft.Xrm.Data.Powershell

$counter = 0

write $jobName

foreach($record in $package) {

    Remove-CrmRecord -conn $crmConnection -EntityLogicalName 'cdi_pageview' -Id $record.cdi_pageviewid
    $counter++
}

}

$packageNumber = 1;

write 'Starting jobs...'

foreach($p in $packages) {

$jobname = $jobName =  ('Del_Data_Pak_{0}' -f $packageNumber)
Start-job  -Name $jobname -ScriptBlock $packageCmd -ArgumentList $jobName, $crmDevConnection, $p

$packageNumber++

}

write 'Starting jobs completed'`

Based on the comments on SO do I need to create a connection object in each "packageCmd ". But this only works in case is modified the file "Microsoft.Xrm.Data.PowerShell.psd1" as described before.

thuld commented 8 years ago

I just found the feature "Runspaces" and in this video is mentioned that the "Deserialize" ("Deserialized.Microsoft.Xrm.Tooling.Connector.CrmServiceClient" v.s "Microsoft.Xrm.Tooling.Connector.CrmServiceClient") is not happing when using runspaces:

RedsGT commented 8 years ago

Hi @thuld.

Powershell jobs refuse to handle in-memory active objects for thread safe reasons. This isn't a bug in Microsoft.Xrm.Data.PowerShell but a limit on job handling. The solution (as you alluded to above) is to use Runspaces. Runspaces are an advanced topic and are not easy to set up. That being said, Boe Prox has created a wrapper module for RS over at http://learn-powershell.net/2015/03/31/introducing-poshrsjob-as-an-alternative-to-powershell-jobs/ that not only lets you use RS in a syntactically similar fashion to Jobs, but is also more performant.

I tested the wrapper by re-creating the error you were seeing (while creating a test record ) and then just substituting Start-RSJob for Start-Job. I verified that the record was created successfully.

This should get you were you need to go.

thuld commented 8 years ago

Hello @RedsGT

thanks for the RsJob tip. Based on my understanding do I need to comment-out this line to make "Microsoft.Xrm.Data.PowerShell " work with jobs.

# Minimum version of the Windows PowerShell host required by this module #PowerShellHostVersion = '4.0'

Ok, jobs are not needed for Runspace, but based on the comment on SO is ShellHostVersion not relevant for this module.

http://stackoverflow.com/questions/35204785/start-job-and-psversion-error

So just in case this module should support job, is it required to comment out the mentioned line?

RedsGT commented 8 years ago

When I look directly at https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell/blob/v2/Microsoft.Xrm.Data.PowerShell/Microsoft.Xrm.Data.PowerShell.psd1

I see:

# Minimum version of the Windows PowerShell host required by this module PowerShellHostVersion = '1.0'

in that section of code while the main branch shows:

# Minimum version of the Windows PowerShell host required by this module PowerShellHostVersion = '4.0'

So the main branch is wrong and should be updated, but branch v2 is correct. I'll put in the change for the main branch, but you should be using v2 anyway.

RedsGT commented 8 years ago

The change is in pull request #53

kenakamu commented 8 years ago

fixed the issue by merging v2 to v1 branch