1

I have this codes below all working OK - however when i try to export to xls it does not export anything - i am getting blind now... any one can help?

$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
   }
}
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129

1 Answers1

1

Because you're piping into Format-Table. Format-Table is used ONLY when you send data to the screen, like when you want to view something in the console. Remove your Format-Table statement and this will work as is.

In this example I use Tee-Object to snap out a variable containing the output of your command, and then send the main stream on to Format-Table for viewing.

Then, in the next step, we pipe that variable into the CSV file you want to export.

$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
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • thanks so much mate,- does the below should be: "export" with a viable "| Tee-Object -Variable $Export | Format-Table -GroupBy date -Wrap" } $export | Export-Csv C:\temp\test4.csv – Jenna Shaik Dec 08 '15 at 17:42
  • No, when you use the Tee-Object command, you don't use a $ for the variable name. Tee-Object, Set-Variable and Get-Variable are probably the only cmdlets in which you don't put the `$`. – FoxDeploy Dec 08 '15 at 18:05
  • Thanks again mate - really powerful command - for some reason when i run, its only outputs data from one VPC, it lists from all VPCS from format-table however it only output to CSV last VPC - your input is greatly appreciated – Jenna Shaik Dec 08 '15 at 19:13
  • Any further ideas to above comment? – Jenna Shaik Dec 09 '15 at 08:12
  • ANYONE ELSE PLEASE CAN HELP ME? – Jenna Shaik Dec 09 '15 at 15:32
  • I think we were missing an extra close curly brace, should work now :) – FoxDeploy Dec 09 '15 at 15:58
  • You are a legend - thank you so much mate, additional pair eyes always helps :-) – Jenna Shaik Dec 10 '15 at 07:50
  • Anyone else - this script is great for pulling reports from AWS, Make use of it... Credit to FoxDeploy – Jenna Shaik Dec 10 '15 at 07:51