Solved

Powershell V2 pipleline include value from a function

Posted on 2013-06-13
2
257 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:
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 68

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now