0

I am trying to do something similar to this. First function Second function calls first function and trigger a Start-Job

example:

Function CreateDeleteDirs {
Param(
  [Parameter(
  Mandatory = $True,
  HelpMessage = 'Remote Path to create dirs on. Provide full path.')
  ]
  [ValidateNotNullOrEmpty()]
  [string]$RootPath
  )
  ***do some stuffs***
}
Function CreateDeleteDirBack {
Param(
  [Parameter(
  Mandatory = $True,
  HelpMessage = 'Remote Path to create dirs on. Provide full path.')
  ]
  [ValidateNotNullOrEmpty()]
  [string]$RootPath
  )
  $scriptBlock = {
 param ($RootPath)
 CreateDeleteDirs -RootPath $RootPath 

} Start-Job -ScriptBlock $scriptBlock -ArgumentList @($RootPath) }
}

So its always failing with calling CreateDeleteDirs from second function .. how can I do this

The exact error snippet

PS C:\Program Files\WindowsPowerShell\Modules> CreateDeleteDirsBackground -RootPath Y: 

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
21     Job21           BackgroundJob   Running       True            localhost            ...                      



PS C:\Program Files\WindowsPowerShell\Modules> Receive-Job Job21
The term 'CreateDeleteDirs' 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: (CreateDeleteDirs:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : localhost


PS C:\Program Files\WindowsPowerShell\Modules> 
udaya
  • 77
  • 8

1 Answers1

0

I have modified your code to output the root path so we can see it working and waited for the job to complete.

$script_block = {
  function CreateDeleteDirs {
    Param(
      [Parameter(Mandatory = $True, HelpMessage = 'Remote Path to create dirs on. Provide full path.')]
      [ValidateNotNullOrEmpty()]
      [string]$RootPath
    )

    write-host "Rootpath = '$RootPath'"
  }

  function CreateDeleteDirBack  {
    Param(
      [Parameter(Mandatory = $True, HelpMessage = 'Remote Path to create dirs on. Provide full path.')]
      [ValidateNotNullOrEmpty()]
      [string]$RootPath
    )

    CreateDeleteDirs -RootPath $RootPath 
  } 
}

Start-Job -InitializationScript $script_block -ScriptBlock {CreateDeleteDirBack  'foo'} | Wait-Job | Receive-Job
Steve
  • 710
  • 1
  • 6
  • 12
  • Thanks ! But I couldnt use CreateDeleteDirs as an individual function further with this structure – udaya Mar 11 '19 at 15:53