我想在PS中理清数组。我试图解决的问题是返回一个复制的虚拟机列表和一些基本的统计数据。如何在PowerShell中创建自定义数组?
已经通过网站和建议的大量阅读,我能得到的最好的是下面的脚本:
$myArray = @()
$vms = get-vm | where-object { $_.replicationstate -ne "Disabled" }
foreach ($vm in $vms)
{
$vmRepl = Get-VMReplication
$replfreq = (New-TimeSpan -seconds $vmRepl.replicationfrequencysec)
$lastrepl = $vmRepl.lastreplicationtime
$nextrepl = $lastrepl + $replfreq
$secfrom = [math]::Round((New-TimeSpan -start $vmRepl.lastreplicationtime).TotalSeconds)
$secto = [math]::Round((New-TimeSpan -end ($vmRepl.lastreplicationtime + $replfreq)).TotalSeconds)
$obj = New-Object System.Object
$obj | Add-Member -MemberType NoteProperty -Name Name -Value $vmRepl.Name
$obj | Add-Member -MemberType NoteProperty -Name ReplicationFrequencySec -Value $vmRepl.replicationfrequencysec
$obj | Add-Member -MemberType NoteProperty -Name SecondsSinceLastRepl -Value $secfrom
$obj | Add-Member -MemberType NoteProperty -Name SecondsUntilNextRepl -Value $secto
$obj | Add-Member -MemberType NoteProperty -Name LastReplication -Value $lastrepl
$obj | Add-Member -MemberType NoteProperty -Name NextReplication -Value $nextrepl
$myArray += $obj
}
write-output $myArray | ft -AutoSize
这工作时,我只有一个虚拟机,但是当有多个的出现卷曲内输出括号。
我想我终于在正确的轨道上。我只需要有人帮助我理清拼图的其余部分。
另一个奇怪的是,New-TimeSpan停止与多个虚拟机一起工作。
在此先感谢。
布拉登
我看不出你是如何使用'$ vm'变量的。 – PetSerAl
你可以创建一个哈希表:'$ obj = @ {key0 ='val'; key1 = 3.14}'并且过滤管道:'$ array |选择key0,key1 | ft -AutoSize' – mtman