为什么Get-EC2VPC的输出导出为空CSV



我下面的代码都正常工作-但是当我试图导出到xls时,它什么都不导出-我现在失明了。。。有谁能帮忙吗?

$StoppedInstances = (Get-EC2Instance).instances | Where-Object {$_.State.Name -eq "stopped"  -or $_.State.Name -eq "running"} 
$VPCS = Get-EC2Vpc
foreach ($VPC in $VPCS) {
    $StoppedInstances | Where-Object {$_.VpcId -eq $VPC.VpcId} | foreach {
       New-Object -TypeName PSObject -Property @{
           'InstanceId' = $_.InstanceId
           'InstanceName' = ($_.Tags | Where-Object {$_.Key -eq 'Name'}).Value
           'LaunchTime' = $_.LaunchTime
           'State' = $_.State.Name
           #'State1' = $_.State.GetType()
           'Private IP' = $_.PrivateIpAddress
           'Public IP' = $_.PublicIpAddress
           'Public Dns' = $_.PublicDnsName
           'loadbalace' = $_.AmiLaunchIndex
            'vpcID' = $_.VpcId
            'instancetype' = $_.InstanceType
           'EBSDISK' = $_.BlockDeviceMappings.Count
           'ELB' = $_.NetworkInterfaces
       } | Format-Table -GroupBy date -Wrap | Export-Csv C:temptest4.csv
   }
}

因为您正在导入Format Table。"格式化表格"仅在将数据发送到屏幕时使用,例如当您想在控制台中查看某些内容时。删除您的格式表语句,这将按原样工作。

在本例中,我使用Tee对象来捕捉包含命令输出的变量,然后将主流发送到Format Table以进行查看。

然后,在下一步中,我们将该变量管道传输到要导出的CSV文件中。

$StoppedInstances = (Get-EC2Instance).instances | Where-Object {$_.State.Name -eq "stopped"  -or $_.State.Name -eq "running"} 
$VPCS = Get-EC2Vpc
$export = foreach ($VPC in $VPCS) {
    $StoppedInstances | Where-Object {$_.VpcId -eq $VPC.VpcId} | foreach {
       New-Object -TypeName PSObject -Property @{
           'InstanceId' = $_.InstanceId
           'InstanceName' = ($_.Tags | Where-Object {$_.Key -eq 'Name'}).Value
           'LaunchTime' = $_.LaunchTime
           'State' = $_.State.Name
           #'State1' = $_.State.GetType()
           'Private IP' = $_.PrivateIpAddress
           'Public IP' = $_.PublicIpAddress
           'Public Dns' = $_.PublicDnsName
           'loadbalace' = $_.AmiLaunchIndex
            'vpcID' = $_.VpcId
            'instancetype' = $_.InstanceType
           'EBSDISK' = $_.BlockDeviceMappings.Count
           'ELB' = $_.NetworkInterfaces
       } 
    }
} 
$export | Format-Table -GroupBy date -Wrap 
$export | Export-Csv C:temptest4.csv

最新更新