The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Azure App Service - Web Apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
Powershell Webjob Write-Host and Write-Output issues RRS feed

  • Question

  • I would like some logging for my Powershell script that I run in a Webjob. I have issues with both Write-Host and Write-Output.

    The Code

    function GetNumber(){
        Write-Host "GetNumber called"
        return 3
    }
    
    $number = GetNumber
    Write-Host $number
    $sum = $number + 1
    Write-Host $sum

    output from a run:

    GetNumber called
    3
    4


    Write-Host

    Running this in Azure as a Webjob however result in a crash I cannot resolve:

    [11/03/2017 10:12:30 > c2d2b2: SYS INFO] Status changed to Initializing [11/03/2017 10:12:30 > c2d2b2: SYS INFO] Run script 'LogIssue.ps1' with script host - 'PowerShellScriptHost' [11/03/2017 10:12:30 > c2d2b2: SYS INFO] Status changed to Running [11/03/2017 10:12:31 > c2d2b2: ERR ] Write-Host : The Win32 internal error "The handle is invalid" 0x6 occurred [11/03/2017 10:12:31 > c2d2b2: ERR ] while setting character attributes for the console output buffer. Contact [11/03/2017 10:12:31 > c2d2b2: ERR ] Microsoft Customer Support Services. [11/03/2017 10:12:31 > c2d2b2: ERR ] At D:\local\Temp\jobs\triggered\LogIssue\qysrjmli.z0x\LogIssue.ps1:2 char:5 [11/03/2017 10:12:31 > c2d2b2: ERR ] + Write-Host "GetNumber called" [11/03/2017 10:12:31 > c2d2b2: ERR ] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [11/03/2017 10:12:31 > c2d2b2: ERR ] + CategoryInfo : ResourceUnavailable: (:) [Write-Host], HostExcep [11/03/2017 10:12:31 > c2d2b2: ERR ] tion [11/03/2017 10:12:31 > c2d2b2: ERR ] + FullyQualifiedErrorId : SetConsoleTextAttribute,Microsoft.PowerShell.Com [11/03/2017 10:12:31 > c2d2b2: ERR ] mands.WriteHostCommand [11/03/2017 10:12:31 > c2d2b2: ERR ]

    ...


    Write-Output

    I've seen several suggestions to use Write-Output instead. This works in some simple cases but will affect the return value of functions. See https://powershellstation.com/2011/08/26/powershell%E2%80%99s-problem-with-return/

    In the code example, if we replace Write-Host with Write-Output, the output of the run script will instead be:

    GetNumber called
    3
    1

    Note the 1! The object returned from the function is some mixed object from the Write-Output and the return statement. When you echo the returned object it looks OK but operations on the object won't work as expected.

    [console]::WriteLine

    If we use [console]::WriteLine instead

    function GetNumber(){
        [console]::WriteLine("GetNumber called")
        return 3
    }
    $number = GetNumber
    [console]::WriteLine("{0}",$number)
    $sum = $number + 1
    [console]::WriteLine("{0}",$sum)

    The output is correct again:

    • GetNumber called
      3
      4

    And it outputs logs in the Azure web job!!

    Note the ("{0}",...) format. WriteLine($number) won't create a line-break of some reason.

    The questions

    1. Can I configure the Web app/Webjob to support the Write-Host somehow? (my googling skills failed me)
    2. Is there a better (and not super-complex) way to debug in the Webjob
    3. Is there some way to rewrite the function or the code in general that works better
    Friday, November 3, 2017 10:47 AM

All replies

  • Not sure exactly what's going here, but one suggestion is to try to isolate without using WebJobs, and using the Kudu Console instead (which has a PowerShell flavor). See this page.

    I also wonder whether this recently found issue relates to this in some way.

    Friday, November 3, 2017 8:28 PM
    Moderator
  • Thank you, I will take a look at Kudu. Since the original post a year ago I've had time to look into alternatives, and I think this kind of Powershell based jobs are better run as Azure Runbooks, or we use Logic Apps for the scheduling and implement the functionality in .NET running as a Azure Function or a web api.
    Thursday, December 20, 2018 12:04 PM