2016-09-06 81 views
-1

目前我堅持一個項目,因爲我試圖顯示多個服務器的輸出Get-WmiObject -Class OperatinSsytem到一個DataGridView的輸出。追加DataGridview與服務器輸出..目前被覆蓋

下面就是迄今已完成:

$MainForm_Load = { 
    #TODO: Initialize Form Controls here 
} 

#region Control Helper Functions 
function Load-DataGridView 
{ 
    <# 
    .SYNOPSIS 
     This functions helps you load items into a DataGridView. 

    .DESCRIPTION 
     Use this function to dynamically load items into the DataGridView control. 

    .PARAMETER DataGridView 
     The DataGridView control you want to add items to. 

    .PARAMETER Item 
     The object or objects you wish to load into the DataGridView's items collection. 

    .PARAMETER DataMember 
     Sets the name of the list or table in the data source for which the DataGridView is displaying data. 

    .PARAMETER AutoSizeColumns 
     Resizes DataGridView control's columns after loading the items. 
    #> 
    Param (
     [ValidateNotNull()] 
     [Parameter(Mandatory=$true)] 
     [System.Windows.Forms.DataGridView]$DataGridView, 
     [ValidateNotNull()] 
     [Parameter(Mandatory=$true)] 
     $Item, 
     [Parameter(Mandatory=$false)] 
     [string]$DataMember, 
     [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None' 
    ) 
    $DataGridView.SuspendLayout() 
    $DataGridView.DataMember = $DataMember 

    if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0) 
    { 
     $DataGridView.DataSource = $Item.Tables[0] 
    } 
    elseif ($Item -is [System.ComponentModel.IListSource]` 
    -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView]) 
    { 
     $DataGridView.DataSource = $Item 
    } 
    else 
    { 
     $array = New-Object System.Collections.ArrayList 

     if ($Item -is [System.Collections.IList]) 
     { 
      $array.AddRange($Item) 
     } 
     else 
     { 
      $array.Add($Item) 
     } 
     $DataGridView.DataSource = $array 
    } 

    if ($AutoSizeColumns -ne 'None') 
    { 
     $DataGridView.AutoResizeColumns($AutoSizeColumns) 
    } 

    $DataGridView.ResumeLayout() 
} 

function ConvertTo-DataTable 
{ 
    <# 
     .SYNOPSIS 
     Converts objects into a DataTable. 

     .DESCRIPTION 
     Converts objects into a DataTable, which are used for DataBinding. 

     .PARAMETER InputObject 
     The input to convert into a DataTable. 

     .PARAMETER Table 
     The DataTable you wish to load the input into. 

     .PARAMETER RetainColumns 
     This switch tells the function to keep the DataTable's existing columns. 

     .PARAMETER FilterWMIProperties 
     This switch removes WMI properties that start with an underline. 

     .EXAMPLE 
     $DataTable = ConvertTo-DataTable -InputObject (Get-Process) 
    #> 
    [OutputType([System.Data.DataTable])] 
    param(
    [ValidateNotNull()] 
    $InputObject, 
    [ValidateNotNull()] 
    [System.Data.DataTable]$Table, 
    [switch]$RetainColumns, 
    [switch]$FilterWMIProperties) 

    if($null -eq $Table) 
    { 
     $Table = New-Object System.Data.DataTable 
    } 

    if ($InputObject -is [System.Data.DataTable]) 
    { 
     $Table = $InputObject 
    } 
    elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0) 
    { 
     $Table = $InputObject.Tables[0] 
    } 
    else 
    { 
     if (-not $RetainColumns -or $Table.Columns.Count -eq 0) 
     { 
      #Clear out the Table Contents 
      $Table.Clear() 

      if ($null -eq $InputObject) { return } #Empty Data 

      $object = $null 
      #find the first non null value 
      foreach ($item in $InputObject) 
      { 
       if ($null -ne $item) 
       { 
        $object = $item 
        break 
       } 
      } 

      if ($null -eq $object) { return } #All null then empty 

      #Get all the properties in order to create the columns 
      foreach ($prop in $object.PSObject.Get_Properties()) 
      { 
       if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties 
       { 
        #Get the type from the Definition string 
        $type = $null 

        if ($null -ne $prop.Value) 
        { 
         try { $type = $prop.Value.GetType() } 
         catch { } 
        } 

        if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object') 
        { 
         [void]$table.Columns.Add($prop.Name, $type) 
        } 
        else #Type info not found 
        { 
         [void]$table.Columns.Add($prop.Name) 
        } 
       } 
      } 

      if ($object -is [System.Data.DataRow]) 
      { 
       foreach ($item in $InputObject) 
       { 
        $Table.Rows.Add($item) 
       } 
       return @(,$Table) 
      } 
     } 
     else 
     { 
      $Table.Rows.Clear() 
     } 

     foreach ($item in $InputObject) 
     { 
      $row = $table.NewRow() 

      if ($item) 
      { 
       foreach ($prop in $item.PSObject.Get_Properties()) 
       { 
        if ($table.Columns.Contains($prop.Name)) 
        { 
         $row.Item($prop.Name) = $prop.Value 
        } 
       } 
      } 
      [void]$table.Rows.Add($row) 
     } 
    } 

    return @(,$Table) 
} 
#endregion 

$buttonShow_Click = { 
    $Servers = Get-Content $textbox1.Text 
    foreach ($Server in $Servers) 
    { 
     $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server 
     for ($i = 0; $i -lt $Servers.count; $i++) 
     { 
      $table = ConvertTo-DataTable -InputObject $OS -FilterWMIProperties 
      Load-DataGridView -DataGridView $datagridview1 -Item $table 
      $datagridview1.DataSource = $table 
     } 
    } 
} 

數據在弗里斯特行覆蓋掉了,它不是追加。如何獲取下一臺服務器的數據?

+0

請將那段代碼縮小到仍然顯示問題的最短腳本。 –

+0

$ buttonShow_Click = { $服務器=獲取內容$ textbox1.Text 的foreach(在$ $服務器服務器) { $ OS = GET-WmiObject可以-Class Win32_OperatingSystem -ComputerName $服務器 爲($ I = 0; $我-lt $ Servers.count; $ I ++){ $ 表=的ConvertTo-的DataTable -InputObject $ OS -FilterWMIProperties 負載的DataGridView -DataGridView $ datagridview1 -Item $表 $ datagridview1.DataSource = $表 } } } – Aamir

+0

請編輯您的問題。評論中的代碼是不可讀的。 –

回答

0

所有這兩條線的第一位是半斤八兩:

 Load-DataGridView -DataGridView $datagridview1 -Item $table 
     $datagridview1.DataSource = $table 

我改變你的代碼一點,所以現在它應該工作。

$buttonShow_Click = { 
    $Servers = Get-Content $textbox1.Text 
    $set = @() 
    foreach ($Server in $Servers) 
    { 
     $set += Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server 
    } 
    $table = ConvertTo-DataTable -InputObject $set -FilterWMIProperties 
    $datagridview1.DataSource = $table 
} 
+0

謝謝你這麼多的朋友..這真的有幫助..最後一個查詢.. – Aamir

+0

如果我想擁有不同的表頭,而不是實際的WMI屬性名稱?例如,我將Win32_Operatingsystem屬性名稱作爲標題,但我希望使用ServerName而不是PSComputerName,而不是WindowsDirectory屬性我的自定義名稱,如WinDir等。 – Aamir

+0

函數Get-ServerInfo \t $ Servers = Get-Content $ textboxServers 。文本 \t $套裝= @() \t的foreach($在$服務器服務器) \t {$ OS = GET-WmiObject可以-Class Win32_OperatingSystem -ComputerName $服務器 \t \t \t \t \t \t $道具= @ { \t \t \t \t'ServerName'= $ Server; \t \t \t \t'OS'= $ OS.Caption; \t \t \t \t'InstallDir'= $ OS.WindowsDirectory; \t \t \t \t'Version'= $ OS.version; \t \t \t \t'ServicePack'= $ OS。CSDVersion \t \t \t} \t \t \t新物體PSObject,物業$道具 \t \t \t \t \t \t $表=的ConvertTo-的DataTable -InputObject $設置-FilterWMIProperties \t \t \t#負載的DataGridView -DataGridView $ datagridview1 -Item $表 \t \t \t $ datagridview1.DataSource = $表 – Aamir