Learn Windows PowerShell3 In a Month of Lunches 2nd Edition Chapter 9 Lab

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 9 months ago.

  • Author
    Posts
  • #23101
    Profile photo of bruce blake
    bruce blake
    Participant

    I am trying to learn PowerShell using Learn Windows PowerShell3 In a Month of Lunches 2nd Edition

    The Chapoter 9 Lab for number 4 had this question:
    Write a command that uses pipeline parameter binding to retrieve a list of running processes from every computer in an AD domain.
    Don't use parentheses.

    And this was the answer (which was the same answer that I came up with) but I do not think it is working correctly.

    get-adcomputer -filter * | Select-Object @{Label='computername';Expression={$_.name}} | Get-Process

    Here is the result:

    computername
    ————
    LYNCDC6
    LYNCSE6
    LYNCEXUM6
    MINTZ

    from running this part of the command:

    get-adcomputer -filter * | Select-Object @{Label='computername';Expression={$_.name}}

    Yet, when I run the entire command the results did NOT look correct.

    So I modified the command and added a select and a sort:

    get-adcomputer -filter * | Select-Object @{Label='computername';expression={$_.name}} | Get-Process |
    Select-Object -Property MachineName, ProcessName, Id, VirtualMemorySize | Sort-Object -Property Id

    Now the results shown below tell me I was correct and the results were not what I expected:

    MachineName ProcessName Id VirtualMemorySize
    ———– ———– — —————–
    LYNCDC6 Idle 0 0
    LYNCDC6 Idle 0 0
    LYNCDC6 Idle 0 0
    LYNCDC6 Idle 0 0
    LYNCDC6 System 4 3461120
    LYNCDC6 System 4 3461120
    LYNCDC6 System 4 3461120
    LYNCDC6 System 4 3461120
    LYNCDC6 smss 280 4644864
    ....

    Is it possible that PowerShell 4 needs a different command than PowerShell 3?

    get-host

    Name : ConsoleHost
    Version : 4.0
    InstanceId : d0f58f22-df14-4b5c-b4f0-6b18a2c1f636
    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : en-US
    CurrentUICulture : en-US
    PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    IsRunspacePushed : False
    Runspace : System.Management.Automation.Runspaces.LocalRunspace

  • #23102
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It looks like this might be a bug with Get-Process. I can reproduce it easily on the local computer (running PowerShell 5 with the latest preview):

    $objects = @(
        [pscustomobject] @{ ComputerName = $env:COMPUTERNAME }
        [pscustomobject] @{ ComputerName = '.' }
    )
    
    Trace-Command ParameterBinding -PSHost -Expression {
        $procs = $objects | Get-Process
    }
    
    $procs | sort ID | Format-Table MachineName,ProcessName,ID
    

    In the Trace-Command output, it looks like it's binding both the '.' and the full computer name strings to the ComputerName parameter properly, but for some reason only the first value in the list seems to be actually used when the cmdlet starts retrieving processes. I've poked around in the code a bit with ILSpy, and so far have not spotted where the problem is.

    Edit: Scratch that, found the bug. Will pass the info on to Microsoft. Not sure if this is new to PS 4.0 or not; would have to set up a computer with PowerShell 3 to test.

    In any case, not your fault; your code looks exactly like it should for ByPropertyName pipeline binding to work.

  • #23104
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Bug report filed here, and I also emailed it on to the PS MVP mailing list: https://connect.microsoft.com/PowerShell/feedback/details/1164861

You must be logged in to reply to this topic.