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

时间:2015-12-08 16:57:50

标签: powershell amazon-web-services aws-powershell

我有以下所有正常工作的代码 - 但是当我尝试导出到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:\temp\test4.csv
   }
}

1 个答案:

答案 0 :(得分:1)

因为你要管道进入Format-Table。 Format-Table仅在您将数据发送到屏幕时使用,就像您想要在控制台中查看某些内容一样。删除Format-Table语句,这将按原样运行。

在这个例子中,我使用Tee-Object来捕捉包含命令输出的变量,然后将主流发送到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:\temp\test4.csv