Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Powershell - Pass array from one function to another

$
0
0

All right, so the basics:

I have a 1 button, the first button calls a function which returns an array.

I have another button that needs to use the array from the first function.

I've tried calling the first function from the second function and have a return statement for my array. The code for this is pretty long, but in a general sense it's just how to I return an array from one function and then use it in a second function. I've been Googleing all yesterday and haven't found anything. Below is a short example of what I'm trying to do...

function firstFunction {

$array = 1,2,3

return ,$array

}

function secondFunction {

firstFunction

Write-Host $array

}

Second function doesn't return anything in array from the first function. Any ideas?

Hmm well I made a little test and that actually works...now why doesn't my script work I've no idea...I'll include my functions below...

function getUserLogon
{
$logonfile = "C:\Users\benw\Documents\PowershellScripts\Development\UserTracking\logon*.txt"
$logonfile2 = "C:\Users\benw\Documents\PowershellScripts\Development\UserTracking\archive\*.txt"
	$outputBox.clear()
	$username = $UserComboBox.SelectedItem.ToString()
	$startdate = $StartDateBox.text
	$enddate = $EndDateBox.text
	
	$data = @(get-content $logonfile,$logonfile2| select-string $username)
	
	$array = @()
	
	$start = [datetime]::parseexact($startdate,"MM/dd/yyyy",$null)
	$end = [datetime]::parseexact($enddate,"MM/dd/yyyy",$null)

	if($start.length -gt 0 -and $end.length -gt 0)
	{
	if($start -le $end)
	{
		foreach($line in $data)
			{
				$var = $line
				$var = [string]$var
				$var = $var.substring(4,10)
				$var2 = [datetime]::parseexact($var,"MM/dd/yyyy",$null)
				if(($var2) -ge ($startdate) -and ($var2) -le ($enddate))
				{
					$array += $line
				}
			}
			if($array.length -gt 1)
			{
				$outputBox.text = $array | out-string
			}
			else
			{
				$outputBox.text = "No matches found"
			}
		}
		else
		{
			
			<#
			foreach($line in $data)
			{
				$var = $line
				$var = [string]$var
				$var = $var.substring(4,10)
				$var2 = [datetime]::parseexact($var,"MM/dd/yyyy",$null)
				if(($var2) -le ($startdate) -and ($var2) -ge ($enddate)) #-ge before date -le after date -eq same date
				{
					$array += $line
				}
			}
			if($array.length -gt 1)
			{
				$outputBox.text = $array | out-string
			}
			else
			{
				$outputBox.text = "No matches found"
			}
			#>
			$outputBox.text = "Ensure the Start date is before the End date."
		}
	}
	elseif ($start.length -gt 0 -or $end.length -gt 0)
	{
		foreach($line in $data)
			{
				$var = $line
				$var = [string]$var
				$var = $var.substring(4,10)
				$var2 = [datetime]::parseexact($var,"MM/dd/yyyy",$null)
				if(($var2) -eq ($startdate) -or ($var2) -eq ($enddate)) #-ge before date -le after date -eq same date
				{
					$array += $line
				}
			}
			if($array.length -gt 1)
			{
				$outputBox.text = $array | out-string
			}
			else
			{
				$outputBox.text = "No matches found"
			}
	}
	else
	{
		$outputBox.text = "Please select a Start and End Date"
	}
return ,$array
}

And my second function...

function exportCsvResults
{
	getUserLogon
	Write-Host $array
}

Which writes out nothing...


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>