本文介绍了PSCustomObject到Hashtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PSCustomObject转换为Hashtable的最简单方法是什么?它与splat运算符,大括号以及似乎是键值对的显示一样.当我尝试将其转换为[Hashtable]时不起作用.我还尝试了.toString(),分配的变量说了一个字符串,但是什么也不显示-有什么想法吗?

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?

推荐答案

不要太难.这样的事情应该可以解决问题:

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 = Get-Date }
$theObject = new-object psobject -Property $ht1

# Convert the PSCustomObject back to a hashtable
$ht2 = @{}
$theObject.psobject.properties | Foreach { $ht2[$_.Name] = $_.Value }

这篇关于PSCustomObject到Hashtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:09