Solved

Powershell Functions

Posted on 2012-04-12
4
426 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

16 Experts available now in Live!

Get 1:1 Help Now