Solved

Powershell Functions

Posted on 2012-04-12
4
435 Views
Last Modified: 2012-04-12
I'm kinda new to Power-Shell and my previous scripting experience was DOS Batch as well as a little Perl several years ago, but in both cases most of my scripts were at the largest about 100 lines so I never bothered much with Functions. I am now attempting to write a very large script and wanted to keep it organized by using Functions, but I'm having issues...

Can somebody explain to me how to make variables work between functions and the root level of the script in a powershell script? For example, why doesn't this work:
Function Get-NewUserInfo
{
$fname = (Read-Host "First Name")
$lname = (Read-Host "Last Name")
$mi = (Read-Host "Middle Initial")
$job = (Read-Host "Job Title")
$dept = (Read-Host "Department")
}

Get-NewUserInfo

Write-Host ""
Write-Host $fname
Write-Host $mi
Write-Host $lname
Write-Host $job
Write-Host $dept

Open in new window


Thanks,
-Al
0
Comment
Question by:Cacophony777
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 37838499
The default scope of vars is "local to script block". A function builds an own script block, and hence all vars set therein are volatile. You can use something like
$script:fname = (Read-Host "First Name")

Open in new window

in your function. If you use $global, the vars are set for the scope of the PowerShell session.
0
 
LVL 1

Author Comment

by:Cacophony777
ID: 37838592
So once I call that variable by $script:fname is it available to the entire script for the duration of it's run? ...or if another function in my script needs to modify that same variable do I have to again refer to it by $script:fname?

Also, is there a way to tell my script that I want all variable I use set to the "Script" scope?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 37838729
If you access $fname, it should be available everywhere. However, if you try to set it, you will create a local instance of that var. Example:
function test
{
  Write-Host $var
  $script:var = "set in function,#1"
  Write-Host $var
  $var = "set in function,#2"
  Write-Host $var
}

Cls
$var = "set in script"
test
Write-Host $var

Open in new window

You can't change or set a default scope, sorry. It is an intentional implemention detail of PowerShell that all changes should be local by default.
Causing side-effects like setting global vars in a function is considered bad practice anyway. You should construct an object, change it attributes, and use that object for the "interface":
Function Get-NewUserInfo ($emp)
{
  $emp.fname = Read-Host "First Name"
  $emp.lname = Read-Host "Last Name"
  $emp.mi    = Read-Host "Middle Initial"
  $emp.job   = Read-Host "Job Title"
  $emp.dept =  Read-Host "Department"
}

$emp = New-Object PSObject
"fname","lname","mi","job","dept" | % { Add-Member -InputObject $emp NoteProperty $_ "" -PassThru }
      
Get-NewUserInfo($emp)

$emp | format-list

Open in new window

I know this is much different from cmd batches, however it is an important step to write "PowerShell'ish" in a proper way ;-).
0
 
LVL 1

Author Closing Comment

by:Cacophony777
ID: 37839289
Thanks... great explanation!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now