?
Solved

Return Value from Powershell Function

Posted on 2013-05-17
2
Medium Priority
?
4,044 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

762 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