header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Question about passing properties into a function
Last Post 18 Mar 2010 03:50 PM by Scott. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
ScottUser is Offline
New Member
New Member
Posts:7
Avatar

--
17 Mar 2010 03:12 PM
    Hi...

    I am having problems figuring out how to code something and thought someone here might be able to give me a helping hand. I have spent hours searching the web and have come to the conclusion that perhaps I don't even  know the right terminology ;-)

    I am trying to write a function that works within the pipeline, takes in an object and a Property list and does some work.

    I have successfully been able to get the function to work quite well with objects, however, I can not for the life of me figure out what type of variable powershell should see the properties as and how to process them inside the pipeline function.

    Here is some code. I have tried to strip out everything so it is down to the bare bones....

    Function Transform-Object{
    [CmdletBinding()]
    Param(
       [Parameter(ValueFromPipeline=$true)]
       [PSObject]$InputObject,
       [PSObject]$Property
    )
    BEGIN
    {
    }
    PROCESS
    {
        $HighlightValue="Handles"

        if ($Property){
            # Foreach value in the propertylist,
            # Export out just those names and in that order
            # highlighting the proper one.
            } # if end
        else{
            "Row"
            ForEach($i in $InputObject | Get-Member -type *Property) {
                # Export all the properties of the Object
                # highlighting the proper one.
                $Name = $i.Name
                $Value = $InputObject.$Name
                if ($Name -eq $HighlightValue) { "Cell $Name Cell, Cell Bold $Value Cell Bold" }
                else { "Cell $Name Cell, Cell $Value Cell" }
            } # foreach end
            "End Row"
        } #else end
    } # process end
    END
    {
    }
    } # function end


    Get-Process | Select-Object -Property Handles, Id, ProcessName | Transform-Object # -Property ProcessName


    Thanks for any and all help!

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    18 Mar 2010 05:19 AM

    Try this:

    function Transform-Object
    {
        [CmdletBinding()]
        param(
           [Parameter(ValueFromPipeline=$true)]
           [PSObject]$InputObject,
           [Object[]]$Property = $null
        )
       
        BEGIN
        {
            $valueToHighlight = "Handles"
        }
        PROCESS
        {
            Write-Host
           
            if ($Property)
            {           
                $properties = $InputObject | Select-Object -Property $Property
               
                for ([int]$i = 0; $i -lt $Property.Length; $i++)
                {
                    $name = $($Property[$i]).ToString()
                    $value = $properties.$name
                   
                    if ($name -eq $valueToHighlight)
                    {
                        Write-Host "$name, " -NoNewLine
                        Write-Host "$value" -BackgroundColor Green -ForeGroundColor White
                    }
                    else
                    {
                        Write-Host "$name, $value"
                    }
                }
            }
            else
            {          
                $properties = $InputObject | Get-Member -MemberType Properties
               
                foreach ($property in $properties)
                {
                    $name = $property.Name
                    $value = $InputObject.$name
                   
                    if ($name -eq $valueToHighlight)
                    {
                        Write-Host "$name, " -NoNewLine
                        Write-Host "$value" -BackgroundColor Green -ForeGroundColor White
                    }
                    else
                    {
                        Write-Host "$name, $value"
                    }
                }
            }
        }
        END
        {
        }
    }

    Get-Process | Select-Object -Property Handles, Id, ProcessName | Transform-Object -Property Handles, ProcessName

    ScottUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    18 Mar 2010 03:50 PM
    GW,
    Thank you so very much for helping! This is EXACTLY what I was looking for ;-)
    You are wonderful!
    Scott
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer