【问题标题】:Powershell Object array to Convertto-htmlPowershell 对象数组到 Convertto-html
【发布时间】: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 个属性:PrincipalIs GroupRoleRole 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


【解决方案1】:

你可以试试$localAccounts | select Principal,Role,"Role Description" | ConvertTo-Html

但如果要更改标题名称,则需要先将项目转换为 PSCustomObjects。

$localAccounts | %{ [PSCustomObject]@{
   UserName=$_.Principal
   Role=$_.Role
   Description=$_.'Role Description'} | ConvertTo-Html

【讨论】:

  • 请查找以下内容,因为除了长度 $localAccounts | 之外没有看到任何属性gm TypeName: System.String 下面可能会给出一些想法 $localAccounts[0] Principal Is Group Role Role Description $localAccounts[1] ---------- -------- --- --- ------------------------------------------------------------ ---------- $localAccounts[2] User1 false Admin 完全访问权限
  • 如果您只有一个字符串数组,那么您尝试做的事情将非常困难并且容易出错。如果您使用如何创建 $localAccounts 变量来更新您的问题,我们或许可以为您提供帮助。
猜你喜欢
  • 2017-02-18
  • 2013-04-18
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
相关资源
最近更新 更多