-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathSizeTransformAttribute.ps1
27 lines (25 loc) · 1.18 KB
/
SizeTransformAttribute.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class SizeTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute {
# Implement the Transform() method
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object] $inputData) {
<#
The parameter value(s) are passed in here as $inputData. We aren't accepting array input
for our function, but it's good to make these fairly versatile where possible, so that
you can reuse them easily!
#>
$outputData = switch ($inputData) {
{ $_ -is [SizeTransformAttribute] } { $_ }
{ $_ -is [int] } { [System.Drawing.Size]::new($_, $_) }
{ $_ -is [string] -and (($_ -split '[,x]').count -eq 2) } {
$sp = $_ -split '[,x]'
[System.Drawing.Size]::new($sp[0], $sp[1])
}
default {
# If we hit something we can't convert, throw an exception
throw [System.Management.Automation.ArgumentTransformationMetadataException]::new(
"Could not convert input '$_' to a valid Size object."
)
}
}
return $OutputData
}
}