locked
Receive-job RRS feed

  • Question

  • Hi,

    I have launched the creation of 6 VM's with start-job but apparently they failed (ater running for some hours). How can I debug what went wrong since output is lost.

    Please advise.
    J.


    Jan Hoedt


    • Edited by janhoedt Friday, March 4, 2016 9:33 AM UPdate
    Friday, March 4, 2016 9:31 AM

Answers

  • The fastest way to debug a job is to use a transcript:

    start-job -ScriptBlock {start-transcript c:\test\mylog.txt;dir c:\;1/0;stop-transcript} |wait-job
    cat c:\test\mylog.txt


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, March 7, 2016 3:37 PM
    Saturday, March 5, 2016 8:25 PM
  • You should take time to learn how to format your scripts so that they a readable.

    $sb={ 
        start-transcript c:\logs\CreateVMW7.txt
        Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
        Create-NewVMW7 -options ...;
        stop-transcript
    }
    $job=start-job -script $sb |Wait-Job
    Receive-Job $job
    


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, March 24, 2016 2:23 PM
    Thursday, March 24, 2016 9:56 AM
  • Start-Transcript c:\logs\logfile.txt

    ... script here

    Stop-Transcript


    \_(ツ)_/


    • Edited by jrv Saturday, March 5, 2016 8:21 PM
    • Marked as answer by janhoedt Monday, March 7, 2016 3:37 PM
    Saturday, March 5, 2016 8:20 PM

All replies

  • Debug your scripts before using them in a job.  You can also run a transcription of the job.


    \_(ツ)_/

    Friday, March 4, 2016 6:37 PM
  • Thanks, transcript is an option indeed. Can you advise on approach to write output to a logfile? Have not been looking into debugging into detail yet.
    <object data-extension-version="0.4.0.129" data-install-updates-user-configuration="true" id="__symantecPKIClientMessenger" style="display:none;"></object>

    Jan Hoedt

    Saturday, March 5, 2016 8:12 PM
  • Start-Transcript c:\logs\logfile.txt

    ... script here

    Stop-Transcript


    \_(ツ)_/


    • Edited by jrv Saturday, March 5, 2016 8:21 PM
    • Marked as answer by janhoedt Monday, March 7, 2016 3:37 PM
    Saturday, March 5, 2016 8:20 PM
  • The fastest way to debug a job is to use a transcript:

    start-job -ScriptBlock {start-transcript c:\test\mylog.txt;dir c:\;1/0;stop-transcript} |wait-job
    cat c:\test\mylog.txt


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, March 7, 2016 3:37 PM
    Saturday, March 5, 2016 8:25 PM
  • Great idea. That would be great to put into a function, so you could just add something like  -enabletranscript c:\logfile\nameofscript.log

    Jan Hoedt

    Monday, March 7, 2016 4:33 PM
  • Could only look at it now. 2 more things:

    1.could you explain why the | wait-job?
    2.the error I could capture is
    "The term 'New-CMDeviceVariable' is not recognized as the name of a cmdlet, function, script file, or operable program. "

    The script itself works fine, but not when launched from within a job. So the jobs can't see the loaded Configuration Manager (SCCM) modules. How can I make sure it sees the modules IN the job?

    This is the script:

    start-job -name 'Create W7 VM' -scriptblock  {start-transcript c:\logs\CreateVMW7.txt;Create-DeployVMGuest blabla;stop-transcript}


    Jan Hoedt

    Wednesday, March 23, 2016 4:03 PM
  • As you already know each job is a separate instance of PowerShell and requires that mall modules be explicitly loaded.

    \_(ツ)_/

    Wednesday, March 23, 2016 4:11 PM
  • Right! So I probably have to work with the -initializationscript to load my modules

    Jan Hoedt

    Wednesday, March 23, 2016 4:22 PM
  • This is what I have now:

    start-job -name 'Create VMW7' -InitializationScript { Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' } -scriptblock  {start-transcript c:\logs\CreateVMW7.txt;Create-NewVMW7 -options ...;stop-transcript}

    I can see in the logfile that it is working and see the vm getting created but when I run a get-job there is none(?).

    Jan Hoedt

    Thursday, March 24, 2016 9:12 AM
  • You should take time to learn how to format your scripts so that they a readable.

    $sb={ 
        start-transcript c:\logs\CreateVMW7.txt
        Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
        Create-NewVMW7 -options ...;
        stop-transcript
    }
    $job=start-job -script $sb |Wait-Job
    Receive-Job $job
    


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, March 24, 2016 2:23 PM
    Thursday, March 24, 2016 9:56 AM
  • Works fine but get-job still doesnt return any result. When I do a receive-job I get the output though. That's quite ennoying since I want to kill the (test)job but can't.

    Jan Hoedt


    • Edited by janhoedt Thursday, March 24, 2016 2:27 PM Update
    Thursday, March 24, 2016 2:24 PM
  • Forgot a comma :-)

    Working fine now. Trying to make it work for multiple computers but that doesn't work (yet).

    $computernames = @('Computer1','Computer2')
    $script={   
            param($computername)
                start-transcript c:\logs\Create$ComputerName.log
                Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
                Create-VM - opptions ...;
                stop-transcript
        }
    foreach ($computer in $computernames)
        {
            $job = Start-job -script $script -ArgumentList $computername -ErrorAction Continue 
            Receive-Job $job -Keep
        }




    Jan Hoedt

    Thursday, March 24, 2016 3:12 PM