Question

IN POWERSHELL     write a script that retrieves a collection of objects (an array) loops them through...

IN POWERSHELL     write a script that retrieves a collection of objects (an array) loops them through a foreach loop and displays one property of the object with the write-host cmdlet.

Homework Answers

Answer #1

Following is a script in powershell that initiates a array of fruits and loops through them by a foreach loop and displays the object using write-host.

here,

  • $fruits = @("mango","apple","banana","guava","peaches","melons")
    this statement declares the list of string objects, we have declared 6 objects
  • foreach ($fruit in $fruits)
    this statement will use a for each loop on the above fruits object, here fruit variable is set as name of object being traversed.
  • Write-Host $fruit
    here, this statement will print the fruit object being traversed using write-host

TO RUN : & ".\run.ps1"

FILE : run.ps1

$fruits = @("mango","apple","banana","guava","peaches","melons")

 foreach ($fruit in $fruits) {
   Write-Host $fruit
 }

OUTPUT:

mango
apple
banana
guava
peaches
melons

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included Download Lab04.zip for all of the additional supporting files that you will need to compile and run. Extract the files into your working directory You can find the rules of blackjack all over the Internet. To get an idea of what you are trying to accomplish in this lab, I’ll demonstrate the final solution. The dealer stands on all 17s. Doubling after splitting is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT