none
2 styles of foreach syntax, each of them makes something different RRS feed

  • Question

  • Good afternoon, 

    I'm PS beginner and I'm just getting familiar with it. There is one thing I can't figure out. I supposed those two snippets will do exactly do same, but I've never been so wrong: 

    <#1st: #>

    Get-Process | % { if($_.WorkingSet -gt 10MB) {Write-Host $_.Name -ForegroundColor Green} else {Write-Host $_.Name -ForegroundColor Green}}

    <#2nd#> $prc = Get-Process foreach ($proc1 in $prc) { if($proc1.WorkingSet -gt 10MB) { Write-Host $proc1.Name -ForegroundColor Red } else { Write-Host $proc1.Name -ForegroundColor green } }

    1st one writes all processes (despite if their working set size) in Green, 2nd one writes all processes which have WS above 10MB in red, all other processes are written in green. 

    Could anyone please point out difference between these two scripts? I thought they'll do exactly the same thing. 

    Thank you in advance.

    Stanley

    Saturday, March 8, 2014 11:30 AM

Answers

  • Mine works just fine.

    get-process |
         %{
              if($_.Workingset -gt 20MB){$c='red'}else{$c='green'}
              write-host $_.Name -fore $c
         }

    Here is what you wrote but it is layed out to be readable.  Look very carefully at what you posted.

    Get-Process|%{
               if( $_.WorkingSet -gt 10MB){
                    Write-Host $_.Name -ForegroundColor Green
               }else{
                    Write-Host $_.Name -ForegroundColor Green
               }
         }


    ¯\_(ツ)_/¯

    • Marked as answer by Sranda Zitra Saturday, March 8, 2014 6:42 PM
    Saturday, March 8, 2014 1:24 PM

All replies

  • Mine works just fine.

    get-process |
         %{
              if($_.Workingset -gt 20MB){$c='red'}else{$c='green'}
              write-host $_.Name -fore $c
         }

    Here is what you wrote but it is layed out to be readable.  Look very carefully at what you posted.

    Get-Process|%{
               if( $_.WorkingSet -gt 10MB){
                    Write-Host $_.Name -ForegroundColor Green
               }else{
                    Write-Host $_.Name -ForegroundColor Green
               }
         }


    ¯\_(ツ)_/¯

    • Marked as answer by Sranda Zitra Saturday, March 8, 2014 6:42 PM
    Saturday, March 8, 2014 1:24 PM
  • Well....this is embarassing...I can't believe I didn't see such a stupid mistake. 

    Thanks for your time. 

    Saturday, March 8, 2014 5:58 PM
  • Well....this is embarassing...I can't believe I didn't see such a stupid mistake. 

    Thanks for your time. 

    I didn't see it either until I decided to reformat the look so it was more readable.  This is why I tend to harp on coders formatting code in a readable way.

    You do not have to put everything on one line as you can see by my examples.


    ¯\_(ツ)_/¯

    Saturday, March 8, 2014 6:27 PM
  • This story of putting kindergarten-like errors on technet forums really taught me a lesson and I'll format my code in better way next time. Have a nice rest of the weekend. 
    Saturday, March 8, 2014 6:36 PM
  • This story of putting kindergarten-like errors on technet forums really taught me a lesson and I'll format my code in better way next time. Have a nice rest of the weekend. 

    Thanks - It is something that was drilled into me many years ago and it has prevented a great many stupid errors.  Unfortunately we humans can still find good ways to do dumb things.  I am sure both of us will continue to make silly errors. Formatting helps to make them more findable although sometimes it is very hard to see ones own mistakes.  The silly mistakes can be the hardest to see.

    Have a good day.


    ¯\_(ツ)_/¯

    Saturday, March 8, 2014 6:49 PM