Start-Job InitializationScript misbehavior RRS feed

  • Question

  • I have a script that is intended to backup our DFS Namespace structures, including all of the metadata, as the DFS command line tools are eventually going away.  Although the number of namespaces is relatively small, there are several very large ones, so I created a small function to get the namespace folders and targets, with the intent of having each namespace folder enumeration be a separate job.  Unfortunately, I'm running into a problem with the -InitializatioScript parameter on Start-Job.

    I have defined a scriptblock to define the enumeration function:

    $dfsNamespaceFunction =	{ 
        function Get-DfsNamespaceTargets {
            Param ( [String[]]$root 
            Foreach ($r in $root) {
    	    Get-DfsnFolder -Path $($r.Path + "\*") |
    	        ForEach-Object { 
                        Get-DfsnFolderTargGet-DfsnFolderTarget -Path $_.Path } | 
                            Select-Object Path,TargetPath,State,ReferralPriorityClass,ReferralPriorityRank

    To call the function, I am using the following:

    $job += Start-Job -name $root -InitializationScript $dfsNamespaceFunction -ScriptBlock {param ($root,$file,) Get-DfsNamespaceTargets -Path $root | Export-Csv -Path $file -NoTypeInformation} -ArgumentList $root,$folderTargetFile

    where $job is an array holding the jobs (one for each root) for monitoring once all are launched. 

    When I run my script, I get the following line in the console, one for each Start-Job execution:

    function Get-DfsNamespaceTargets { }

    When I check the job status, all show failed, and when I run Receive-Job against them, they all have the same error message:

    The term 'Get-DfsNamespaceTargets' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
        + CategoryInfo          : ObjectNotFound: (Get-DfsNamespaceTargets:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
        + PSComputerName        : localhost

    First, to my understanding, there should be NO console output from Start-Job given the command line I have above.  Is that correct?

    Second, why would the function scriptblock being passed appear to be empty when it's defined in the variable passed to -InitializationScript?

    And third, even if the scriptblock is blank, why isn't the function defined in the Start-Job scope?  I've tested creating an empty function like that, and it works fine (even if it does nothing). 

    Any assistance is appreciated!


    Friday, May 4, 2018 4:59 PM

All replies

  • The init script is not normally used to define functions at a global scope.

    Define the function like this:

     function global:Get-DfsNamespaceTargets


    Friday, May 4, 2018 7:21 PM
  • Thanks for the reply.

    At this point, the problem appears to have been between the keyboard and the chair.   I had an old function definition that used [ScriptBlock]::Create() that had been commented out that apparently got un-commented.  I've deleted that offending line completely, and this seems to be working correctly.

    Monday, May 7, 2018 10:05 PM