In PowerShell, functions return any values returned by each line in a function; an explicit statement is returnnot needed.
The method String.IndexOf()returns an integer value, so in this example it DoSomethingreturns "2", and the hash table as an array of objects, as shown in .GetType().
function DoSomething {
$site = "Something"
$app = "else"
$app.IndexOf('s')
return @{"site" = $($site); "app" = $($app)}
}
$siteInfo = DoSomething
$siteInfo.GetType()
The following example shows 3 ways to block unwanted output:
function DoSomething {
$site = "Something"
$app = "else"
$null = $app.IndexOf('s')
[void]$app.IndexOf('s')
$app.IndexOf('s')| Out-Null
@{"site" = $($site); "app" = $($app)}
}
$siteInfo = DoSomething
$siteInfo['site']
Here is an example of how to wrap multiple statements in ScriptBlock to capture unwanted output:
function DoSomething {
$null = .{
$site = "Something"
$app = "else"
$app
}
@{"site" = $($site); "app" = $($app)}
}
DoSomething