Solved

Return Value from Powershell Function

Posted on 2013-05-17
2
3,818 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
Comment Utility
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
Comment Utility
Way too simple..  Sheesh..

Thank you!!
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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
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…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now