Need help with Start-Job in V2

Welcome Forums General PowerShell Q&A Need help with Start-Job in V2

This topic contains 2 replies, has 2 voices, and was last updated by

5 years, 4 months ago.

  • Author
  • #10801

    Points: 0
    Rank: Member

    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+'"'


    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

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Points: 0
    Rank: Member

    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


    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.

The topic ‘Need help with Start-Job in V2’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort