Search code examples
powershellpowershell-cmdletstart-job

Invoking custom cmdlet from script block in PowerShell


I'm very new to PowerShell. I wrote a cmdlet which works fine. However, when I try and invoke it inside a job...

. .\MyCmdlet.ps1 # Dot Source
$GetProcesssJob = Start-Job -ScriptBlock {
  MyCmdlet
} -Credential $specialCredentials

...I get the error that it's "not recognized as the name of a cmdlet, function, script file, or operable program". What am I doing wrong?


Solution

  • My problems were two-fold. As TheIncorrigible1 pointed out, I needed to put the dot-sourcing inside the ScriptBlock. However, I had tried that previously and it didn't work. I now realize that's because the credentials I was using in $specialCredentials didn't have access privileges to the file MyCmdlet.ps1!