Link to home
Start Free TrialLog in
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!
ASKER CERTIFIED SOLUTION
Avatar of elijah eccher
elijah eccher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of J0rtIT
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elijah eccher
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.