how to pass a remote variable back into a function

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5661

    by willbs at 2013-03-13 11:14:19

    i have this code, i check a list of services against a remote UUT
    how do i pass $servcount back into the function so i can use it later locally?

    function GetUnListedServices ($Serv)
    {
    $Data = @()
    $Script = {param($Servs) if (-not (get-service $Servs -errorAction SilentlyContinue) ) {$servcount = 1; "The Service $Servs IS NOT on the UUT Service List, FAIL!" } }
    foreach ($EXV in $Serv) {
    $Data += invoke-command -scriptblock $Script -computername $global:uutName -credential $global:Credential -ArgumentList $EXV -OutVariable out | out-file -filepath $global:OutputReportFile -append
    }
    if ($servcount -eq $null) {"All Services are Present and Match, PASS!" | out-file -filepath $global:OutputReportFile -append}
    }

    by ArtB0514 at 2013-03-13 12:57:54

    Just put it at the end of the function where it will be returned automatically:
    function Get-MyUnListedServices ($Serv) {
    $Data = @()
    $Script = {param($Servs) if (-not (get-service $Servs -errorAction SilentlyContinue) ) {$servcount = 1; "The Service $Servs IS NOT on the UUT Service List, FAIL!" } }
    foreach ($EXV in $Serv) {
    $Data += invoke-command -scriptblock $Script -computername $global:uutName -credential $global:Credential -ArgumentList $EXV -OutVariable out | out-file -filepath $global:OutputReportFile -append
    }
    if ($servcount -eq $null) {"All Services are Present and Match, PASS!" | out-file -filepath $global:OutputReportFile -append}
    $servCount
    }

    Then call the function:

    "Found $(Get-MyUnlistedServices $ServerList) unlisted services"

    by willbs at 2013-03-13 13:09:21

    when i run that function, $servcount always comes back null because it is embedded in the remote call
    i would like to extract $servcount when the "then" portion of the 1st if-then is run and use it in the second if statement of the function
    i don't need to pass $servcount back to the function call

    by ArtB0514 at 2013-03-13 13:16:39

    Sorry about that, I didn't read your script too fully. It looks like you don't do anything with the $servcount variable anywhere in the remote script, and that the count of the $Data objects will be equal to the sum of the $servcount items. Try it this way:

    function Get-MyUnListedServices ($Serv) {
    $Data = @()
    $Script = {
    param($Servs)
    if (-not (get-service $Servs -errorAction SilentlyContinue) ) {
    "The Service $Servs IS NOT on the UUT Service List, FAIL!"
    }
    }
    foreach ($EXV in $Serv) {
    $ThisItem = invoke-command -scriptblock $Script -computername $global:uutName -credential $global:Credential -ArgumentList $EXV
    $ThisItem | out-file -filepath $global:OutputReportFile -append
    $Data += $ThisItem
    }
    $servcount = $Data.Count
    if ($servcount -eq $null) {"All Services are Present and Match, PASS!" | out-file -filepath $global:OutputReportFile -append}
    $servCount
    }

    "Found $(Get-MyUnlistedServices $ServerList) unlisted services"

    I also made a few changes in your call to Invoke-Command. You never used the variable $Out that you created, so I removed it. In addition, I try to avoid pipelines that try to do too many things at once, so I split yours up. Please just take that as an example of my personal preferences. Ignore if it doesn't work for you.

    by willbs at 2013-03-13 15:02:39

    great it worked very well,
    it was the $servcount = $Data.Count command that i didn't know about, but figured there was something out there
    i modified my code to work with that for practice but i'll use your code, being a noobe, my code is very bulldozer-ish, and yours is quite elegant looking
    also FYI, $servcount is returned as "0" so i had to do the compare to "0" instead of $null
    thanks a lot for your help

You must be logged in to reply to this topic.