如何在函数 "Format-Table" 中 "Add-Member" 以在 Powershell 中获取新的 "MemberType"?

How to "Add-Member" in Function "Format-Table" to get new "MemberType" in Powershell?

我无法从合并的 table 中选择值。 我正在做两个 Oracle 查询。然后我将两个 table 放在一起并计算新列。然后我不能选择计算值。

我怀疑这与 "MemberType" 有关。新计算的列未显示为 "NoteProperty".

也许有人知道好的解决方案?

$tablespace_raw = Invoke-SqlQuery -Query 'SELECT TABLESPACE_NAME, BLOCK_SIZE, CONTENTS from DBA_TABLESPACES' -Stream
Clear-SqlMessage
$tablespace_raw | Format-Table

TABLESPACE_NAME BLOCK_SIZE CONTENTS 
--------------- ---------- -------- 
SYSTEM                8192 PERMANENT
UNDOTBS1              8192 UNDO     
SYSAUX                8192 PERMANENT
TEMP                  8192 TEMPORARY
USERS                 8192 PERMANENT
BMS                   8192 PERMANENT

$tablespace_metrics_raw = Invoke-SqlQuery -Query 'SELECT TABLESPACE_NAME, USED_SPACE, TABLESPACE_SIZE from DBA_TABLESPACE_USAGE_METRICS' -Stream
Clear-SqlMessage
$tablespace_metrics_raw | Format-Table

TABLESPACE_NAME USED_SPACE TABLESPACE_SIZE
--------------- ---------- ---------------
BMS                 538952         1024000
SYSAUX               55464         4194302
SYSTEM               65984         4194302
TEMP                     0          128000
UNDOTBS1              1440         4194302
USERS                    8           64000

$tablespace_raw | InnerJoin $tablespace_metrics_raw -On TABLESPACE_NAME | Format-Table -Property *, @{Name="USED_SPACE_Bytes";Expression={[double](($_.USED_SPACE) * ($_.BLOCK_SIZE))}}, 
                         @{Name="TABLESPACE_SIZE_Bytes";Expression={[double](($_.TABLESPACE_SIZE) * ($_.BLOCK_SIZE))}},
                         @{Name="TABLESPACE_Free_Percent";Expression={[int](($_.USED_SPACE) / ($_.TABLESPACE_SIZE) * 100)}}

TABLESPACE_NAME BLOCK_SIZE CONTENTS  USED_SPACE TABLESPACE_SIZE USED_SPACE_Bytes TABLESPACE_SIZE_Bytes TABLESPACE_Free_Percent
--------------- ---------- --------  ---------- --------------- ---------------- --------------------- -----------------------
SYSTEM                8192 PERMANENT      65984         4194302        540540928           34359721984                       2
UNDOTBS1              8192 UNDO            1440         4194302         11796480           34359721984                       0
SYSAUX                8192 PERMANENT      55464         4194302        454361088           34359721984                       1
TEMP                  8192 TEMPORARY          0          128000                0            1048576000                       0
USERS                 8192 PERMANENT          8           64000            65536             524288000                       0
BMS                   8192 PERMANENT     538952         1024000       4415094784            8388608000                      53

$tablespace_raw | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name            MemberType   Definition                    
----            ----------   ----------                    
Equals          Method       bool Equals(System.Object obj)
GetHashCode     Method       int GetHashCode()             
GetType         Method       type GetType()                
ToString        Method       string ToString()             
BLOCK_SIZE      NoteProperty decimal BLOCK_SIZE=8192       
CONTENTS        NoteProperty string CONTENTS=PERMANENT     
TABLESPACE_NAME NoteProperty string TABLESPACE_NAME=SYSTEM 


for($i=0;$i-le $tablespace_raw.length-1;$i++){
    $tablespace_raw[$i].TABLESPACE_NAME
    $tablespace_raw[$i].CONTENTS
    $tablespace_raw[$i].BLOCK_SIZE
    $tablespace_raw[$i].USED_SPACE_Bytes   -->> NOTHING!
    $i
}

我认为我不理解在 PowerShell 代码中在客户端执行 SQL 连接的动机。也许你需要为此添加一些推理。

但是对其他情况一无所知,让数据库服务器计算这些值不是容易得多吗?

$sql = '
SELECT
    ts.TABLESPACE_NAME,
    ts.BLOCK_SIZE,
    ts.CONTENTS,
    tsm.USED_SPACE,
    tsm.TABLESPACE_SIZE,
    tsm.USED_SPACE * ts.BLOCK_SIZE USED_SPACE_Bytes,
    tsm.TABLESPACE_SIZE * ts.BLOCK_SIZE TABLESPACE_SIZE_Bytes,
    /* casting to INT, otherwise Invoke-SqlQuery is unhappy */
    CAST(ROUND(tsm.USED_SPACE / tsm.TABLESPACE_SIZE * 100) AS INT) TABLESPACE_Free_Percent
FROM
    DBA_TABLESPACES ts
    INNER JOIN DBA_TABLESPACE_USAGE_METRICS tsm ON tsm.TABLESPACE_NAME = ts.TABLESPACE_NAME
'

$tablespace_info = Invoke-SqlQuery -Query $sql -Stream
foreach ($row in $tablespace_info) {
    $row.TABLESPACE_NAME
    $row.CONTENTS
    $row.BLOCK_SIZE
    $row.USED_SPACE_Bytes
}