locked
Get a return value from invoke-command RRS feed

  • Question

  • Hi

    I'm calling invoke-command and need to get the "return value". I've tried to simplify it below but basically the invoke-command will return the status of a database. This piece of the script works. But I need to call this from outside the invoke-command and return the status back outside

    Invoke-Command -ComputerName XXXX -ScriptBlock {

    # There is a code here which ends up returning the database status ...

    $DatabaseStatus = "Online" }

    How do I return the value of $DatabaseStatus from the invoke-command so that I can continue processing it?

    I was hoping that I could  $DBState = Invoke-Command ..... but it doesn't work - I realise that invoke-command is running on a remote machine and I don't know if you can pass output values of invoke-command back to the originating code?  

    Thanks in advance

    Graham


    Regards Graham New System Center 2012 Blog! - http://www.systemcentersolutions.co.uk
    View OpsMgr tips and tricks at http://systemcentersolutions.wordpress.com/

    Saturday, November 24, 2012 9:38 PM

Answers

  • Try this...

    $Result = Invoke-Command -ComputerName <<Server>> -ScriptBlock {
    
    Import-Module SQLPS -DisableNameChecking
    
    $DBState = Invoke-SQLCmd "Select state_desc from master.sys.databases where name = '<<DBName>>'" -ServerInstance "DMZWG1" -UserName sa -Password <<Password>>
    
    Return $DBState # you shouldn't even need Return
    }
    
    $Result


    Admiral Ackbar says...


    • Edited by RiffyRiot Tuesday, December 4, 2012 5:45 PM
    • Marked as answer by Graham Davies Tuesday, December 4, 2012 5:55 PM
    Tuesday, December 4, 2012 5:41 PM

All replies

  • Just return the DatabaseStatus in your script block:

    $DatabaseStatus = Invoke-Command -ComputerName XXXX -ScriptBlock {
    # There is a code here which ends up returning the database status ... 
    $DatabaseStatus = "Online"
    $DatabaseStatus
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Saturday, November 24, 2012 10:04 PM
  • I did a quick test with this code...

    $result=invoke-command -computer PC2 -credential $cred -scriptblock {$process=$args[0];Get-Process -Name $process} -argumentlist 'cmd'
    $result

    ... and it returns the result of the remotely executed code in $result.

    You could try write-output $DatabaseStatus as your last instruction.

    You'll have to post your scriptblock to diagnose further.


    Admiral Ackbar says...

    Saturday, November 24, 2012 10:21 PM
  • Hi,

    Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.

    If you have any feedback on our support, please click here .


    Cataleya Li
    TechNet Community Support

    Tuesday, November 27, 2012 8:04 AM
    Moderator
  • Hi

    Sadly this doesn't work for me (I had already tried it as per my first post).

    $Result = Invoke-Command -ComputerName <<Server>> -ScriptBlock {
    
    Import-Module SQLPS -DisableNameChecking
    
    $DBState = Invoke-SQLCmd "Select state_desc from master.sys.databases where name = '<<DBName>>'" -ServerInstance "DMZWG1" -UserName sa -Password <<Password>>
    
    write-host $DBStatus = $DBState.ItemArray[0] 
    }
    
    $Result 
    
    write-host $Result 

    I get 2 lines of output (one from each write-host).

    The first line is = ONLINE

    The second line is blank.

    If I comment out the write-host $Result then not surprisingly I only get the one line = ONLINE

    Cheers

    Graham


    Regards Graham New System Center 2012 Blog! - http://www.systemcentersolutions.co.uk
    View OpsMgr tips and tricks at http://systemcentersolutions.wordpress.com/

    Tuesday, December 4, 2012 11:49 AM
  • Try this...

    $Result = Invoke-Command -ComputerName <<Server>> -ScriptBlock {
    
    Import-Module SQLPS -DisableNameChecking
    
    $DBState = Invoke-SQLCmd "Select state_desc from master.sys.databases where name = '<<DBName>>'" -ServerInstance "DMZWG1" -UserName sa -Password <<Password>>
    
    Return $DBState # you shouldn't even need Return
    }
    
    $Result


    Admiral Ackbar says...


    • Edited by RiffyRiot Tuesday, December 4, 2012 5:45 PM
    • Marked as answer by Graham Davies Tuesday, December 4, 2012 5:55 PM
    Tuesday, December 4, 2012 5:41 PM
  • Hi

    Well, whether we should or shouldn't need the explicit return, it does make all the difference.

    Many Thanks

    Graham


    Regards Graham New System Center 2012 Blog! - http://www.systemcentersolutions.co.uk
    View OpsMgr tips and tricks at http://systemcentersolutions.wordpress.com/


    Tuesday, December 4, 2012 5:56 PM
  • Or, you can omit "return" keyword as below: $DBStatus = $DBState.ItemArray[0] But, you'd fail when adding Write-Host which don't get a return.
    Sunday, November 9, 2014 11:12 PM
  • Here's a way that's rarely thought of... use convertto-json and convertfrom-json

    It's a little unorthodox, and may require some tweaking for each case, but the general idea is there.  

    $returnedVariable = Invoke-Command -ComputerName $computerName {
    $services = Get-service
    $services | ConvertTo-Json} | ConvertFrom-Json
    
    
    $returnedVariable

    Monday, November 18, 2019 8:54 PM
  • Here's a way that's rarely thought of... use convertto-json and convertfrom-json

    It's a little unorthodox, and may require some tweaking for each case, but the general idea is there.  

    $returnedVariable = Invoke-Command -ComputerName $computerName {
    $services = Get-service
    $services | ConvertTo-Json} | ConvertFrom-Json
    
    
    $returnedVariable

    Please do not post before actually reading the question and avoid posting on topics that are answerd, closed and 8 years old.  It serves no purpose, is misleading and adds unnecessary noise to a topic.


    \_(ツ)_/

    Monday, November 18, 2019 9:01 PM
    Moderator