Solved

Powershell V2 pipleline include value from a function

Posted on 2013-06-13
2
260 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 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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