0

I'm exasperated, trying not to resort to using a $Global variable to pass variable, I've seen the other seemingly identical "subjects" in this forum but in the details I am lost or it appears that they are doing something different - or I just don't understand the question or solution.

I am getting an array of event objects from a Get-WinEvent in func1. I want to return that array and use it as an argument for another func2. CASE#1

 $evtArray = Get-WinEvt -FilterHashtable @{ LogName=Application } #simplified for brevity
 return $evtArray  #not sure if working or not
 return ,$evtArray #not sure if working or not
 return @,$evtArray #not sure if working or not
}**```

```**function func2($evtArray) { #doesn't work
function func2( ,$evtArray ) #syntax error
function func2( ,($evtArray)) #syntax error
  ....further processing of the $evtArray occurs......**```

CASE#2 (may be the same as CASE#1 - not sure)
```**$array += ,(<date-time1>,[int],"string")
$array += ,(<date-time2>,[int],"string")**
....
Then pass $array as argument to a function
**func2($array)**

I've spent some hours on this and seen a dozen promising looking same subjects......

3
  • You don’t call functions using parenthesis. Call it like Get-Something -SomeParameter $evtarray Commented Feb 26, 2021 at 0:26
  • Right, old habits, I wasn't doing that in my code, func2 $array or func2 ,$array or func2 ,(array) .... this appears to a problem for a lot of people, I see a lot of people with same questions but no clear answer..... Commented Feb 26, 2021 at 0:32
  • Your code is hard time o follow. Where is func1? Commented Feb 26, 2021 at 0:38

1 Answer 1

1

I recommend you do not use return. It ends the current function and is generally unnecessary. To pass arguments to a function, do not enclose in parenthesis. The arguments should be space delimited and preferably you name the parameters, although that is not required.

$evtArray = Get-WinEvent -FilterHashtable @{ LogName='Application'; ID = 0}

Function func2($Array){
    $array.gettype()
}

func2 $evtArray

As I said before you don't have to name the parameter and instead use positional arguments, but best practice for scripts/modules is to use full names.

func2 -Array $evtArray

Output when called either way is

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     True     Object[]                                 System.Array
Sign up to request clarification or add additional context in comments.

4 Comments

Doug, Thank so much for your insight. I'm a n00bie at powershell, but in your example $evtArray is global scope var and I am really trying to avoid doing that.....'function func1 { $evtArray = Get-WinEvent -FilterHashtable @{ LogName='Application'; ID = 0} return ,evt$array' 'function func2($tmparray) { .........} ' 'func2 = func1'
Ok, here is what I have working.... I've seen some ways to pass array as argument but they are complicated and the solutions appear to assume the reader knows a lot more about powershell than I do
``function firstFunction { $array = 1,2,3 return ,$array } function secondFunction { $tmpArray = firstFunction Write-Host $tmpArray }`
News flash! I got this to work! func2 @(func1)

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.