Powershell展开pscustomobject内的结果数组

时间:2017-11-23 22:39:19

标签: powershell

在多个阵列中抓取DC的一堆信息。

直截了当,但在使固定磁盘对象以所需格式返回时遇到问题:

$alldomains = @("domain1.com","domain2.com","domain3.com","domain4.com")
$password = Read-Host "Enter $domain password" -AsSecureString    

Foreach ($domain in $alldomains) {

    $creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "$domain\$env:USERNAME", $password
    $dclist = (Get-ADForest $domain -Credential $creds).Domains | %{(Get-ADDomainController -Filter * -Server $_ ).hostname}
        Foreach ($DC in $dclist) {
            $cpu = gwmi win32_processor -ComputerName $DC -Credential $creds
            $disk = @(gwmi win32_logicaldisk -ComputerName $DC -Credential $creds | ? {$_.drivetype -eq '3'} | select @{N='Drive Letter';E={$_.caption}},description,drivetype,volumename,@{N='Size (GB)';E={[math]::Round(($_.size/1GB),2)}})

            New-Object PSCustomObject -Property @{
                'Domain'=$domain;
                'Hostname'=$DC;
                'CPU Model'=$cpu.name
                'CPU Cores'=$cpu.NumberOfCores
                'CPU Logical Processors'=$cpu.NumberOfLogicalProcessors
                'Memory (GB)'=$memory.Capacity/1GB
                'Fixed Disks'=$($disk)
            }     ormat:
        }
} 

当我运行脚本时,我得到以下输出:

.\tsyDCs.ps1 | ft

CPU Logical Processors Domain                          Hostname                                        CPU Cores CPU Model                                 Memory (GB) Fixed Disks                                                                               
---------------------- ------                          --------                                        --------- ---------                                 ----------- -----------                                                                               
                     4 Domain1.com DC1.Domain1.com         4 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=4...
                     4 Domain1.com DC2.Domain1.com         4 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=4...
                     4 Domain1.com DC3.Domain1.com         4 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=4...
                     4 Domain1.com DC4.Domain1.com         4 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=4...
                    12 Domain2.com DC1.Domain2.com         6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=1...
                    12 Domain2.com DC2.Domain2.com         6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=1...
                    12 Domain2.com DC3.Domain2.com         6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=System; Size ...
                    12 Domain2.com DC4.Domain2.com         6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=1...
                     2 Domain3.com  DC1.Domain3.com          2 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=9...
                     2 Domain3.com  DC2.Domain3.com          2 Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz          16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=; Size (GB)=1...
                    12 Domain4.com  DC1.Domain4.com          6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=System; Size ...
                    12 Domain4.com  DC2.Domain4.com          6 Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz           16 {@{Drive Letter=C:; description=Local Fixed Disk; drivetype=3; volumename=System; Size ...

如何使用pscustomobject扩展这些结果,有没有办法嵌入表格'里面的FT结果?

1 个答案:

答案 0 :(得分:0)

这样做了:

IF OBJECT_ID('TEMPDB.DBO.#TEMP_DEL', 'U') IS NOT NULL
    DROP TABLE #TEMP_DEL
DECLARE @CYMO VARCHAR(8) = (SELECT '01' + RIGHT(REPLACE(CONVERT(CHAR(10), DATEADD(YY, -7, GETDATE()), 103), '/', ''), 6))
SELECT [NAME] TABLE_NAME INTO #TEMP_DEL FROM DBNAME.SYS.TABLES
WHERE [NAME] LIKE 'ABC%' AND '01'+RIGHT(NAME,6) < @CYMO
WHILE ((SELECT COUNT(1) FROM #TEMP_DEL) > 0)
BEGIN
    DECLARE @TAB VARCHAR(100) = (SELECT TOP 1 TABLE_NAME FROM #TEMP_DEL)
    DECLARE @CMD NVARCHAR(200) = 'DROP TABLE '+@TAB
    EXEC SP_EXECUTESQL @CMD
    DELETE #TEMP_DEL WHERE TABLE_NAME = @TAB
END