troubleshooting Question

Return Multiple Powershell Variables

Avatar of elijah eccher
elijah eccher asked on
Powershell
4 Comments2 Solutions54 ViewsLast Modified:
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!
EXPERT CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros