Solved

Return Value from Powershell Function

Posted on 2013-05-17
2
3,853 Views
Last Modified: 2013-05-17
At the risk of asking a "silly" question..  I would like to know the "best practice" for "returning" a value from a PowerShell function..  There are several different ways that this can be done..  I would like to have two different variables to be re-usable elsewhere in the script..

# http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/4da65aaa-f261-4d7c-8726-d7613f56d0eb/
#[PowerShell] - Return value from a function
Function showval
{
$val="keeper"
#Return $val
$val2="forkeeps"
Return ,$val,$val2
}

$ReUse = showval
#Method 1 - ForEach
$ReUse | ForEach { `
  "Item: "+$_
  }

#Was thinking of how to implement a counter for the variable
  #Get-Content foo.txt  | foreach { '{0} {1}' -f $_.ReadCount, $_ }

#This also works..
# Foreach ($Reuses in $Reuse) { `
#  "Item: $Reuses" `
# }

#Use of split works too..
#Write-Host "ReUse "$ReUse # shows: keeper forkeeps
#$dnstream=$ReUse.Split(",")[0]
#$dnstream1=$ReUse.Split(",")[1]
#Write-Host "value1 "$dnstream # shows keeper
#Write-host "value2 "$dnstream1 # shows forkeeps

Open in new window


Thanks,

Kent
0
Comment
Question by:Kent Dyer
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39176732
here's a simple example:
function returnMV()
{
$data = @()
$data += (Get-Process | select -first 1)
$data += Get-Date
$data += "jonny brown"
return $data  
}

$returnvalue = returnMV
$returnvalue[0]
$returnvalue[1]
$returnvalue[2]

Open in new window


in youe case:
Function showval
{
$data = @()
$val="keeper"
$data+=$val
#Return $val
$val2="forkeeps"
$data+=$val2
Return $data
}

Open in new window

0
 
LVL 17

Author Closing Comment

by:Kent Dyer
ID: 39176759
Way too simple..  Sheesh..

Thank you!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

12 Experts available now in Live!

Get 1:1 Help Now