Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell V2 pipleline include value from a function

Posted on 2013-06-13
2
Medium Priority
?
270 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
2 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 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 71

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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
Loops Section Overview

916 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