none
'Write-Host' is not recognized as the name of a cmdlet RRS feed

  • Question

  • I run into this weird issue in my Powershell data processing script which has the following structure.

    Data sources -> Threading Pool (long running runspaces) reads data from data sources and then put the PSObjects to -> Parsing Concurrent Queue (System.Collections.Concurrent.BlockingCollection[psobject]) <- Parsing Thread Pool (long running runspaces) pick the PSObjects from the queue, does the parsing and put the parsed data to another -> Output Concurrent Queue -> Write-Host in main thread picks the data from the queue and write to stdout.

    The error happened in the main function which dumps the data to stdout. The code snippet is as below. It worked for a bunch of loops and then ceased with these errors. May i ask if you have encountered the same similar issues before and what are the possible cause of this ? 

    Thanks you so much !

    Error 1:

    Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At C:\Program Files\Splunk\bin\splunk-powershell-common.ps1:1439 char:5
    +     Write-Host "</stream>"
    +     ~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException


    There is another error which is also very weird.

    Error 2:

    [ref] cannot be applied to a variable that does not exist.
    At C:\Program Files\script.ps1:1413 char:13
    +         if ($outputQueue.TryTake([ref] $dataP, $tryMilliseconds) -eq  ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (dataP:VariablePath) [], RuntimeException
        + FullyQualifiedErrorId : NonExistingVariableReference

    Function in main thread:

    function waitAndPrintData($outputQueue) {
        $tryMilliseconds = 1000
        while (1) {
            $dataP = $null
            if ($outputQueue.TryTake([ref] $dataP, $tryMilliseconds) -eq $False) {
                continue
            }
            if ($null -eq $dataP) {
                continue
            }
            Write-Host $dataP
    }

    Monday, March 25, 2019 7:35 PM