3

First, I'm just learning PowerShell, but I have programmed in many scripting languages.

I'm writing a script to retrieve output from a command on a remote server and then check that output for the word "running". Right now it retrieves the output of the command, and I have it in my variable, $data. The problem is I'm putting it through my "clean_data" function, and it's not the same contents in the variables that I'm passing to the function.

What I'm wondering is how on Earth does the output of the $data variable have one content when printed to screen, but when passed to a function and immediately printed have a new content? What am I missing?

I just want to loop through the lines that were returned to see how many have "running" in them and print a nice error if there isn't.

CODE (Partial: function and relevant code)

function clean_data($input) {
  Write-Output "Received data"$input
  Write-Output "Cleaning data"
  $output=@()
  foreach ($line in $input) {
     Write-Output "Looking at: "$line.ToString()
     if ($line.ToString().Contains("running")) {
         $output+=$line
     }
   }
   Write-Output $output
   return ,$output
}

$data = Get-Job -id $jobid.id | Receive-Job
Write-output "Data Type: "$data.GetType().Fullname
Write-Output "Output of $cmd"
Write-Output "============================================================"
Write-Output $data
Write-Output "============================================================"
clean_data $data
Write-Output $newdata
Remove-Job -Id $jobid.Id

OUTPUT

Data Type:
System.Object[]
Output of C:\opcragt.bat servername
============================================================

C:\Windows\system32>"D:\Program Files\HP\HP BTO Software\bin\win64\opcragt.cmd" servername


Node: servername
HPOM Managed Node status:
-------------------------
OV Performance Core coda (3500) is running
OV Communication Broker ovbbccb (10988) is running
OV Control ovcd (10400) is running
OV Config and Deploy ovconfd (5092) is running

Subagent EA:
Action Agent opcacta (9716) is running
Monitor Agent opcmona (9872) is running
Message Agent opcmsga (14712) is running
Message Interceptor opcmsgi (13512) is running
WMI Interceptor opcwbemi (6608) is running

**************************************************
Remote administration completed successfully on all nodes.

============================================================
Received data
Current
-------
Cleaning data

1 Answer 1

8

Careful, $input is an automatic variable used by PowerShell to allow iteration over data passed into a function. Try this instead:

function clean_data($data) {
  Write-Verbose "Received data: $data"
  Write-Verbose "Cleaning data"
  $output = @()
  foreach ($line in $data) {
     Write-Verbose "Processing line: $line"
     if ($line -match 'running') {
          $output += $line
     }
   }
   $output
}
Sign up to request clarification or add additional context in comments.

1 Comment

LOL. I knew it had to be something simple. Either I haven't gotten to the chapter on automatic variables yet or the book I'm reading doesn't cover it. At any rate, thanks so much for the help. It works as I want it to now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.