locked
Getting output value from invoke-command RRS feed

  • Question

  • Hi All,

    I have the code snippet below.

    $scriptblock =
    {
        $filename = 'D:\profile\test\summary.txt'
        write-output "Getting the file information"
        write-output "Import complete"
        return $filename
    }
    
    $result = Invoke-Command -ScriptBlock $scriptblock
    

    The above is similar to a problem that I am trying to solve, i want to get the value of the filename outside the script block.

    I was wondering if some string manipulation is the answer.

    Tuesday, March 3, 2020 11:44 PM

All replies

  • Hi All.

    I have a Ps script which a lot of write-output commands, this script is within a script block that uses WinRM. I want to return a value so that the value can be used elsewhere, the problem I have is that I used

    Return $filename

        $file_details = Invoke-Command -ComputerName $server -Credential $Credentials -ScriptBlock $ScriptBlock -ErrorAction Stop 
    

    The Issue is that the script has a lot of output mesages, and then at the end is the filename.

    All I want to do is extract the filename.

    Is there a way to do this ?

    Thanks in advance.

    • Merged by jrv Wednesday, March 4, 2020 12:43 AM DUPLICATE
    Monday, March 2, 2020 12:41 PM
  • The Return $filename is it within ScriptBlock ??


    Link that may help for Invoke-Command and Outputs.

    https://social.technet.microsoft.com/Forums/en-US/985276d1-016a-42cd-bba0-19de2292549b/output-from-invokecommand-not-returning?forum=winserverpowershell

    https://social.technet.microsoft.com/Forums/en-US/1e994906-5693-4a7e-85da-606d77d55469/output-to-local-text-file-for-invokecommand?forum=winserverpowershell

    Monday, March 2, 2020 1:12 PM
  • Hi All.

    I have a Ps script which a lot of write-output commands, this script is within a script block that uses WinRM. I want to return a value so that the value can be used elsewhere, the problem I have is that I used

    Return $filename

        $file_details = Invoke-Command -ComputerName $server -Credential $Credentials -ScriptBlock $ScriptBlock -ErrorAction Stop 

    The Issue is that the script has a lot of output mesages, and then at the end is the filename.

    All I want to do is extract the filename.

    Is there a way to do this ?

    Thanks in advance.

    There is no way to answer your question.  Without code it is just an arbitrary question.  Any values returned from a scriptblock will end up in the return variable.

    Example:

    $sb = {
        'Hello'
        Write-Host 'Nothing to see here' -Fore Green
        Write-Output 'World'
        return 'Gone in a flash'
        'This line goes nowhere'
    }
    
    Invoke-Command $sb
    $return = Invoke-Command $sb
    $return
    



    \_(ツ)_/

    Monday, March 2, 2020 10:18 PM
  • If I follow, this works:

    $result[2]

    Wednesday, March 4, 2020 12:28 AM
  • Please do not repost the same exact issue just because you don't understand the answer.  If you can't understand answers then state the issue you are having with understanding.  Reposting repeatedly will not get you a better answer.


    \_(ツ)_/

    Wednesday, March 4, 2020 12:35 AM
  • i want to get the value of the filename outside the script block.

    If you want to output only the filename you have to stop outputting more than that.

    $scriptblock =
    {
        $filename = 'D:\profile\test\summary.txt'
        $filename
    }
    
    $result = Invoke-Command -ScriptBlock $scriptblock
    You may additionally read about Write-Verbose and Write-Debug.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, March 4, 2020 12:47 AM
    Wednesday, March 4, 2020 12:45 AM
  • I posted the example to get y9ou to understand how data is managed in PowerShell.  data returned from a function a script or a script block follows the standards of all programming languages with one exception.  PowerShell allows multiple objects to be returned.

    Learning how this works is of the greatest importance in PowerShell.

    Also a script block can be seen as an "anonymous function" stored in a variable.

    Example:

    # anonymous function
    {
        Get-Process | select -first 5
    }.Invoke()
    
    # script block
    $sb = {
        Get-Process | select -first 5
    }
    $sb.Invoke()
    
    # function
    function test{
        Get-Process | select -first 5
    }
    test

    A function is just a named anonymous function

    The rules for data returned are that all output is returned from any of the above.

    The normal method for managing this is to only return what you need as a customobject or to return only the item needed.

    $scriptblock ={
        $filename = 'D:\profile\test\summary.txt'
        Write-Host "Getting the file information"
        write-Host "Import complete"
        return $filename
    }
    $filename = $scriptblock.Invoke()
    

    Now review the variable st on the return.

    Avoid arrays as return objects as they are unreliable.  In fact you should mostly always avoid managing data in PowerShell in arrays except when creating collections of objects and then "Type" the array according to the objects it contains.

    $psarray = [pscustomobject[]]@()


    \_(ツ)_/

    Wednesday, March 4, 2020 1:00 AM