Script blocks not working

Welcome Forums General PowerShell Q&A Script blocks not working

This topic contains 3 replies, has 3 voices, and was last updated by

2 years, 7 months ago.

  • Author
  • #54743

    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    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

    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    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

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    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

    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    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.

The topic ‘Script blocks not working’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort