0

Can anyone give some help with powershell tables?

The working part of the script

Function CheckWMI {
Param (
    [Parameter(Mandatory=$True)]
    $Computers

)
        $CheckWMIResults = New-Object system.Data.DataTable
        $Function = $CheckWMIResults.columns.add("ComputerName", [System.Type]::GetType("System.String") )
        $Function = $CheckWMIResults.columns.add("Attempts", [System.Type]::GetType("System.Int32") )
        $Function = $CheckWMIResults.columns.add("Result", [System.Type]::GetType("System.String") )

    ForEach ($Computer in $Computers) {
        $CheckWMIResults.Rows.Add($Computer,"0","Incomplete")
    }

}

CheckWMI "192.168.1.8","192.168.1.7","192.168.1.6"

As you can see it takes each of the ip addresses and create a separate row for them.

Now how can I select one of those rows and update it, such as the count column of the second row?

Nick W.
  • 1,536
  • 3
  • 24
  • 40

1 Answers1

3

There is no need to use a data structure so heavy as a DataTable for this. All you need is a simple collection like an array and the generic PSObject. The following rewrites your script above, then sets the Result of the first computer to Complete:

Function CheckWMI {
Param (
    [Parameter(Mandatory=$True)]
    [string[]]$Computers

)
    $CheckWMIResults = @();

    ForEach ($Computer in $Computers) {
        $TempResults = New-Object PSObject;
        $TempResults | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer;
        $TempResults | Add-Member -MemberType NoteProperty -Name "Attempts" -Value 0;
        $TempResults | Add-Member -MemberType NoteProperty -Name "Result" -Value "Incomplete";
        $CheckWMIResults += $TempResults;
    }
    $CheckWMIResults;
}

$Results = CheckWMI -Computers "192.168.1.8","192.168.1.7","192.168.1.6"
$Results[0].Result = "Complete";
$Results;

If you do need type checking (which the DataTable gives you), define your own type.

add-type @"
public class WMIResults {
   public string ComputerName;
   public int Attempts;
   public string Result;
}
"@

Function CheckWMI {
Param (
    [Parameter(Mandatory=$True)]
    [string[]]$Computers

)
    $CheckWMIResults = @();

    ForEach ($Computer in $Computers) {
        $TempResults = New-Object WMIResults;
        $TempResults.ComputerName = $Computer
        $TempResults.Attempts = 0;
        $TempResults.Result = "Incomplete";
        $CheckWMIResults += $TempResults;
    }
    $CheckWMIResults;
}

$Results = CheckWMI -Computers "192.168.1.8","192.168.1.7","192.168.1.6"
$Results[0].Result = "Complete";
$Results;

See http://blogs.msdn.com/b/powershell/archive/2009/03/11/how-to-create-an-object-in-powershell.aspx and Get-Help Add-Type for more details on this second method ( you could use a struct instead of a class for trivial cases, but classes are generally a better idea).

Community
  • 1
  • 1
alroc
  • 27,574
  • 6
  • 51
  • 97
  • one last question, how would I select the row by using the ComputerName – Nick W. Apr 08 '13 at 13:07
  • The same way you search for anything else in a collection. `$Results|Where-object{$_.ComputerName -eq "criteria"}` – alroc Apr 08 '13 at 13:12