Solved

Powershell V2 pipleline include value from a function

Posted on 2013-06-13
2
264 Views
Last Modified: 2013-06-13
Hi,

I am trying to work out how to put the return value of a function into a powershell pipeline.

Here is my basic code that does not work below. Any ideas on how to change this to make it work.

Thanks,

Ward

function fnPackage_Type($package_type)
{
	switch ($package_type)
    {
    	0 {"Software Distribution Package"}
		3 {"Driver Package"}
		4 {"Task Sequence Package"}
		5 {"Software Update Package"}
		6 {"Device Setting Package"}
		7 {"Virtual Package"}
		257 {"Image Package"}
		258 {"Boot Image Package"}
		259 {"Operating System Install Package"}
        default {"Unknown"}
    }
}

$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_PackageBaseclass"
$data = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$results = $data | Select-Object Name,PkgSourcePath,PackageType,fnPackage_Type($_.PackageType)
$results

Open in new window

0
Comment
Question by:whorsfall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39243756
function fnPackage_Type($package_type)
{
	switch ($package_type)
    {
    	0 {"Software Distribution Package"}
		3 {"Driver Package"}
		4 {"Task Sequence Package"}
		5 {"Software Update Package"}
		6 {"Device Setting Package"}
		7 {"Virtual Package"}
		257 {"Image Package"}
		258 {"Boot Image Package"}
		259 {"Operating System Install Package"}
        default {"Unknown"}
    }
}

$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_PackageBaseclass"
$data = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$results = $data | Select-Object Name,PkgSourcePath,PackageType,@{Name="PackageTypeDescription";Expression={fnPackage_Type($_.PackageType)}}
$results

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39244106
That is how I would use enhanced features and the pipeline:
function fnPackage_Type(
  [Parameter(ValueFromPipelineByPropertyName = $true)] $PackageType,
  [Parameter(ValueFromPipeline = $true)] $obj
)
{
  $obj | Add-Member NoteProperty "PackageTypeDescription" (
    switch ($PackageType)
      {
        0 {"Software Distribution Package"}
      3 {"Driver Package"}
      4 {"Task Sequence Package"}
      5 {"Software Update Package"}
      6 {"Device Setting Package"}
      7 {"Virtual Package"}
      257 {"Image Package"}
      258 {"Boot Image Package"}
      259 {"Operating System Install Package"}
          default {"Unknown"}
      } 
    )
} 

$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_PackageBaseclass"
Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query |
  fnPackageType | Select-Object Name,PkgSourcePath,PackageType,PackageTypeDescription

Open in new window

0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question