【问题标题】:Powershell Expand Array of results inside pscustomobjectPowershell 扩展 pscustomobject 中的结果数组
【发布时间】:2017-11-23 22:39:18
【问题描述】:

跨多个阵列获取 DC 的大量信息。

足够简单,但在让 Fixed Disks 对象以所需格式返回时遇到问题:

$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 结果?

【问题讨论】:

  • 什么是“所需格式”?在你的帖子中放一些你想要的例子,它可以帮助我们弄清楚如何做到这一点,或者尝试手动做到这一点会告诉你没有简单的答案。 “有没有办法在 | FT 结果中嵌入‘表格’?” - 是的。您已经完成了,您的 Fixed Disks 属性是一个对象数组,您的输出是嵌套“行”在默认格式下的样子。
  • 期望的结果是显示所有信息的 Fixed Disks 列,而不是当前显示的 truncatd 输出。
  • 大概是因为没有足够的空间显示所有信息而被截断。如果您不希望数据丢失并且不希望它被截断,那么您需要说出您确实想要看到的内容。您是否需要它们仍然是 PSCustomObjects 或者它们可以成为字符串 - 或单个字符串?你想为每个磁盘多行吗?更窄的其他列?更宽的控制台屏幕?里面有标题的表?您是否尝试过 ft -AutoSizeft -Wrap 或两者兼而有之?如果服务器有 5 或 20 个磁盘怎么办?

标签: powershell


【解决方案1】:

做到了:

$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}
        $cpu = gwmi win32_processor -ComputerName $DC -Credential $creds
        $disklist = @(gwmi win32_logicaldisk -ComputerName $DC -Credential $creds | ? {$_.drivetype -eq '3'} | select @{N='DriveLetter';E={$_.caption}},description,drivetype,volumename,@{N='Size (GB)';E={[math]::Round(($_.size/1GB),2)}})
            Foreach ($disk in $disklist) {
                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
                    'Drive Letter' = $disk.'DriveLetter'
                    'Volume Name' = $disk.Volumename
                    'Size (GB)' = $disk.'Size (GB)'
            }
        }     
    }
} 

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2021-07-04
    • 2020-08-25
    • 2012-04-10
    相关资源
    最近更新 更多