Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 - Fatal编程技术网

Powershell嵌套变量

Powershell嵌套变量,powershell,Powershell,Soo试图做如下的事情 $Computers=获取内容。\~test.txt ForEach ($Computer in $Computers) { $computer.Attempts += 1 $computer.result = Successful } ++编辑++ 我想要的输出是 $MyComputerName.Attempt += 1 $MyComputerName.result 因此,每台计算机都有自己的名称,在脚本中创建一个基本表,这样它就不必一直从文本文

Soo试图做如下的事情

$Computers=获取内容。\~test.txt

ForEach ($Computer in $Computers) {
     $computer.Attempts += 1
     $computer.result = Successful
}
++编辑++ 我想要的输出是

$MyComputerName.Attempt += 1
$MyComputerName.result
因此,每台计算机都有自己的名称,在脚本中创建一个基本表,这样它就不必一直从文本文件中读取某些信息


其中$computer是文本文件中的计算机名。我正在尝试创建一种更快的检查贵重物品的方法。

如果我跟踪您,您希望为每台计算机创建新对象:

$Computers = Get-Content .\~test.txt

ForEach ($Computer in $Computers) {
    New-Object PSObject -Property @{
        Name = $Computer 
        Attempts = 1
        Result = 'Successful'
    }
}

好吧,最终我们弄明白了,事情是这样的。导入CSV是为每一行创建一个新对象的好工具

$Computers = @(Import-CSV .\Computers.txt)

ForEach ($Computer IN $Computers) {
    Write-host $Computer.Name
    Write-Host $Computer.IP
    Write-Host $Computer.Other
}
Computers.txt:

52123-9421,123.123.123.123,yea
HELLBOMBS-PC,123.123.123.123,yea
52123-942dv,123.123.123.123,yea
52123-942RM,123.123.123.123,yea

你的代码没有多大意义。请更详细地解释你想要实现的目标。那么,如果我想从中加载信息,我将如何调用该对象。让我们在脚本的后面说,我想检查$Computer.result=successful?将foreach循环分配给一个变量,并使用数组索引来获取它(在多个对象的情况下)。另一种方法是用它创建一个哈希表,然后使用哈希表“Key”名称来引用该对象。