pscustomobject

PowerShell type accelerators: PSObject vs PSCustomObject

北慕城南 提交于 2019-11-28 13:20:02
In PowerShell v3.0 PSCustomObject was introduced. It's like PSObject , but better. Among other improvements (e.g. property order being preserved), creating object from hashtable is simplified: [PSCustomObject]@{one=1; two=2;} Now it seems obvious that this statement: [System.Management.Automation.PSCustomObject]@{one=1; two=2;} would work the same way, because PSCustomObject is an "alias" for full namespace + class name. Instead I get an error: Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Management.Automation.PSCustomObject". I

PowerShell type accelerators: PSObject vs PSCustomObject

為{幸葍}努か 提交于 2019-11-27 07:37:13
问题 In PowerShell v3.0 PSCustomObject was introduced. It's like PSObject , but better. Among other improvements (e.g. property order being preserved), creating object from hashtable is simplified: [PSCustomObject]@{one=1; two=2;} Now it seems obvious that this statement: [System.Management.Automation.PSCustomObject]@{one=1; two=2;} would work the same way, because PSCustomObject is an "alias" for full namespace + class name. Instead I get an error: Cannot convert the "System.Collections.Hashtable

PSCustomObject to Hashtable

只愿长相守 提交于 2019-11-26 05:58:35
问题 What is the easiest way to convert a PSCustomObject to a Hashtable ? It displays just like one with the splat operator, curly braces and what appear to be key value pairs. When I try to cast it to [Hashtable] it doesn\'t work. I also tried .toString() and the assigned variable says its a string but displays nothing - any ideas? 回答1: Shouldn't be too hard. Something like this should do the trick: # Create a PSCustomObject (ironically using a hashtable) $ht1 = @{ A = 'a'; B = 'b'; DateTime =