是否可以调整此代码以导出foreach
循环外的所有行:
这很好(在循环内):
$vms = Get-VM | Where { $_.State –eq ‘Running’ } | Select-Object -ExpandProperty Name
foreach($vm in $vms) {
# Get network interface details
$out = Get-VMNetworkAdapter -vmname $vm | select VMName, MacAddress, IPAddresses
$vm_name = $out.VMName | Get-Unique
$ip = ($out.IPAddresses | ForEach-Object {
$_ | ? {$_ -notmatch ':'}
}) -join " "
# If more than 1 MAC , put it in same row separated by space (00:15:5D:58:12:5E 00:15:5D:58:12:5F )
$mac = ($out.MacAddress | ForEach-Object {
$_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}) -join ' '
$results = @()
$comp = Get-WmiObject Win32_ComputerSystem | Select-Object -ExpandProperty name
$obj = New-Object -TypeName psobject
$obj | Add-Member -MemberType NoteProperty -Name "VM NAME" -Value $vm_name
$obj | Add-Member -MemberType NoteProperty -Name "IP ADDRESS" -Value $ip
$obj | Add-Member -MemberType NoteProperty -Name "MAC ADDRESS" -Value $mac
$obj | Add-Member -MemberType NoteProperty -Name "HYPER-V HOST" -Value $comp
$results += $obj
Write-Output $results
$results| Export-Csv -Path "c:\1.csv" -NoTypeInformation -append
}
但是,当我将$results| Export-Csv -Path "c:\1.csv" -NoTypeInformation -append
移到循环外时,
仅最后一行保存到CSV
在内部循环中,$results
变量包含所有行,当我在循环write-host $results
外部将该变量移动时,仅打印(最后)一行
答案 0 :(得分:2)
对于它的价值,您的代码可以精简很多。您的许多步骤都是不必要的:
$results = Get-VM | Where State –eq Running | Get-VMNetworkAdapter | ForEach-Object {
[pscustomobject]@{
'VM NAME' = $_.VMName
'IP ADDRESS' = ($_.IPAddresses -notmatch ':') -join ' '
'MAC ADDRESS' = ($_.MacAddress -replace '(..)(..)(..)(..)(..)','$1:$2:$3:$4:$5:') -join ' '
'HYPER-V HOST' = $env:COMPUTERNAME
}
}
$results | Export-Csv -Path "c:\1.csv" -NoTypeInformation
注意:
Get-VM
返回的VM直接传送到Get-VMNetworkAdapter
Where-Object
不需要脚本块。 Where State -eq Running
比Where { $_.State -eq 'Running' }
易于读写。$_.IPAddresses -notmatch ':'
之类的运算符,例如-notmatch
在数组上工作。 'a','b','0','c' -notmatch '\d'
将返回'a','b','c'
。-replace
也是如此。 'a0','b1','c2' -replace '\d',''
将返回return 'a','b','c'
。根本不需要foreach
循环。$env:COMPUTERNAME
应该比使用WMI获取计算机名称更快ForEach-Object {...}
脚本块)中创建的任何对象都将出现在脚本块的输出中。这就是$results = ... | ForEach-Object {...}
起作用的原因。无需使用@()
显式创建数组并将值添加到它们。[pscustomobject]
比使用Add-Member
要容易得多。答案 1 :(得分:0)
想通了:
在循环外移动了Sub asd()
'Fills the labels of the table
Sheets("report").Range("B8").Value = "Month"
Sheets("report").Range("C8").Value = "Production Cost"
Sheets("report").Range("D8").Value = "Inventory Cost"
Sheets("report").Range("E8").Value = "Total Cost"
Sheets("report").Range("B12").Value = "Total"
'Fills the labels of the table
lastrow = Sheets("data").Range("A1").End(xlDown).Row
For i = 2 To 0 Step -1
Sheets("report").Cells(lastrow - i - 2, 2).Value = Sheets("data").Cells(lastrow - i, 1).Value
Sheets("report").Cells(lastrow - i - 2, 3).Value = Sheets("data").Cells(lastrow - i, 2).Value * Sheets("data").Cells(lastrow - i, 4).Value
Sheets("report").Cells(lastrow - i - 2, 4).Value = Sheets("data").Cells(lastrow - i, 3).Value * Sheets("data").Cells(lastrow - i, 5).Value
Sheets("report").Cells(lastrow - i - 2, 5).Value = Sheets("report").Cells(lastrow - i - 2, 3).Value + Sheets("report").Cells(lastrow - i - 2, 4).Value
Next i
Sheets("report").Range("C12").Formula = "=SUM(C9:C11)"
Sheets("report").Range("D12").Formula = "=SUM(D9:D11)"
Sheets("report").Range("E12").Formula = "=SUM(E9:E11)"
End Sub
变量(使其成为“全局”变量)
$results