Questions- Comparison, Wildcards,Using If,Else, and For Each

Welcome Forums General PowerShell Q&A Questions- Comparison, Wildcards,Using If,Else, and For Each

Viewing 4 reply threads
  • Author
    Posts
    • #270125
      Participant
      Topics: 4
      Replies: 4
      Points: 44
      Rank: Member

      Would anyone be able to help me with a few questions that I’m stuck on?

      1. Comparison: Write the PowerShell code to determine if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint: you can create a $memory variable to test your comparison.

      2. Wildcards: Write the PowerShell code to determine if the string value stored in $servicename starts with ‘bi’. Case does not matter.

      3. Using If: Write the PowerShell code to write output to the terminal of ‘Memory is too low!’ if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint, you can sue the code from #1 above!

      4. Using Else: Add to the code in #3 to write output to the terminal of ‘You have enough memory!’ if the condition is not met

      . 5. Using ForEach: Assume you have a collection of services in a variable called $services. Using foreach, write PowerShell commands that iterate through the list of services and refreshes each service. Hint: Look for a method on Service objects called ‘Refresh’.

      • This topic was modified 3 weeks, 1 day ago by Andrzac79.
    • #270134
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      This looks a lot like a homework assignment, so I’m not going to just give you the answers, but I will point you in the right direction.

      1. Comparison: Write the PowerShell code to determine if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint: you can create a $memory variable to test your comparison.

      Get-Help About_Comparison_Operators

      Hint: -lt

      2. Wildcards: Write the PowerShell code to determine if the string value stored in $servicename starts with ‘bi’. Case does not matter.

      Get-Help About_Comparison_Operators

      Hint: -like

      3. Using If: Write the PowerShell code to write output to the terminal of ‘Memory is too low!’ if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint, you can sue the code from #1 above!

      Get-Help About_If

      Hint: In ISE hold down CTRL and press j.  Type if for a code snippet.

      4. Using Else: Add to the code in #3 to write output to the terminal of ‘You have enough memory!’ if the condition is not met

      Same as above.  For writing to the terminal Get-Help Write-Host

      . 5. Using ForEach: Assume you have a collection of services in a variable called $services. Using foreach, write PowerShell commands that iterate through the list of services and refreshes each service. Hint: Look for a method on Service objects called ‘Refresh’.

      Get-Help About_ForEach

      Get-Service | Get-Member (This will show the properties and methods of service objects).

       

      This should get you well on your way.  If you want to post back your answers, I’ll give you some feedback.

      • #270161
        Participant
        Topics: 4
        Replies: 4
        Points: 44
        Rank: Member

        Hi sorry I wasn’t just digging for the answers.

        Mike I appreciate you responding to me and for the tips.  I’ve figured out the first four, but am stuck on the last one.  This is what I have so far.

         

        for ($services in get-service | select refresh)

        {                Write-Host $Services

        }

    • #270137
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      We’re not here to do your homework. Post code and what exactly is not working if you want assistance.

    • #270167
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Since $services is already an array of services per the stem of the question, there is no need to call Get-Service again.  Just build your ForEach loop to iterate over $Services.  Inside the ForEach loop call the Refersh method of the current object.

       

    • #270173
      Participant
      Topics: 4
      Replies: 4
      Points: 44
      Rank: Member

      Ok thanks for your help!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.