首页 > 解决方案 > Powershell 对象数组到 Convertto-html

问题描述

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

标签: powershell

解决方案


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

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

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

推荐阅读