We help IT Professionals succeed at work.
Get Started

Return Multiple Powershell Variables

elijah eccher
on
50 Views
Last Modified: 2019-12-13
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!
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 2 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE