【发布时间】:2021-04-29 13:46:13
【问题描述】:
Powershell 新手,在格式化方面需要帮助,如下所示。由于变量 Object 数组没有任何属性可供选择,因此不确定如何获取键值并转换为 html 作为表。
$localAccounts
Principal Is Group Role Role Description
---------- -------- ------ ---------------------------------------------------------
User1 false Admin Full access rights
User2 false Admin Full access rights
User3 false Admin Full access rights
User4 false Custom User-defined roles or roles on non-root inventory objects
User5 false Custom User-defined roles or roles on non-root inventory objects
$localAccounts.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
I need an output as html as table
UserName Role Description
User1 Admin Full access rights
User2 Admin Full access rights
User3 Admin Full access rights
User4 Custom User-defined roles or roles on non-root inventory objects
User5 Custom User-defined roles or roles on non-root inventory objects
【问题讨论】:
-
“既然变量对象数组没有任何属性可供选择”是什么意思?我在数组中的对象上看到 4 个属性:
Principal、Is Group、Role和Role Description...您所要做的就是以 HTML 表格格式输出,并使用ConvertTo-Html。 -
请查找以下内容,因为除了长度 $localAccounts | 之外没有看到任何属性gm TypeName: System.String Name MemberType Definition ---- ---------- ---------- $localAccounts[0] Principal Is Group Role Role Description
-
下面可能会给出一些想法 $localAccounts[0] Principal Is Group Role Role Description $localAccounts[1] ---------- -------- - ----- --------------------------------------------- ------------ $localAccounts[2] User1 false Admin 完全访问权限 $localAccounts[3] root false Admin 完全访问权限
-
请将此信息作为格式化文本放在您的问题中,而不是作为评论,因为这变得不可读(吞下换行符等)另外,` $localAccounts | Format-Table -AutoSize` 在控制台中显示?如果你做`$localAccounts | 你会得到什么?转换为 Html` ?你是怎么得到这个数组
$localAccounts的? -
对我来说,你看起来像 _HAD_ 一个具有这些属性的对象数组,在其上使用
Format-Table并将其捕获为变量 $localAccounts 中的字符串数组。在这种情况下,保持对象数组不变并通过管道传递给ConvertTo-Html
标签: powershell