none
Is there a way to make a condition based on one row of output? RRS feed

  • Question

  • In this blog entry, a couple scripting guys explained how to get the active power plan for a list of computers.

    What if I want to categorize those results based on the active power plan?

    Monday, November 16, 2015 7:49 PM

Answers

  • Thanks, I think I have it:

    $activePlan = Get-CimInstance -Namespace root/cimv2/power -ClassName Win32_PowerPlan -Filter "IsActive = 'True'"
    $activePlan.ElementName

    • Proposed as answer by Bill_StewartModerator Tuesday, November 17, 2015 9:18 PM
    • Marked as answer by KWFJ Tuesday, November 17, 2015 9:30 PM
    Tuesday, November 17, 2015 4:58 PM

All replies

  • Please clarify what you mean by "categorize".

    What have you tried?


    -- Bill Stewart [Bill_Stewart]

    Monday, November 16, 2015 7:58 PM
    Moderator
  • I mean if (-property elementName != "High performance") {...}
    Monday, November 16, 2015 7:59 PM
  • Yeah, what you want is difficult to understand. Do you also want to know which is active? If so, try something like the example below.

    Get-WmiObject -Class win32_powerplan -Namespace root\cimv2\power | select ElementName,@{N='ComputerName''E={$_.__Server}},@{N='Active';E={$_.isActive}

    It's not necessary, but I've used calculated properties in the example above. Here's the same thing without any changes to the property names. Both of these example return which plan is active.

    Get-WmiObject -Class win32_powerplan -Namespace root\cimv2\power | select ElementName,__Server,IsActive



    Monday, November 16, 2015 8:02 PM
  • PowerShell does, indeed, have an if statement. The documentation describes it pretty well:


    PS C:\> help about_If

    PowerShell doesn't use != as an inequality operator but rather -ne - again, the help is instructive:


    PS C:\> help about_Comparison_Operators
    

    What have you tried so far?


    -- Bill Stewart [Bill_Stewart]

    Monday, November 16, 2015 8:05 PM
    Moderator
  • I think I will try to set a variable equal to tommy's helpful pipe command, and then, I'll use the variable in an if statement. I might need to use a for each loop on the input.
    Monday, November 16, 2015 8:18 PM
  • A basic primer on how objects work in PowerShell might be helpful.

    Windows IT Pro - PowerShell: Objects and Output


    -- Bill Stewart [Bill_Stewart]

    Monday, November 16, 2015 8:24 PM
    Moderator
  • I'm fairly confident that you're only ever going to have one power plan active at a time -- seems likely. In that case, you could filter either of my commands better, and set that result, as the value of your variable. It'll end up being a touch easier to compare, if you do it that way.

    One way to filter (the much preferred/best practice way), would be to use Get-WmiObject's -Filter parameter (Get-Help Get-WmiObject -ShowWindow). The second way, which I would avoid since you can filter on the first cmdlet (always a better choice), is to pipe to the Where-Object cmdlet. You should know that both options exists, but also know which is better. While piping is powerful, it's not always the best, or first choice.


    Monday, November 16, 2015 8:47 PM
  • Thanks, I think I have it:

    $activePlan = Get-CimInstance -Namespace root/cimv2/power -ClassName Win32_PowerPlan -Filter "IsActive = 'True'"
    $activePlan.ElementName

    • Proposed as answer by Bill_StewartModerator Tuesday, November 17, 2015 9:18 PM
    • Marked as answer by KWFJ Tuesday, November 17, 2015 9:30 PM
    Tuesday, November 17, 2015 4:58 PM
  • Nice work. You're definitely filtering the better of two different ways, and returning the results you were after. Well done.
    Tuesday, November 17, 2015 6:02 PM
  • Agreed, nicely done.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, November 17, 2015 9:18 PM
    Moderator