Avatar of elijah eccher
elijah eccher
 asked on

Return Multiple Powershell Variables

Hi All,

I'm creating a "Loop-caller" cmdlet. supposed to be super simple but I cannot get around how to pass these variables.

The idea is that as an Administrator I often run into little jobs like "check all computers for XXX" where xxx varies anywhere from IP, MAC, installed program, driver version ETC.
I've written tiny modular cmdlets for finding such things.

The idea of this "Loop-caller" is to take a txt file of computernames and call the function for each one, then I want the loop-caller to put the returned data from the info cmdlet into a hash table.

Example:

# Up-Info : This program asks the use for a target computer and then queries that computer for uptime.
param(
    [Parameter(Mandatory)]$ComputerName
)
$computer = get-fqdn $ComputerName          # calls my cmdlet "get-fqdn" which scrapes ping -a -n 1 for a machine's FQDN (our NW has more than one domain.)
$bootTime = (get-date) - (gcim -ComputerName $computer -ClassName win32_operatingsystem).LastBootUpTime
return $computer, $bootTime

# This Returns 2 Variables $computer and $bootTime

# Loop Caller
Param(
    [Parameter(Mandatory)]${Where is the List.txt?},
    [Parameter(Mandatory)]${Which Program are we Looping?}
)
$lsfile =cat ${Where is the List.txt?}
$loopCMD = ${Which Program are we Looping?}
$keys = @{}
for($i=0;$i -lt $lsfile.count;$i++){
   $keys.add{powershell.exe $loopCMD $lsfile[$i]}
}
# This function works great without the $keys = @{} and the $keys.add{}  
# it calls the function and passes the computername which is properly read and the output is displayed on screen.
# I want to store the output as an object instead I would like a hash Table with $computer = $bootTime Where Loop-Caller is creating the hash table as it gets the return values.

is there a way to return 2 variables at once without dumping them into a single string and using split? I don't want to throw away that much information!
Powershell

Avatar of undefined
Last Comment
elijah eccher

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
elijah eccher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jose Gabriel Ortega Castro

Indeed when you want to return several values, save them into a variable (or class if ps version 5.0 or higher)
I'll let you this one :

https://devblogs.microsoft.com/scripting/powershell-5-create-simple-class/

Take care!
EXPERT CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
elijah eccher

ASKER
Thanks so much. I typically avoid extremely vague variable names like 'path' which could be cmd arguments, parameters etc, and I had yet to run into the 'helpmessage' method, so i was using those goofy variables in the mean-time so the mandatory-parameter messages would be user-readable (Which 'path: ' ? so vague and unhelpful.) Furthermore, I was calling new powershell sessions because i was not getting return results, but you've fixed that for me too. I've learned to much about good practice with this one answer. Thank YOU!

Building up-info outside of loop-caller is so that loop-caller can be used in HIGH modularity with all of my OTHER ps scripts.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck