由于使用PowerShell表达模式,PowerShell提供了一些不错的方法来反序列化对象,例如:
Invoke-Expression小命令Invoke-Command小命令&我的一般期望是,给定表达式的结果应与在该表达式的相同序列化版本上使用上面列出的反序列化命令之一相同(有关背景,请参见“在PowerShell中保存哈希表”上的ConvertTo-Expression答案对象符号(PSON)问题)。
换一种说法:
<Expression> <=> Invoke-Command {<Expression>} <=> &([ScriptBlock]::Create('<Expression>'))
<Expression> <=> Invoke-Expression '<Expression>'
Run Code Online (Sandbox Code Playgroud)
例子:
Get-ChildItem <=> &{Get-ChildItem}
Get-ChildItem <=> Invoke-Command {Get-ChildItem}
Get-ChildItem <=> Invoke-Expression 'Get-ChildItem'
1, 2, 3 <=> &{1, 2, 3}
1, 2, 3 <=> Invoke-Command {1, 2, 3}
1, 2, 3 <=> Invoke-Expression '1, 2, 3'
Run Code Online (Sandbox Code Playgroud)
对于几乎每个表达式而言,确实确实存在这种情况,但是由于PowerShell默认情况下会展开(枚举)输出,因此在表达式包含具有单个项目的数组的情况下,此定义会有所不同:
,1 <?> Invoke-Command {,1}
,1 <?> Invoke-Expression ',1'
,"Test" <?> Invoke-Command {,"Test"}
,"Test" <?> Invoke-Expression …Run Code Online (Sandbox Code Playgroud)