Retrieve a collection ob objects, and loop through them.
I am taking a college course for my very last credit needed for my associates degree. The class is online and during the summer so its hard to receive any help from the college or students. The course is just an elective that was available for me to take in order to graduate early, I will most likely never use any of this in my career or future. So any help would be appreciated. 

Please provide detail in your response on how you answered the question so that I can maybe understand it for myself. 

Thank you in advance for any help. 

***************************************... 
MY TASK 

-Write a script that prints out the first 100 multiples of 2 
You must use a for loop or while loop 

-Write a script that retrieves a collection of objects (an array), loops through them, and displays one property (your choice) of the object with the Write-Host cmdlet 
You must use a for loop or while loop 

***************************************... 

I believe I figured out the first question using this command in powershell. 

$a = 2 
$b = 1 
$c = $a * $b 

write-host "Here are the first 100 multiples of 2.`n`nStarting at 2x1 and ending at 2x100" 
do 

write-host 
$a * $b 

$b++ 

$c = $a * $b 
}while($c -ne 202) 

That seems to work, but let me know if I am completely wrong. 

The other question is where I am completely lost.
June 28th, 2015 2:51pm

Your script works, but can be shortened and simplified. Have a look in the documentation for examples of how to use for and while:

about_For

about_While

As for your second task, you can retrieve a collection of objects using for instance Get-Process. Use dot notation or Select-Object to get properties from objects. You will need to find the count of objects in your collection to use in your loop.

One last tip is to write your script in PowerShell ISE. Good luck ;)

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 4:55pm

Your script works, but can be shortened and simplified. Have a look in the documentation for examples of how to use for and while:

about_For

about_While

As for your second task, you can retrieve a collection of objects using for instance Get-Process. Use dot notation or Select-Object to get properties from objects. You will need to find the count of objects in your collection to use in your loop.

One last tip is to write your script in PowerShell ISE. Good luck ;)

June 28th, 2015 4:56pm

Your write-host is not doing anything.  The loop also does nothing.  You have not read the instruction correctly. It wants a annray of objects to work on. You are juust loopin on nothing.

Here is a hint:

# array here;
$a=1..100

# loop on array

foreach($x in $a){
    write-host "X is $x and X times 2 is $($x * 2 )" -fore green
}

The purpose of this in college is not to turn you into a programmer but to teach you how to think like a human being.  You are clearly in need to this challenge as you have not given this any though.  T+YOU start with a lame disclaimer and then expect others to do the thinking for you.  YOu will not get a good job with that approach.  Try engaging your brain.  It will shrivel with lack of use.

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 5:54pm

Your script works, but can be shortened and simplified. Have a look in the documentation for examples of how to use for and while:

about_For

about_While

As for your second task, you can retrieve a collection of objects using for instance Get-Process. Use dot notation or Select-Object to get properties from objects. You will need to find the count of objects in your collection to use in your loop.

One last tip is to write your script in PowerShell ISE. Good luck ;)

June 28th, 2015 8:53pm

Your script works, but can be shortened and simplified. Have a look in the documentation for examples of how to use for and while:

about_For

about_While

As for your second task, you can retrieve a collection of objects using for instance Get-Process. Use dot notation or Select-Object to get properties from objects. You will need to find the count of objects in your collection to use in your loop.

One last tip is to write your script in PowerShell ISE. Good luck ;)

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 8:53pm

Your write-host is not doing anything.  The loop also does nothing.  You have not read the instruction correctly. It wants a an array of objects to work on. You are just looping on nothing.

Here is a hint:

# array here;
$a=1..100

# loop on array

foreach($x in $a){
    write-host "X is $x and X times 2 is $($x * 2 )" -fore green
}

The purpose of this in college is not to turn you into a programmer but to teach you how to think like a human being.  You are clearly in need to this challenge as you have not given this any though.  You start with a lame disclaimer and then expect others to do the thinking for you.  You will not get a good job with that approach.  Try engaging your brain.  It will shrivel with lack of use.

June 28th, 2015 9:52pm

Thank you so much I really appreciate it. Yes at first I was using notepad to type my scripts. And then I learned about ISE. That is 1000 times better, I love it. 

Thanks again.

Free Windows Admin Tool Kit Click here and download it now
June 29th, 2015 10:01pm

Thank you so much I really appreciate it. Yes at first I was using notepad to type my scripts. And then I learned about ISE. That is 1000 times better, I love it. 

Thanks again.


Wonderful.  We love this. You should consider this as a career.
June 29th, 2015 10:27pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics