0

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!

1
  • Probably add what you've discovered as an answer with an explanation, this will offer great benefits to both you and the community. Commented Jul 30, 2015 at 7:47

1 Answer 1

1

Although OP has found the answer I am putting this here in case the link in OP breaks.

As per https://technet.microsoft.com/en-us/library/hh847849.aspx

Windows PowerShell protects access to variables, aliases, functions, and Windows PowerShell drives (PSDrives) by limiting where they can be read and changed...

...An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or Windows PowerShell drives in one or more scopes.

So for example a variable declared at the script level will be accessible inside of functions defined in the script. A variable declared inside of a function will not be accessible outside that function unless specifically declared in such a way as to allow access, such as declaring the variable global.

Here is a break down of scopes from that same link:

Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.

Local: The current scope. The local scope can be the global scope or any other scope.

Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.

Private: Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.

This is the general information. After this point it gets more complicated than is necessary for this question. The link is included at the top for anyone who needs to go deeper. As the original poster has already figured out the ability to access the variable in question depends on how/where it is declared. In this case simply declaring the variable outside of the function will suffice.

Sign up to request clarification or add additional context in comments.

Comments

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.