PSCustomObject 到哈希表 [英] PSCustomObject to Hashtable

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

问题描述

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 到哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆