-
Notifications
You must be signed in to change notification settings - Fork 3
/
convertto-xxx.ps1
92 lines (82 loc) · 2.42 KB
/
convertto-xxx.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#requires -version 2.0
param(
[Parameter(ParameterSetName="xxx", Mandatory=$true, Position=0)]
$InputObject,
[Parameter(ParameterSetName="xxx", Mandatory=$true)]
$yProperty,
[Parameter(ParameterSetName="xxx", Mandatory=$true)]
$xProperty,
[Parameter(ParameterSetName="xxx")]
[ScriptBlock]
$CellValue = {"TRUE"},
[Parameter(ParameterSetName="xxx")]
$NullValue = "-",
[Parameter(ParameterSetName="TEST", Mandatory=$true)]
[Switch]
$Test
)
function Assert
{
param(
[Parameter(Mandatory=$true, Position=0)]
[ScriptBlock]$script,
[Parameter(Position=1)]
$Message=$null
)
if (!(&$script))
{
$ThrowAway,$stack = Get-PscallStack
$msg = @"
ASSERT( $script ) FAILED at
$($stack | out-string)
$Message
Continue script execution?
"@
if ( !($pscmdlet.ShouldContinue($msg, "ASSERT FAILED")))
{
exit
}
}
}
function Add-Property
{
param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
$obj,
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[String]
$prop,
[Parameter(Mandatory=$true, Position=2)]
$value)
Add-Member -InputObject $obj -MemberType NoteProperty -Name $prop -Value $value -Force
}
if ($PSCmdlet.ParameterSetName -eq "TEST")
{
foreach ($snapin in Get-PSSnapin) {
Write-output $snapin.Name
$InputObject = get-command -PSSnapin $snapin -type Cmdlet |sort noun
&$MyInvocation.MyCommand.Path $InputObject -nullValue "*" -YProperty Verb -xProperty Noun -CellValue {$args[0].name}|ft -auto |out-string –stream
}
exit
}
foreach ($group in $InputObject | Group-Object -Property $yProperty |Sort Name)
{
$outObj = New-Object psobject
Add-Property $outObj Name $group.Name
foreach ($inObj in $InputObject)
{
$Xname = $inObj.$xProperty
Assert {$Xname } "Property [$XProperty] not found on input object"
Assert {$Xname -ne "NAME" } "Cannot have 'Name' as a property"
if ($InObj.$yProperty -eq $group.Name)
{
Add-Property $outObj $Xname (&$CellValue $inObj)
}elseif (!$outObj.$XName)
{
Add-Property $outObj $Xname $nullValue
}
}
Write-Output $outObj
}