简体   繁体   中英

Not able to get Image ID of existence ec2 instance from powershell

I am trying to get the spowershell script so that I can pull up the image id of existence ec2 instance.

like :

Write-Host -BackgroundColor White -ForegroundColor Blue "What is the ID of the instance"
$tid=Read-Host "Instance ID"
Write-Host -BackgroundColor White -ForegroundColor Blue "Input the region the instance inhabits"
$tregion=Read-Host "Region"
$Tinstancetype = ((Get-EC2Instance -InstanceId $tid).instances).InstanceType
Write-Host "Instance Type : $Tinstancetype"

Like in this code above, I entered Instance id and it spill out instance type. I need the code which can spill out Image ID.

You are only requesting the Instance Type property. Get-EC2Instance will bring back an array of objects, each object has a number of properties. your command is only displaying the value of the property `InstanceType'

If you run (Get-EC2Instance -InstanceId $tid).instances | Select * (Get-EC2Instance -InstanceId $tid).instances | Select * you will see all the properties and their values, one of those properties will be image ID.

Example of Get-EC2Instance output:

C:\> (Get-EC2Instance -InstanceId i-12345678).Instances

AmiLaunchIndex        : 0
Architecture          : x86_64
BlockDeviceMappings   : {/dev/sda1}
ClientToken           : TleEy1448154045270
EbsOptimized          : False
Hypervisor            : xen
IamInstanceProfile    : Amazon.EC2.Model.IamInstanceProfile
ImageId               : ami-12345678
InstanceId            : i-12345678
InstanceLifecycle     :
InstanceType          : t2.micro
KernelId              :
KeyName               : my-key-pair
LaunchTime            : 12/4/2015 4:44:40 PM
Monitoring            : Amazon.EC2.Model.Monitoring
NetworkInterfaces     : {ip-10-0-2-172.us-west-2.compute.internal}
Placement             : Amazon.EC2.Model.Placement
Platform              : Windows
PrivateDnsName        : ip-10-0-2-172.us-west-2.compute.internal
PrivateIpAddress      : 10.0.2.172
ProductCodes          : {}
PublicDnsName         : 
PublicIpAddress       : 
RamdiskId             :
RootDeviceName        : /dev/sda1
RootDeviceType        : ebs
SecurityGroups        : {default}
SourceDestCheck       : True
SpotInstanceRequestId :
SriovNetSupport       :
State                 : Amazon.EC2.Model.InstanceState
StateReason           :
StateTransitionReason :
SubnetId              : subnet-12345678
Tags                  : {Name}
VirtualizationType    : hvm
VpcId                 : vpc-12345678

Your script will look something like this:

Write-Host -BackgroundColor White -ForegroundColor Blue "What is the ID of the instance"
$tid=Read-Host "Instance ID"
Write-Host -BackgroundColor White -ForegroundColor Blue "Input the region the instance inhabits"
$tregion=Read-Host "Region"
$Instance = ((Get-EC2Instance -InstanceId $tid).instances)
$Tinstancetype = $Instance.InstanceType
$ImageID =$Instance.ImageId
Write-Host "Instance Type : $Tinstancetype"
Write-Host "Instance Type : $ImageID"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM