Script blocks not working


This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Remco_NL Remco_NL 9 months, 3 weeks ago.

  • Author
  • #54743
    Profile photo of Remco_NL

    Hello All, I'm now getting the basics with powershell and i'm watching Powershell Jumpstart on MVA.
    On the part "The Pipeline : Deeper" Jeffrey gave an example of using the pipeline. with called script-parameters. it looks very cool but I cant get this to work.
    The command is :
    get-ADComputer – filter * |get-wmiobject win32_bios -computername {$}

    I believe the command works as follow: get-adcomputers is pushing everything to the pipeline and the parameter {$} grabs the value and use it on the -computername property.

    Did Jeffrey use a wrong example or do I something wrong?

  • #54748
    Profile photo of Dave Wyatt
    Dave Wyatt

    That script block syntax only works for parameters that accept pipeline input (either by value or by property name, doesn't matter). In this case, Get-WmiObject's -ComputerName parameter doesn't accept pipeline input.

    You can still use ForEach-Object, though:

    get-ADComputer – filter * | ForEach-Object { get-wmiobject win32_bios -computername $ }
  • #54749
    Profile photo of Rob Simmers
    Rob Simmers

    There is a space between the hypen and Filter parameter (- Filter), it should be -Filter. Get-AD* cmdlets return a non-standard PSObject with Microsoft.ActiveDirectory.Management.XXXX. If you do:

    Get-ADComputer –Filter * | Select Name | Get-WMIObject -Class Win32_BIOS -ComputerName {$_.Name}

    It should correct the issue if the space in the parameter wasn't your issue. Select-Object generates a new PSObject with the properties you tell it get, so the $_.Name reference will work as expected. It's just something quirky specifically with the AD cmdlets. Also, note that you are trying to run an AD command for EVERY computer in AD. You can do a small amount of computers or a single computer:

    Get-ADComputer –Filter "Name -eq 'Computer123'" | Select Name | Get-WMIObject -Class Win32_BIOS -ComputerName {$_.Name}


    #Find all computers that start with CMP
    Get-ADComputer –Filter "Name -like 'CMP*'" | Select Name | Get-WMIObject -Class Win32_BIOS -ComputerName {$_.Name}
  • #54759
    Profile photo of Remco_NL

    I'll get it now. Thanks
    Indeed get-wmiobject does not accept pipeline input. (by default). But with those commands you can grab "Name" and put it in get-wmiobject cmdlet.
    I use the GET-ADcomputer in a small environment (Hyper-v lab with 10 vm's) so the output doesn't generate a lot of data.

You must be logged in to reply to this topic.