PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
- All output is captured, and returned
- The return keyword really just indicates a logical exit point
Thus, the following two script blocks will do effectively the exact same thing:
Copy$a = "Hello, World"
return $a
Copy$a = "Hello, World"
$a
return
The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).
Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.
It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.
You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.
PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
- All output is captured, and returned
- The return keyword really just indicates a logical exit point
Thus, the following two script blocks will do effectively the exact same thing:
Copy$a = "Hello, World"
return $a
Copy$a = "Hello, World"
$a
return
The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).
Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.
It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.
You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.
This part of PowerShell is probably the most stupid aspect. Any extraneous output generated during a function will pollute the result. Sometimes there isn't any output, and then under some conditions there is some other unplanned output, in addition to your planned return value.
So, I remove the assignment from the original function call, so the output ends up on the screen, and then step through until something I didn't plan for pops out in the debugger window (using the PowerShell ISE).
Even things like reserving variables in outer scopes cause output, like [boolean]$isEnabled which will annoyingly spit a False out unless you make it [boolean]$isEnabled = $false.
Another good one is $someCollection.Add("thing") which spits out the new collection count.
Why not use the return statement?
Explaining Rationale for Function Output Behavior
newbie question on function return values
[V1.2] Blueprint Roblox Editor : Visual programming tools for Luau - Community Resources - Developer Forum | Roblox
Videos
Coming from a managed code background, I use return in just about every function I write. When I want to return a collection to the pipeline, I add a # return comment so I know that I explicitly meant to return the object. For the community and my coworkers... I am on the wrong side of this battle. I'll continually receive code reviews saying I should not use the return statement, but I think thats their choice. I use it for the purpose of being explicit. I've helped the same people who want me to stop using it debug their broken code because they accidentally were returning something else into their pipeline that they didn't want there. Not just once, but at least once a sprint. Each.
So, grand PowerShell community, what's up with the pushback on return?
Hi ! I have 2 function in my Script powershell. One get the value of the user and the other one use the values. I found a way to return multiple values from a function (Table).It’s working perfectly .But the values are not recognized by the second function ($Return[1] is empty). Do anyone knows the problem of my code ?
Thanks
$Button2.Add_Click({test1})
$BtnConfirm.Add_Click({test2})
Function test1($Folders,$CheckValue, $UserResearch)
{
$Folders = $TextBoxLink.Text
$CheckValue = $True
if($CheckBoxYes.Checked -eq $true)
{
$CheckValue = $True
}
elseif($CheckBoxNo.Checked -eq $true)
{
$CheckValue = $False
}
$UserResearch = $TextBoxUser.Text
Return $Folders,$CheckValue,$UserResearch
}
$Return = test1
Function test2($Return)
{
###################################### SI FILTRE ################################
if($Return[1] -eq $True)
{
New-Item -Name "superCaMarche.txt" -Path \\srv-files\RNS_Services\Informatique\Test_Depraz -Value $CheckValue
#Récupère le nom de domaine
$Domain = (Get-ADDomain).NetBIOSName
#Sotck dans une variable les objects du dossier donné par l'utilisateur
$Folders = Get-Item -Path $Path
#Parcours le dossier ou l'arborescence
ForEach ($Folder in $Folders)
{
#Récupère les droits sur un dossier et les stocks
$ACLs = Get-Acl $Folder.FullName | ForEach-Object { $_.Access }
#Parcours tous les droits sur un dossier
ForEach ($ACL in $ACLs)
{
#Controle si il existe bien des droits
If ($ACL.IdentityReference -match "\\")
{
#Controle si le dossier possède des droits lié au domaine
If ($ACL.IdentityReference.Value.Split("\")[0].ToUpper() -eq $Domain.ToUpper())
{
#Entre dans une variable le nom du groupe AD
$Name = $ACL.IdentityReference.Value.Split("\")[1]
#Vérifie que l'object récupéré est bien un groupe
If ((Get-ADObject -Filter 'SamAccountName -eq $Name').ObjectClass -eq "group")
{
#Parcours les informations du groupe récupéré
ForEach ($User in (Get-ADGroupMember $Name -Recursive | Select -ExpandProperty Name))
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = $Name
User = $User
FileSystemRights = $ACL.FileSystemRights
AccessControlType = $ACL.AccessControlType
Inherited = $ACL.IsInherited
}
$Result | Select group,FileSystemRights,AccessControlType,Inherited, User |Where-Object{$User.Equals($UserResearch)} |export-csv \\srv-files\RNS_Services\Informatique\Test_Depraz\"$UserResearch".csv -Append
#$Result | Select Path,Group,User,FileSystemRights,AccessControlType,Inherited
}
}
}
}
}
}
}
Either return a string that you then parse out separate values using delimiters, or return a data structure like an array.
All right, so the basics: I have a 1 button, the first button calls a function which returns an array. I have another button that needs to use the array from the first function. I've tried calling the first function from the second function and have a return statement for my array. The code for this is pretty long, but in a general sense it's just how to I return an array from one function and then use it in a second function. I've been Googleing all yesterday and haven't found anything. B…