Windows Vista Forums

Pipeline question
  1. #1


    RobertZ Guest

    Pipeline question

    I've scoured the net and haven't been able to figure out how to do this or if
    it's even possible.

    What I'm trying to do is create some modular scripts I can pipeline to each
    other. For example:
    GetADObj.ps1 will perform an AD query.
    GetEvents.ps1 will get and filter the event log configs from a list of AD
    objects piped in.
    ExportXLS.ps1 will export a list of fields to an excell document.

    So I can run the scripts like so GetADObj | GetEvents | ExportXLS (I know
    the syntax here is not quite right. The reall syntax will have paths and
    paramters.)

    With some help (thanks Brandon) I have the GetADObj script passing a list of
    objects to the pipeline and the GetEvents scrip recieves the pipeline and is
    able to access the event log configs on AD machine objects pipeline. I think
    I've managed replaced the incoming AD objects with Event Viewer Config
    objects, I haven't figured out how to include the hostname from the AD object
    with the outgoing objects.

    Is it possible to add information to the pipeline like the hostname the
    event log configs came from?

    Here is the bare bones GetEvents .ps1 I have so far...

    process
    {
    # get the name of the host from the incoming AD object pipeline
    $strHostname = [string]$_.properties.name

    # this portion dispalys to the screen the information I'd like to pass to
    the pipeline
    write-host $strHostname -ForegroundColor Green
    $a = [System.Diagnostics.Eventlog]::GetEventLogs($strHostname)
    $a



    # Send the eventlog info and the hostname out to the pipeline
    $_ = $a,$strHostname
    }

      My System SpecsSystem Spec

  2. #2
    sapienscripter's Avatar

    Scripting Guru
    Join Date : Jun 2008
    Syracuse, NY
    Posts : 59
    Vista Ultimate 32bit
    Local Time: 01:12 AM
    usa us new york

     

    Re: Pipeline question

    If you are managing Active Directory, is there any reason you can't use the free Quest AD cmdlets? It will make your life, at least that part of it, much easier.

      My System SpecsSystem Spec

  3. #3
    sapienscripter's Avatar

    Scripting Guru
    Join Date : Jun 2008
    Syracuse, NY
    Posts : 59
    Vista Ultimate 32bit
    Local Time: 01:12 AM
    usa us new york

     

    Re: Pipeline question

    You can create custom objects or use Add-Member to add properties to existing objects. Another possibility is to use Select-Object and a hashtable for a new property.

      My System SpecsSystem Spec

  4. #4


    Brandon Shell [MVP] Guest

    Re: Pipeline question

    To be honest... I think your best bet is using custom objects. I have a couple
    of blog post here http://bsonposh.com/page/2?s=Custom+Object specifically
    http://bsonposh.com/archives/221

    But to answer your question... You can use Add-Member to add a property.

    $object | add-member -membertype noteproperty -name HostNam -value $name


    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    R> I've scoured the net and haven't been able to figure out how to do
    R> this or if it's even possible.
    R>
    R> What I'm trying to do is create some modular scripts I can pipeline
    R> to each
    R> other. For example:
    R> GetADObj.ps1 will perform an AD query.
    R> GetEvents.ps1 will get and filter the event log configs from a list
    R> of AD
    R> objects piped in.
    R> ExportXLS.ps1 will export a list of fields to an excell document.
    R> So I can run the scripts like so GetADObj | GetEvents | ExportXLS (I
    R> know the syntax here is not quite right. The reall syntax will have
    R> paths and paramters.)
    R>
    R> With some help (thanks Brandon) I have the GetADObj script passing a
    R> list of objects to the pipeline and the GetEvents scrip recieves the
    R> pipeline and is able to access the event log configs on AD machine
    R> objects pipeline. I think I've managed replaced the incoming AD
    R> objects with Event Viewer Config objects, I haven't figured out how
    R> to include the hostname from the AD object with the outgoing objects.
    R>
    R> Is it possible to add information to the pipeline like the hostname
    R> the event log configs came from?
    R>
    R> Here is the bare bones GetEvents .ps1 I have so far...
    R>
    R> process
    R> {
    R> # get the name of the host from the incoming AD object pipeline
    R> $strHostname = [string]$_.properties.name
    R> # this portion dispalys to the screen the information I'd like to
    R> pass to
    R> the pipeline
    R> write-host $strHostname -ForegroundColor Green
    R> $a = [System.Diagnostics.Eventlog]::GetEventLogs($strHostname)
    R> $a
    R> # Send the eventlog info and the hostname out to the pipeline
    R> $_ = $a,$strHostname
    R> }



      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: Pipeline question

    Output the object after assign it the values

    .
    .
    .
    $_ = $a,$strHostname
    $_
    # if the ExportXLS takes an array use the comma operator
    ,$_
    }

    -
    Kiron

      My System SpecsSystem Spec

  6. #6


    RobertZ Guest

    RE: Pipeline question

    Addendum: I see that the Event Viewer objects (System.Diaganositcs.Eventlog)
    has a hostname propererty I can pull the hostname from, but I'm still curious
    about compounding multiple streams of information into the pipeline.

    Thanks in advance if anyone know how/if this is possible.

    ~RobertZ

      My System SpecsSystem Spec

  7. #7
    sapienscripter's Avatar

    Scripting Guru
    Join Date : Jun 2008
    Syracuse, NY
    Posts : 59
    Vista Ultimate 32bit
    Local Time: 01:12 AM
    usa us new york

     

    Re: Pipeline question

    See if this example helps:

    #Try-Me.ps1
    $ps=Get-Process | Measure-Object
    $running=Get-Service | where {$_.status -eq "running"} | Measure-Object
    $stopped=Get-Service | where {$_.status -eq "stopped"} | Measure-Object

    $obj=New-Object PSobject

    $obj | Add-Member -MemberType "NoteProperty" -name "ProcCount" -value $ps.Count
    $obj | Add-Member -MemberType "NoteProperty" -name "SvcRunning" -value $running.Count
    $obj | Add-Member -MemberType "NoteProperty" -name "SvcStopped" -value $stopped.Count


    write $obj

    If you put this in a script and run it, a custom object is written to the pipeline with properties ProcCount,SvcRunning and SvcStopped. This was created from data from three other PowerShell expressions. I think you are trying to do something similar.

      My System SpecsSystem Spec

  8. #8


    Kirk Munro [MVP] Guest

    Re: Pipeline question

    Hi Robert,

    In your GetEvents.ps1 file, your statement to send the object to the
    pipeline is wrong. You don't want to assign your objects to $_. You just
    want to output them to the next stage in the pipeline. Change this:

    $_ = $a,$strHostname

    to this:

    $a
    $strHostname

    or this:

    $a | Out-Default
    $strHostname | Out-Default

    Your scripts control the pipeline completely. They can take objects in
    (which will be referenced in the $_ variable) and pass them along by
    outputting the $_ variable or pass along completely different objects by
    outputting something else you have created.

    Also, if you want to output data to the console but not pass it along the
    pipeline, pipe the data you want just in the console to Write-Host.

    And personally, I recommend adding data to the objects using Add-Member
    rather than creating custom objects. I do this *a lot* and it works very
    well. Not to say that the other doesn't work as well, but I like retaining
    the original object type and class as I extend it with additional
    information.

    Lastly, you might not even need your ExportXLS script. Just use Export-Csv
    and then when the script is done use the invoke operator on the csv file
    path to open it in Excel.

    Please let me know if this helps or if you need more assistance.

    --
    Kirk Munro [MVP]
    Poshoholic
    http://poshoholic.com


    "RobertZ" <RobertZ@xxxxxx> a écrit dans le message de
    groupe de discussion : DE85A663-3BBE-403A-8764-1FCEDCE3EBF4@xxxxxx...

    > I've scoured the net and haven't been able to figure out how to do this or
    > if
    > it's even possible.
    >
    > What I'm trying to do is create some modular scripts I can pipeline to
    > each
    > other. For example:
    > GetADObj.ps1 will perform an AD query.
    > GetEvents.ps1 will get and filter the event log configs from a list of AD
    > objects piped in.
    > ExportXLS.ps1 will export a list of fields to an excell document.
    >
    > So I can run the scripts like so GetADObj | GetEvents | ExportXLS (I know
    > the syntax here is not quite right. The reall syntax will have paths and
    > paramters.)
    >
    > With some help (thanks Brandon) I have the GetADObj script passing a list
    > of
    > objects to the pipeline and the GetEvents scrip recieves the pipeline and
    > is
    > able to access the event log configs on AD machine objects pipeline. I
    > think
    > I've managed replaced the incoming AD objects with Event Viewer Config
    > objects, I haven't figured out how to include the hostname from the AD
    > object
    > with the outgoing objects.
    >
    > Is it possible to add information to the pipeline like the hostname the
    > event log configs came from?
    >
    > Here is the bare bones GetEvents .ps1 I have so far...
    >
    > process
    > {
    > # get the name of the host from the incoming AD object pipeline
    > $strHostname = [string]$_.properties.name
    >
    > # this portion dispalys to the screen the information I'd like to pass to
    > the pipeline
    > write-host $strHostname -ForegroundColor Green
    > $a = [System.Diagnostics.Eventlog]::GetEventLogs($strHostname)
    > $a
    >
    > # Send the eventlog info and the hostname out to the pipeline
    > $_ = $a,$strHostname
    > }

      My System SpecsSystem Spec

  9. #9


    RobertZ Guest

    Re: Pipeline question

    Thanks everyone. This has helped me a lot.

    "Kirk Munro [MVP]" wrote:

    > Hi Robert,
    >
    > In your GetEvents.ps1 file, your statement to send the object to the
    > pipeline is wrong. You don't want to assign your objects to $_. You just
    > want to output them to the next stage in the pipeline. Change this:
    >
    > $_ = $a,$strHostname
    >
    > to this:
    >
    > $a
    > $strHostname
    >
    > or this:
    >
    > $a | Out-Default
    > $strHostname | Out-Default
    >
    > Your scripts control the pipeline completely. They can take objects in
    > (which will be referenced in the $_ variable) and pass them along by
    > outputting the $_ variable or pass along completely different objects by
    > outputting something else you have created.
    >
    > Also, if you want to output data to the console but not pass it along the
    > pipeline, pipe the data you want just in the console to Write-Host.
    >
    > And personally, I recommend adding data to the objects using Add-Member
    > rather than creating custom objects. I do this *a lot* and it works very
    > well. Not to say that the other doesn't work as well, but I like retaining
    > the original object type and class as I extend it with additional
    > information.
    >
    > Lastly, you might not even need your ExportXLS script. Just use Export-Csv
    > and then when the script is done use the invoke operator on the csv file
    > path to open it in Excel.
    >
    > Please let me know if this helps or if you need more assistance.
    >
    > --
    > Kirk Munro [MVP]
    > Poshoholic
    > http://poshoholic.com
    >
    >
    > "RobertZ" <RobertZ@xxxxxx> a écrit dans le message de
    > groupe de discussion : DE85A663-3BBE-403A-8764-1FCEDCE3EBF4@xxxxxx...

    > > I've scoured the net and haven't been able to figure out how to do this or
    > > if
    > > it's even possible.
    > >
    > > What I'm trying to do is create some modular scripts I can pipeline to
    > > each
    > > other. For example:
    > > GetADObj.ps1 will perform an AD query.
    > > GetEvents.ps1 will get and filter the event log configs from a list of AD
    > > objects piped in.
    > > ExportXLS.ps1 will export a list of fields to an excell document.
    > >
    > > So I can run the scripts like so GetADObj | GetEvents | ExportXLS (I know
    > > the syntax here is not quite right. The reall syntax will have paths and
    > > paramters.)
    > >
    > > With some help (thanks Brandon) I have the GetADObj script passing a list
    > > of
    > > objects to the pipeline and the GetEvents scrip recieves the pipeline and
    > > is
    > > able to access the event log configs on AD machine objects pipeline. I
    > > think
    > > I've managed replaced the incoming AD objects with Event Viewer Config
    > > objects, I haven't figured out how to include the hostname from the AD
    > > object
    > > with the outgoing objects.
    > >
    > > Is it possible to add information to the pipeline like the hostname the
    > > event log configs came from?
    > >
    > > Here is the bare bones GetEvents .ps1 I have so far...
    > >
    > > process
    > > {
    > > # get the name of the host from the incoming AD object pipeline
    > > $strHostname = [string]$_.properties.name
    > >
    > > # this portion dispalys to the screen the information I'd like to pass to
    > > the pipeline
    > > write-host $strHostname -ForegroundColor Green
    > > $a = [System.Diagnostics.Eventlog]::GetEventLogs($strHostname)
    > > $a
    > >
    > > # Send the eventlog info and the hostname out to the pipeline
    > > $_ = $a,$strHostname
    > > }
    >

      My System SpecsSystem Spec

  10. #10


    RobertZ Guest

    Re: Pipeline question

    Mostly I'm creating these scripts as an exercise to learn PowerShell. I'll
    check out Quest since they sound useful just from the name. Thanks

    "sapienscripter" wrote:

    >
    > If you are managing Active Directory, is there any reason you can't use
    > the free Quest AD cmdlets? It will make your life, at least that part
    > of it, much easier.
    >
    >
    > --
    > sapienscripter
    >
    > Coming Soon: 'Managing Active Directory with Windows PowerShell: TFM'
    > (http://www.sapienpress.com/ad.asp)
    >
    > Windows PowerShell MVP
    >
    > 'My Blog' (http://blog.sapien.com/)
    > 'FollowMe on Twitter' (http://www.twitter.com/JeffHicks)
    >

      My System SpecsSystem Spec

Pipeline question problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipeline question stej PowerShell 4 17 Jan 2010
Hosting Powershell in app -- Question about Pipeline thoward37 PowerShell 2 10 Jan 2008
Using the $_ pipeline with WMI Larry R PowerShell 2 27 Apr 2007
Question: Can you write to the pipeline w/ out writing to the console? Brandon Shell PowerShell 4 01 Feb 2007
Pipeline Syntax Chris Warwick PowerShell 2 20 Jun 2006