forked from iRon7/ConvertTo-Expression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTo-Expression.ps1
295 lines (260 loc) · 14.4 KB
/
ConvertTo-Expression.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<#PSScriptInfo
.VERSION 3.3.9
.GUID 5f167621-6abe-4153-a26c-f643e1716720
.AUTHOR Ronald Bode (iRon)
.DESCRIPTION Stringifys an object to a PowerShell expression (PSON, PowerShell Object Notation).
.COMPANYNAME
.COPYRIGHT
.TAGS PSON PowerShell Object Notation Expression Serialize Stringify
.LICENSE https://github.com/iRon7/ConvertTo-Expression/LICENSE.txt
.PROJECTURI https://github.com/iRon7/ConvertTo-Expression
.ICON https://raw.githubusercontent.com/iRon7/ConvertTo-Expression/master/ConvertTo-Expression.png
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.SYNOPSIS
Serializes an object to a PowerShell expression.
.DESCRIPTION
The ConvertTo-Expression cmdlet converts (serializes) an object to a
PowerShell expression. The object can be stored in a variable, file or
any other common storage for later use or to be ported to another
system.
An expression can be restored to an object using the native
Invoke-Expression cmdlet:
$Object = Invoke-Expression ($Object | ConverTo-Expression)
Or Converting it to a [ScriptBlock] and invoking it with cmdlets
along with Invoke-Command or using the call operator (&):
$Object = &([ScriptBlock]::Create($Object | ConverTo-Expression))
An expression that is stored in a PowerShell (.ps1) file might also
be directly invoked by the PowerShell dot-sourcing technique, e.g.:
$Object | ConvertTo-Expression | Out-File .\Expression.ps1
$Object = . .\Expression.ps1
Warning: Invoking partly trusted input with Invoke-Expression or
[ScriptBlock]::Create() methods could be abused by malicious code
injections.
.INPUTS
Any. Each objects provided through the pipeline will converted to an
expression. To concatinate all piped objects in a single expression,
use the unary comma operator, e.g.: ,$Object | ConvertTo-Expression
.OUTPUTS
String[]. ConvertTo-Expression returns a PowerShell [String] expression
for each input object.
.PARAMETER InputObject
Specifies the objects to convert to a PowerShell expression. Enter a
variable that contains the objects, or type a command or expression
that gets the objects. You can also pipe one or more objects to
ConvertTo-Expression.
.PARAMETER Depth
Specifies how many levels of contained objects are included in the
PowerShell representation. The default value is 9.
.PARAMETER Expand
Specifies till what level the contained objects are expanded over
separate lines and indented according to the -Indentation and
-IndentChar parameters. The default value is equal to the -Depth value.
A negative value will remove redundant spaces and compress the
PowerShell expression to a single line (except for multi-line strings).
Xml documents and multi-line strings are embedded in a "here string"
and aligned to the left.
.PARAMETER Indentation
Specifies how many IndentChars to write for each level in the
hierarchy.
.PARAMETER IndentChar
Specifies which character to use for indenting.
.PARAMETER Strong
By default, the ConvertTo-Expression cmdlet will return a weakly typed
expression which is best for transfing objects between differend
PowerShell systems.
The -Strong parameter will strickly define value types and objects
in a way that they can still be read by same PowerShell system and
PowerShell system with the same configuration (installed modules etc.).
.PARAMETER Explore
In explore mode, all type prefixes are omitted in the output expression
(objects will cast to to hash tables). In case the -Strong parameter is
also supplied, all orginal (.Net) type names are shown.
The -Explore switch is usefull for exploring object hyrachies and data
type, not for saving and transfering objects.
.EXAMPLE
PS> (Get-UICulture).Calendar | ConvertTo-Expression
[pscustomobject]@{
'AlgorithmType' = 1
'CalendarType' = 1
'Eras' = , 1
'IsReadOnly' = $False
'MaxSupportedDateTime' = [datetime]'9999-12-31T23:59:59.9999999'
'MinSupportedDateTime' = [datetime]'0001-01-01T00:00:00.0000000'
'TwoDigitYearMax' = 2029
}
.EXAMPLE
PS> (Get-UICulture).Calendar | ConvertTo-Expression -Strong
[pscustomobject]@{
'AlgorithmType' = [System.Globalization.CalendarAlgorithmType]'SolarCalendar'
'CalendarType' = [System.Globalization.GregorianCalendarTypes]'Localized'
'Eras' = [array][int]1
'IsReadOnly' = [bool]$False
'MaxSupportedDateTime' = [datetime]'9999-12-31T23:59:59.9999999'
'MinSupportedDateTime' = [datetime]'0001-01-01T00:00:00.0000000'
'TwoDigitYearMax' = [int]2029
}
.EXAMPLE
PS> Get-Date | Select-Object -Property * | ConvertTo-Expression | Out-File .\Now.ps1
PS> $Now = .\Now.ps1 # $Now = Get-Content .\Now.Ps1 -Raw | Invoke-Expression
PS> $Now
Date : 1963-10-07 12:00:00 AM
DateTime : Monday, October 7, 1963 10:47:00 PM
Day : 7
DayOfWeek : Monday
DayOfYear : 280
DisplayHint : DateTime
Hour : 22
Kind : Local
Millisecond : 0
Minute : 22
Month : 1
Second : 0
Ticks : 619388596200000000
TimeOfDay : 22:47:00
Year : 1963
.EXAMPLE
PS> @{Account="User01";Domain="Domain01";Admin="True"} | ConvertTo-Expression -Expand -1 # Compress the PowerShell output
@{'Admin'='True';'Account'='User01';'Domain'='Domain01'}
.EXAMPLE
PS> WinInitProcess = Get-Process WinInit | ConvertTo-Expression # Convert the WinInit Process to a PowerShell expression
.EXAMPLE
PS> Get-Host | ConvertTo-Expression -Depth 4 # Reveal complex object hierarchies
.LINK
https://www.powershellgallery.com/packages/ConvertFrom-Expression
#>
using namespace System.Management.Automation
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Scope = 'Function')] # https://github.com/PowerShell/PSScriptAnalyzer/issues/1472
[CmdletBinding()][OutputType([scriptblock])] param(
[Parameter(ValueFromPipeLine = $True)][Alias('InputObject')] $Object,
[int]$Depth = 9,
[int]$Expand = $Depth,
[int]$Indentation = 4,
[string]$IndentChar = ' ',
[switch]$Strong,
[switch]$Explore,
[ValidateSet("Name", "Fullname", "Auto")][string]$TypeNaming = 'Auto',
[string]$NewLine = [System.Environment]::NewLine
)
begin {
$ValidUnqoutedKey = '^[\p{L}\p{Lt}\p{Lm}\p{Lo}_][\p{L}\p{Lt}\p{Lm}\p{Lo}\p{Nd}_]*$'
$ListItem = $Null
$Tab = $IndentChar * $Indentation
function Serialize ($Object, $Iteration, $Indent) {
function Quote ([string]$Item) { "'$($Item.Replace('''', ''''''))'" }
function QuoteKey ([string]$Key) { if ($Key -cmatch $ValidUnqoutedKey) { $Key } else { Quote $Key } }
function Here ([string]$Item) { if ($Item -match '[\r\n]') { "@'$NewLine$Item$NewLine'@$NewLine" } else { Quote $Item } }
function Stringify ($Object, $Cast = $Type, $Convert) {
$Casted = $PSBoundParameters.ContainsKey('Cast')
function GetTypeName($Type) {
if ($Type -is [Type]) {
if ($TypeNaming -eq 'Fullname') { $Typename = $Type.Fullname }
elseif ($TypeNaming -eq 'Name') { $Typename = $Type.Name }
else {
$Typename = "$Type"
if ($Type.Namespace -eq 'System' -or $Type.Namespace -eq 'System.Management.Automation') {
if ($Typename.Contains('.')) { $Typename = $Type.Name }
}
}
if ($Type.GetType().GenericTypeArguments) {
$TypeArgument = ForEach ($TypeArgument in $Type.GetType().GenericTypeArguments) { GetTypeName $TypeArgument }
$Arguments = if ($Expand -ge 0) { $TypeArgument -join ', ' } else { $TypeArgument -join ',' }
$Typename = $Typename.GetType().Split(0x60)[0] + '[' + $Arguments + ']'
}
$Typename
} else { $Type }
}
function Prefix ($Object, [switch]$Parenthesis) {
if ($Convert) { if ($ListItem) { $Object = "($Convert $Object)" } else { $Object = "$Convert $Object" } }
if ($Parenthesis) { $Object = "($Object)" }
if ($Explore) { if ($Strong) { "[$(GetTypeName $Type)]$Object" } else { $Object } }
elseif ($Strong -or $Casted) { if ($Cast) { "[$(GetTypeName $Cast)]$Object" } }
else { $Object }
}
function Iterate ($Object, [switch]$Strong = $Strong, [switch]$ListItem, [switch]$Level) {
if ($Iteration -lt $Depth) { Serialize $Object -Iteration ($Iteration + 1) -Indent ($Indent + 1 - [int][bool]$Level) } else { "'...'" }
}
if ($Object -is [string]) { Prefix $Object } else {
$List, $Properties = $Null; $Methods = $Object.PSObject.Methods
if ($Methods['GetEnumerator'] -is [PSMethod]) {
if ($Methods['get_Keys'] -is [PSMethod] -and $Methods['get_Values'] -is [PSMethod]) {
$List = [Ordered]@{}; foreach ($Key in $Object.get_Keys()) { $List[(QuoteKey $Key)] = Iterate $Object[$Key] }
} else {
$Level = @($Object).Count -eq 1 -or ($Null -eq $Indent -and !$Explore -and !$Strong)
$StrongItem = $Strong -and $Type.Name -eq 'Object[]'
$List = @(foreach ($Item in $Object) {
Iterate $Item -ListItem -Level:$Level -Strong:$StrongItem
})
}
} else {
$Properties = $Object.PSObject.Properties | Where-Object { $_.MemberType -eq 'Property' }
if (!$Properties) { $Properties = $Object.PSObject.Properties | Where-Object { $_.MemberType -eq 'NoteProperty' } }
if ($Properties) { $List = [Ordered]@{}; foreach ($Property in $Properties) { $List[(QuoteKey $Property.Name)] = Iterate $Property.Value } }
}
if ($List -is [array]) {
#if (!$Casted -and ($Type.Name -eq 'Object[]' -or "$Type".Contains('.'))) { $Cast = 'array' }
if (!$List.Count) { Prefix '@()' }
elseif ($List.Count -eq 1) {
if ($Strong) { Prefix "$List" }
elseif ($ListItem) { "(,$List)" }
else { ",$List" }
}
elseif ($Indent -ge $Expand - 1 -or $Type.GetElementType().IsPrimitive) {
$Content = if ($Expand -ge 0) { $List -join ', ' } else { $List -join ',' }
Prefix -Parenthesis:($ListItem -or $Strong) $Content
}
elseif ($Null -eq $Indent -and !$Strong -and !$Convert) { Prefix ($List -join ",$NewLine") }
else {
$LineFeed = $NewLine + ($Tab * $Indent)
$Content = "$LineFeed$Tab" + ($List -join ",$LineFeed$Tab")
if ($Convert) { $Content = "($Content)" }
if ($ListItem -or $Strong) { Prefix -Parenthesis "$Content$LineFeed" } else { Prefix $Content }
}
} elseif ($List -is [System.Collections.Specialized.OrderedDictionary]) {
if (!$Casted) { if ($Properties) { $Casted = $True; $Cast = 'pscustomobject' } else { $Cast = 'hashtable' } }
if (!$List.Count) { Prefix '@{}' }
elseif ($Expand -lt 0) { Prefix ('@{' + (@(foreach ($Key in $List.get_Keys()) { "$Key=" + $List[$Key] }) -join ';') + '}') }
elseif ($List.Count -eq 1 -or $Indent -ge $Expand - 1) {
Prefix ('@{' + (@(foreach ($Key in $List.get_Keys()) { "$Key = " + $List[$Key] }) -join '; ') + '}')
} else {
$LineFeed = $NewLine + ($Tab * $Indent)
Prefix ("@{$LineFeed$Tab" + (@(foreach ($Key in $List.get_Keys()) {
if (($List[$Key])[0] -notmatch '[\S]') { "$Key =" + $List[$Key].TrimEnd() } else { "$Key = " + $List[$Key].TrimEnd() }
}) -join "$LineFeed$Tab") + "$LineFeed}")
}
}
else { Prefix ",$List" }
}
}
if ($Null -eq $Object) { "`$Null" } else {
$Type = $Object.GetType()
if ($Object -is [Boolean]) { if ($Object) { Stringify '$True' } else { Stringify '$False' } }
elseif ('adsi' -as [type] -and $Object -is [adsi]) { Stringify "'$($Object.ADsPath)'" $Type }
elseif ('Char', 'mailaddress', 'Regex', 'Semver', 'Type', 'Version', 'Uri' -contains $Type.Name) { Stringify "'$($Object)'" $Type }
elseif ($Type.IsPrimitive) { Stringify "$Object" }
elseif ($Object -is [string]) { Stringify (Here $Object) }
elseif ($Object -is [securestring]) { Stringify "'$($Object | ConvertFrom-SecureString)'" -Convert 'ConvertTo-SecureString' }
elseif ($Object -is [pscredential]) { Stringify $Object.Username, $Object.Password -Convert 'New-Object PSCredential' }
elseif ($Object -is [datetime]) { Stringify "'$($Object.ToString('o'))'" $Type }
elseif ($Object -is [Enum]) { if ("$Type".Contains('.')) { Stringify "$(0 + $Object)" } else { Stringify "'$Object'" $Type } }
elseif ($Object -is [scriptblock]) { if ($Object -match "\#.*?$") { Stringify "{$Object$NewLine}" } else { Stringify "{$Object}" } }
elseif ($Object -is [RuntimeTypeHandle]) { Stringify "$($Object.Value)" }
elseif ($Object -is [xml]) {
$SW = New-Object System.IO.StringWriter; $XW = New-Object System.Xml.XmlTextWriter $SW
$XW.Formatting = if ($Indent -lt $Expand - 1) { 'Indented' } else { 'None' }
$XW.Indentation = $Indentation; $XW.IndentChar = $IndentChar; $Object.WriteContentTo($XW); Stringify (Here $SW) $Type }
elseif ($Object -is [System.Data.DataTable]) { Stringify $Object.Rows }
elseif ($Type.Name -eq "OrderedDictionary") { Stringify $Object 'ordered' }
elseif ($Object -is [ValueType]) { try { Stringify "'$($Object)'" $Type } catch [NullReferenceException]{ Stringify '$Null' $Type } }
else { Stringify $Object }
}
}
}
process {
(Serialize $Object).TrimEnd()
}