Need help with Start-Job in V2

This topic contains 2 replies, has 2 voices, and was last updated by  Sirirat Kongdee 3 years, 10 months ago.

  • Author
    Posts
  • #10801

    Sirirat Kongdee
    Participant

    This following script works in V3 but for some reasons it is doing nothing in V2. The job started and keep running but never completed.


    $command = 'Import-Module Y:\xxxxx\xxxxxxxxx\xxxxxx1\xxxxxx1.psm1
    sftp-file -file ' +$CompressedFile + ' -SftpHostname '+ $ftp + ' -Username ' + $ftpusername +' -Password '+'"'+$ftppassword +'"'+' -Destinaton '+ '"'+$dest +'"'+ '-logfile ' + '"'+$l+'"'

    $sb=[scriptblock]::Create($command)

    Start-Job -ScriptBlock $sb

    I have also tried the following and it is doing the same thing.

    Start-Job -initializationScript {Import-Module Y:\xxxxx\xxxxxxxxx\xxxxxx1\xxxxxx1.psm1} -ScriptBlock $sb}

    The script is meant to be used in ScheduleTask. It is working in V3 and I have no idea why it doesn't work in V2.

    Thank you!

  • #10802

    Don Jones
    Keymaster

    FYI, if you delimit the command in {} instead of single quotes, it'll be a script block without having to cast it.

    Does the command work, by itself, in v2, if you run it interactively? That is, run it not in a job?

    In other words, can you just run &$command and have the command work properly?

  • #10803

    Sirirat Kongdee
    Participant

    Thank you Don for your reply. I really appreciate it.

    I put the command as string and cast it because I need to pass variables. Is there a better way to do it? If I put the variable in the script block, it doesn't pass the value.

    And yes, I can run these command and they work fine interactively.

    Import-Module Y:\xxxxx\xxxxxxxxx\xxxxxx1\xxxxxx1.psm1

    then

    sftp-file -file xxxxx -SftpHostname xxxxxxx -Username xxxxxx -Password xxxxxx

    If I put them in Start-Job -scriptblock { }, the job started and its status is running. But it never completed. I run receive-job command and nothing comes back.

    I also tried

    Start-Job -initializationScript {Import-Module Y:\xxxxx\xxxxxxxxx\xxxxxx1\xxxxxx1.psm1} -ScriptBlock {sftp-file -file xxxxx -SftpHostname xxxxxxx -Username xxxxxx -Password xxxxxx}

    and it does not work either.

You must be logged in to reply to this topic.