2014-09-02 44 views
0
#Function to get the computerlist: Name,OS,IPv4, IPv6,DiskInfo 
function Get-ComputerListnDiskInfo{ 
[CmdletBinding()] 
    param(
    [Parameter(ValueFromPipeline=$True)] [string[]]$ComputerName 
) 
    BEGIN { 
    Import-Module ActiveDirectory -Cmdlet Get-ADComputer -ErrorAction SilentlyContinue 
    } 
    PROCESS { 

    try{ 
     $computerinfo = Get-ADComputer -Filter * -Properties OperatingSystem 
     #Information about Name,Ipv4,IPv6,Device,VolumeName,Free,Busy,Size,Pfree,Pbusy for ALL COMPUTERS container 
     $AllComputerInfo = @() 
     foreach ($comp in $computerinfo){ 
      #Testing if computers is ON LINE 
      $TestCon = Tester $comp.name 
      $test = $TestCon.BooleanV 
      if($test) { 
       #write-output "$Test" 
       $PhysicalDisks = Get-WMIObject -computername $comp.name -query "SELECT * from win32_logicaldisk where DriveType = 3" | Select Deviceid,VolumeName,FreeSpace,Size 
       $Target = @() 
       #Create the Object foreach disk and append in the Target Variable 
       $GetOPNHealthStatus = Get-PhysicalDisk | select FriendlyName,OperationalStatus,HealthStatus 

       Write-Output "$PhysicalDisk.count" 

       #write-output $GetOPNHealthStatus.OperationalStatus 
       $i=0 
       foreach ($disk in $physicalDisks){ 

        #Get all Items: size,free,busy,pfree and pbusy disk space info (can add a number at the end to set decimals) 
        $Size=FormatNSetSizeFreeSpace $disk.Size 
        $Free=FormatNSetSizeFreeSpace $disk.FreeSpace 
        $Busy=FormatNSetBusySpace $disk.Size $disk.FreeSpace 
        $Pfree=PercentFreeBusy $Free $size 
        $PBusy=PercentFreeBusy $Busy $size 

        #Create a new Object using all the info 
        $result =New-Object PSObject -Property @{ 
         Device=$disk.DeviceID 
         VolumeName=$disk.VolumeName 
         Size=$Size 
         Free=$Free 
         Busy=$Busy 
         Pfree = $PFree 
         PBusy = $PBusy 
         OPStatus = $GetOPNHealthStatus.OperationalStatus[$i] 
         HStatus = $GetOPNHealthStatus.HealthStatus[$i] 
        } 

        $i++ 
        #add this info to the target array 
        $Target+= $result 
       } 


        #Add all info into new object 
        $allIComnDiskInfo=New-Object PSObject -Property @{ 
         Name = $comp.Name 
         OS = $comp.OperatingSystem 
         IPV4 = $TestCon.IPv4 
         IPV6 = $TestCon.IPv6 
         disksInfo = $Target 
        } 
       #and Fill any just add this info to the $Allcomputer info (just online computer's) 
       $AllComputerInfo+= $allIComnDiskInfo 
      } 
     } 
     return $AllComputerInfo 
    } 
    Catch{ 
     Write-Warning $_.Exception.Message 
    } 
    } 
} 

$test = Get-ComputerListnDiskInfo 

运行$测试自定义对象以CSV PowerShell的

$test = Get-ComputerListnDiskInfo 

$测试

disksInfo : {@{PBusy=8,148; VolumeName=; Busy=10,306; Pfree=91,853; Free=116,178; Device=C:; Size=126,483; OPStatus=O; HStatus=H}} 
Name  : DC2012 
OS  : Windows Server 2012 R2 Standard 
IPV4  : 192.168.1.251 
IPV6  : fe80::cd63:76bf:3d2b:340f%12 

运行

$test | Export-Csv here.csv 

我得到这个:

#TYPE System.String 
"Length" 
"6" 

为什么会发生这种情况?

为什么我没有得到所有这些信息?

我应该如何查找包含在“DISKINFO”变量信息

我想这个$测试变量传递到另一个功能格式化它,它似乎并没有工作:

谢谢你提前答案

回答

1

首先,您不只是输出自定义对象或自定义对象数组。但这不是我看到的第一个问题。我看到的第一个问题是,你必须有一个参数这个大功能,然后你这样做:

$test = Get-ComputerListnDiskInfo 

所以你调用该函数不带任何参数,所以它没有计算机来运行它对抗。该函数的某些部分可能默认为本地计算机,但它们都可以吗?也许,我不知道。

那么$test实际上包含什么?数组。什么?那么,函数输出的第一个东西是一个字符串:

Write-Output "$PhysicalDisk.count" 

所以你的数组中的第一项是一个字符串。然后你建立了一堆自定义对象和数组,而不是那些,以及那些。太好了,$test阵列中的下一个项目是一个自定义对象。但是$ test不是一个自定义对象或一个自定义对象的数组,它是一个包含各种事物的数组。

这就是为什么Export-CSV不起作用。

+0

嗯,如果你刚刚说的是正确的 那么,为什么当我运行$测试有正确的信息。 据我所知,当你运行它没有参数他们将默认。我会将$ computername更正为$ computerInfo。我没有参数运行它,因为她会得到过程中的信息,因此不需要该参数。 $测试包含此: disksInfo:{@ {PBusy = 8,148;卷名=;忙= 10306; Pfree = 91853;免费= 116178;设备= C :;尺寸= 126483; OPStatus = O; HStatus = H}} 名称:DC2012 操作系统:Windows Server 2012 R2标准版 IPV4:192.168.1。251 IPV6:... – 2014-09-02 21:30:23

+0

它仍然有正确的信息,它只是不存储在你可以很容易地访问它的方式,如你所愿。我有一种感觉,'$ test [1]'可能有你想要的,并且可以像你想要的那样通过管道输出到csv。 – TheMadTechnician 2014-09-02 21:34:07

+0

你是对的@TheMadTechnician,$ test [1] |导出-csv here.csv工作... 现在我得到这个:“disksInfo”,“Name”,“OS”,“IPV4”,“IPV6” “System.Object []”,“DC2012” ,“Windows Server 2012 R2 Standard”,“192.168.1.251”,“fe80 :: cd63:76bf:3d2b:340f%12” 我想导出Diskinfo信息以及... – 2014-09-02 21:38:28