Would anyone be able to help me with a few questions that I’m stuck on?
-
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.
-
Wildcards: Write the PowerShell code to determine if the string value stored in $servicename starts with ‘bi’. Case does not matter.
-
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!
-
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 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.
- 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
- 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
- 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.
- 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.
We’re not here to do your homework. Post code and what exactly is not working if you want assistance.
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
}
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.
ForEach ($s in $Services) {
$s.Refresh()
}