1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Powershell - How do these scripts work? Need HELP!

Discussion in 'All Other Software' started by HorrorFreak, Nov 17, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. HorrorFreak

    HorrorFreak Thread Starter

    Joined:
    Apr 23, 2009
    Messages:
    54
    Hello,

    I have an exam next Monday night and while we have covered all of the basic scripting fundamentals in Powershell, I am still have a problem grasping how some scripts work. I have seven tiny scripts and I was wondering if the kind people here could give me some understanding as to how powershell generates the output of each script.

    I have asked my teacher a number of times but due to a language barrier, I have hit a wall. I figured on a forum dedicated to Powershell and scripting would offer a better understanding. Basically I NEED to pass this exam. The exam is set up that we are given a number of scripts like the one listed and we need to analyse and enter what the exact output will be from the script. All and any help is greatly appreciated.

    Script 1

    for ($i = 1; $i -le 10; $i++)
    {
    $product = $product * $i
    }
    write-host $product



    Output is blank. I'm assuming it is blank because $product has not been assigned any sort of variable therefore multiplying it by $i voids the output?



    Script 2

    $num1 = 6
    $num2 = 2
    Do {
    $Num3 = $num1 - $num2
    $num2 += 2
    }
    while ($num2 -eq $num1)
    write-host $num3



    Output is 4. $Num3 = 6 - 2, so 4. $num 2 is now equal to 4. While $num2 (is it still 2 or now 4?) is equal to 6, which it never is so DO would not happen, therefore why is $Num3 4?
     
  2. HorrorFreak

    HorrorFreak Thread Starter

    Joined:
    Apr 23, 2009
    Messages:
    54
    Script 3

    $num1 = 6
    $num2 = 2

    do
    {
    $num3 = $num1 - $num2
    $num2 += 2
    }
    until($num2 -eq $num1)
    write-host $num3


    Output is 2. So $num3 = 6 - 2 so 4. $num2 is now 4. So DO that UNTIL (so it meets the criteria) $num2 (does it = 2 still or now 4) = 6 which it never does. So write-host $num3 would be 4???


    Script 4

    $num1=3
    $num2=2

    while ($num3 -le $num2)
    {
    $num3 = $num1 - $num2
    $num1 += 3
    $num2 += 1
    write-host $num2
    }


    Output is

    3
    4
    5
    6

    So while 3 is less than 2, which it is; $num3 = 3 - 2... actually, no idea :(
     
  3. HorrorFreak

    HorrorFreak Thread Starter

    Joined:
    Apr 23, 2009
    Messages:
    54
    Script 5

    for ($i = 1; $i -le 4; $i++)
    {
    $sum = $sum+$i
    }
    write-host $sum


    Output is 10. Again no idea :(

    Thanks for any help or input you people may have for me.
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    This is programming 101. A basic incremental loop! I don't program PowerShell and even I understand it.

    Basically this script increments itself 4 times because the 2nd arguement is $i -le 4. That basically says while $i is less than or equal to 4 continue the for loop. The first argument in the for loop tells you that $i is assigned the value of 1. The last argument says increment $i by a value of 1 each time through the loop.

    So the first time through the loop $i equal 1 and $sum equals 0 because it hasn't initially been assigned anything. The value of $sum after the first loop is now 1 because 1+0=1.

    Second time through the for loop $i now equals 2. So now the value of $sum equals 3 because 1 + 2 = 3.

    Third time through the for loop $i now equals 3. So now the value of $sum equals 6 because 3 + 3 = 6.

    4th and final time through the for loop $i now equals 4. Snow the value of $sum equals 10 because 4 + 6 = 10
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I am not sure how you cannot follow the logic on this. Write out what each variable equals on a piece of paper as you loop through the code.
    num1=3
    num2=2
    num3 is less than num2 because num3 is not defined yet.
    num3=1
    num1=6
    num2=3
    1 is less than 3 so the loop continues
    num3 now equals 3 because 6 -3 = 3
    num1=9
    num2=4
    3 is less than 4 so the loop continues
    num3 now equals 5 because 9 - 4 = 5
    num1=12
    num2=5
    5 equals 5 so the loop continues
    num3 now equals 7 because 12 - 5 = 7
    num1=15
    num2=6
    num3 is no longer less than or equal to num2 because 7 is greater than 6.
    The loop is done so it outputs the number 6 to the screen.
     
  6. HorrorFreak

    HorrorFreak Thread Starter

    Joined:
    Apr 23, 2009
    Messages:
    54
    Thank you Squashman. This is my first exposure to any sort of programming and had our teacher explained it the way you just have, step by step, I doubt I would have been as stuck.

    Your explanations were exactly what I was looking for, much appreciated.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1027322

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice