Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell数据表_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell数据表

Powershell数据表,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,有人能为powershell表提供一些帮助吗 脚本的工作部分 Function CheckWMI { Param ( [Parameter(Mandatory=$True)] $Computers ) $CheckWMIResults = New-Object system.Data.DataTable $Function = $CheckWMIResults.columns.add("ComputerName", [System.Type]::

有人能为powershell表提供一些帮助吗

脚本的工作部分

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"
如您所见,它获取每个ip地址并为它们创建一个单独的行


现在,我如何选择这些行中的一行并更新它,例如第二行的count列?

没有必要为此使用像DataTable这样繁重的数据结构。您只需要一个简单的集合,如数组和通用的
PSObject
。下面将重写上面的脚本,然后将第一台计算机的
结果设置为
完成

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;
如果确实需要类型检查(数据表
提供给您),请定义您自己的类型

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;

有关第二种方法的更多详细信息,请参见和
Get Help Add Type
(对于琐碎的情况,您可以使用struct而不是class)。

对于这种情况,不需要使用像DataTable这样重的数据结构。您只需要一个简单的集合,如数组和通用的
PSObject
。下面将重写上面的脚本,然后将第一台计算机的
结果设置为
完成

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;
如果确实需要类型检查(数据表
提供给您),请定义您自己的类型

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;

有关第二种方法的更多详细信息,请参见和
Get Help Add Type

最后一个问题,如何使用计算机名选择行,就像在集合中搜索任何其他内容一样
$Results | Where object{$\.ComputerName-eq“criteria”}
最后一个问题,我如何使用ComputerName选择行,就像在集合中搜索任何其他内容一样<代码>$Results |其中对象{$\.ComputerName-eq“criteria”}