John Savill'ss FAQs on IT Pro Today Hero

Savill's FAQs: Enabling Access to Help in PowerShell Sessions

Three times a week, John Savill tackles your most pressing IT questions. Today, find out about configuring PowerShell sessions so the help subsystem is available, receiving alerts when your Azure B series CPU credits are below a certain level, and breaking out of loops in a PowerShell search.

Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

Today: Accessing the Help subsystem from within a PowerShell session; keeping an eye on your Azure B series CPU credits; and using loops to break out of a PowerShell search once you have your results.


Q. I've created a PowerShell session configuration with a limited set of commands however help fails within, why?

A. To use the help subsystem access to the file system is required which is restricted by default. To enable help to function, add the FileSystem as a visible provider, i.e. add

-VisibleProviders FileSystem

to the configuration file. For example:

New-PSSessionConfigurationFile –ModulesToImport ActiveDirectory, Microsoft.PowerShell.Utility `
–VisibleCmdLets ('*AD*','format*','get-help') `
-VisibleFunctions ('TabExpansion2') -VisibleAliases ('exit','ft','fl') –LanguageMode ‘ConstrainedLanguage’ `
-VisibleProviders FileSystem `
–SessionType ‘RestrictedRemoteServer’ –Path ‘c:\onemtconly.pssc’

Register-PSSessionConfiguration -Name "DCMs" -Path C:\onemtconly.pssc

Q. How can I get an alert if my Azure B series CPU remaining credits drop below a certain value?
Dept - Azure

A. The B-series provides a VM series where the base CPU allocation is less than 100%. For example if you're allocated 10% of a vCPU and if you use less than that 10%, you accumulate credit. You can use the credit in the future to burst above 10%. Save enough and you could push to100% during a period of time. My full walkthrough of the B-series is embedded below:

As part of standard Azure functionality, there are various monitors available to gain insight into the utilization of resources. There is a monitor called CPU Credits Remaining and it shows the CPU credits for a B series VM. Alerts can be created for monitors. So to be notified if your CPU credit drops below a certain value, simply create an Alert. For example:

  1. Open your B series VM in the Azure portal
  2. Under Monitoring select Alert rules
  3. Click the Add metric alert
  4. Create a new alert based on the CPU Credits Remaining monitor and the level you wish to be notified at, for example:

Customize any people that should be notified and click OK.


Q. How can I break out of a loop in PowerShell?

A. Normally in code, a good practice is to avoid any wasted computation. If I'm searching a collection of objects, once I find a match, it's a waste of resource to continue to search through all the other objects. One way to solve this is by using a do...while structure which could be based around a condition of $notfound (or something like that). If this is not possible another approach is to simply break out of the loop using the break command. For example:

foreach($LABOU in $LABOUs)
{
    if($LABOU.Name -eq $LABCode)
        { $LABFound = $true ; break }
}

Note if I had just an array of regular strings and I wanted to see if it contained a string I could have just used -contains, for example:

$strings = ("apple","orange","lemon")
$strings -contains "apple"

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish