Skip to content

Instantly share code, notes, and snippets.

@LindnerBrewery
Last active January 10, 2024 10:13
Show Gist options
  • Save LindnerBrewery/fbc07b3211285c31bae30ec3e473f870 to your computer and use it in GitHub Desktop.
Save LindnerBrewery/fbc07b3211285c31bae30ec3e473f870 to your computer and use it in GitHub Desktop.
Transformation class example
# transformation class that transforms string to service controller object
class ServiceSingleTransformation : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$EngineIntrinsics, [object] $InputData) {
$item = $InputData
if ($item -is [string]) {
$item = Get-Service -name "$item"
}elseif ($item -is [System.ServiceProcess.ServiceController]) {
$item = $item
}else {
throw "Invalid type"
}
return ($item)
}
}
function Test-TransformationClasses {
#add comment based help
<#
.SYNOPSIS
This function demonstrates how transformation classes work
.DESCRIPTION
This function demonstrates how transformation classes work
.PARAMETER Service
This is the service you want to query
.EXAMPLE
Test-TransformationClasses -Service "w32time"
.EXAMPLE
Test-TransformationClasses -Service (Get-Service -name "w32time")
#>
[CmdletBinding()]
Param (
# Param1 help description
[Parameter()]
[ServiceSingleTransformation()]
[System.ServiceProcess.ServiceController]
$Service
)
begin {}
process {
# returns service as System.ServiceProcess.ServiceController
$Service
# throws an error because transformation class only accepts string or System.ServiceProcess.ServiceController
# every time you assign $service a value the transformation class is called
$Service = $null
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment