0

I need to delete a hell lot of records using a web-service. The module Microsoft.Xrm.Data.Powershell provides several cmdlets to get this job done.

The following script should be executed by the different jobs:

$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 'foobar' -Id $record.foobarid
        $counter++
    }
}

This is the code I use to execute my jobs:

foreach($p in $packages) {

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

    $packageNumber++
}

When I execute my code the following error message is thrown inside the jobs:

Der aktuelle Windows PowerShell-Host ist: "ServerRemoteHost" (Version 1.0.0.0). Zum Ausführen 
des Moduls "C:\Users\foobar\Documents\WindowsPowerShell\Modules\Microsoft.Xrm.Data.Powershell\Micro
soft.Xrm.Data.Powershell.psd1" ist mindestens Version 4.0 des Windows PowerShell-Hosts 
erforderlich.
    + CategoryInfo          : ResourceUnavailable: (C:\Users\foobar\Do...Powershell.psd1:String) [ 
   Import-Module], InvalidOperationException
    + FullyQualifiedErrorId : Modules_InsufficientPowerShellHostVersion,Microsoft.PowerShell.Co 
   mmands.ImportModuleCommand
    + PSComputerName        : localhost

The error message says that the module "Microsoft.Xrm.Data.Powershell" requires version 4.0 of PowerShell, but that the current host is using version 1.0.

Based on the online documentation for Start-Job can I pass the parameter -PSVersion but only with the values 2.0 or 3.0.

Does this mean that it is simply not possible to execute an job that uses a module that requires PowerShell 4.0 /5.0?

Update 2016-02-04: I commented out the following line in the 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 inside the jobs:

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

I do not understand why I have now "Deserialized.Microsoft.Xrm.Tooling.Connector.CrmServiceClient" and "Microsoft.Xrm.Tooling.Connector.CrmServiceClient"

thuld
  • 680
  • 3
  • 10
  • 29
  • 2
    IMHO, it is bug in module. PowerShell version and Host version are two different things. Specifying `PowerShellHostVersion` without `PowerShellHostName` looks kind of weird to me. You should try to comment this line `PowerShellHostVersion = '4.0'` in module manifest `Microsoft.Xrm.Data.PowerShell.psd1` and try again. – user4003407 Feb 04 '16 at 15:34
  • @PetSerAl I adjusted the mentioned file, but I now face a different issue. See updated question. – thuld Feb 04 '16 at 16:35
  • @PetSerAl Ok, I found your comment on another question: http://stackoverflow.com/questions/34237490/start-job-wait-job-doesnt-work-with-copy-here#comment56222096_34237490; so I assume I cannot pass these kind of object between different powerShell sessions.... – thuld Feb 04 '16 at 16:42
  • 1
    Yes, you have to create new connection inside the job. – user4003407 Feb 04 '16 at 18:23
  • I assume I should switch to "Runspace", I found a very good video on this topic: The Art of PowerShell Runspace(https://www.youtube.com/watch?v=qyTm-bQrkLY), in this video is also the object serialize behavior of jobs mentioned. – thuld Feb 05 '16 at 15:26

0 Answers0