Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

Powershell V2 pipleline include value from a function

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

ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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