If I declare a variable in a function, can I access that variable outside of the function? Meaning let's say I have a function that will run 5 SQL Queries, and I am using a variable $numberReturned to display the results returned from each query. Can I access $numberReturned from outside the function?
$query1 = "Select * from blahblahblah"
$query2 = "Select * from fooo"
$query3 = "Select * from bar"
Execute-Query $query1
if (Execute-Query $query1)
{
if (Execute-Query $query2)
{
Execute-Query $query3
}
}
Write-Host $QueryName & $numberReturned
Function Execute-Query
{
param($QueryName)
#Stuff to Connect To SQL Server Here
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryName
#$connection is declared above in the connection stuff
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$numberReturned = $SqlAdapter.Fill($DataSet)
}
EDIT --- Upon further research (Mainly This Artcile) I found that my issue was all about scope!