trying out foreach-object - need explaination

This topic contains 2 replies, has 3 voices, and was last updated by  Dudebro 3 months, 1 week ago.

  • Author
    Posts
  • #93459

    squigee
    Participant

    Hey there,

    First question is why is it returning "False" and the "else" to the screen.

    As I see its returning true/false if the object passed includes "svchost" so I tried to create a if statement, but it doesn't work, what am I doing wrong there. Also if anyone asks why am I doing this, its because I trying to understand the logic of the ForEach statement and what it's used for.

    Get-Process | ForEach-Object {($_.Name -in "svchost")
    
     if ($_.Name -eq "True") {
     
     Write-Host "Testtttt"
     }
     
     {
    
    else "Noppppe"
    
    }
    
    }
    

    Getting the following output (small example).

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    else "Nope"

    False

    False

    else "Nope"

    False

  • #93460

    postanote
    Participant

    This looks like you are new to PowerShell, but that is just an assumption based on your post. If true, you really should take a couple of no cost online courses, so that you are better prepared for the adventure with PowerShell and use that as a launching pad to learn more.

    Learn PowerShell: Microsoft Virtual Academy – Getting Started with Microsoft PowerShell.
    'mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276'

    Youtube search for beginning PowerShell
    'youtube.com/results?search_query=beginning+powershell'

    And take full advantage of this guide.

    Windows PowerShell Survival Guide

    Purpose of this Document

    The purpose of this document is to help you to learn more about PowerShell and to be successful in applying it. This document seeks to point to the best content on the web to enable you to reach that goal.

    Scope of this Document
    This page contains links to help you learn more about Microsoft Windows PowerShell. This includes PowerShell fundamentals as well as how PowerShell is used in Windows applications and services. As long as it's PowerShell related, we'll try to point to it! The document is also version agnostic, and contains information about current and future versions of PowerShell.

    'social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx'

    As for ...

    because I trying to understand the logic of the ForEach statement and what it's used for.

    ... Loops are not unique to PowerShell, it has been around in programming since the beginning of programming. All languages use this as well as on looping structures. This is well documented in every place on the web, books and courseware.

    For Loop

    A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.

    'cs.utah.edu/~germain/PPS/Topics/for_loops.html'

    What you need to know about foreach loops in PowerShell

    If you're familiar with common coding practices, you're probably also familiar with a foreach loop. A foreach loop is a simple language construct that enables you to iterate through a set of items in a collection or array. Every programming language has foreach loops, and they all tend to behave in the same way. PowerShell, on the other hand-with its pipeline, built-in cmdlets, and use of aliases-sometimes trips up newbies.

    'pluralsight.com/blog/it-ops/what-you-need-to-know-about-foreach-loops-in-powershell'

    PowerShell Loops

    Table of Contents
    ForEach-Object
    For
    While
    Do while
    Do Until
    See Also
    Other Languages

    'social.technet.microsoft.com/wiki/contents/articles/4542.powershell-loops.aspx'

    PowerShell Tutorial – Loops (For, ForEach, While, Do-While, Do-Until)

    ForEach Loop

    The foreach statement is very useful in PowerShell when you need to loop through an array or loop through items in an object.

    ForEach allows the loop to evaluate the number a certain type of objects in an array or parent object and loop through those objects. One thing to bear in mind is that a ForEach loop is that there is a performance penalty for this convenience.

    The below example, shows a ForEach Loop operating on a object, the code assigns the object resulting from the Get-process cmdlet to a variable, and then iterates through the process items in the object and displays the process name.

    'winserverhelp.com/2010/04/powershell-tutorial-loops-for-foreach-while-do-while-do-until'

        $Processes=get-process | select-object ProcessName
    
        $i=1
    
        foreach ($process in $Processes)
        {write-host $i "Process Name is " $process.Processname;$i++; }
    

    You also need a better understanding of the comparison operators.
    That'-in' is used in looking at a collection, and your post is only a simply string. There is nothing in simple string other that the characters.

    About Comparison Operators

    Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.

    'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1'

  • #93469

    Dudebro
    Participant

    ForEach-Object runs its script block once for each object you send it. The "$_" automatic variable represents the current object.

    Get-Process | ForEach-Object {
    
        If   ( $_.Name -EQ 'svchost' ) { Write-Host 'Coool!' }
        Else                           { Write-Host 'Uncool' }
    }
    

You must be logged in to reply to this topic.