Simply converting via .ToString()
got the desired result for me, without having to pass input via pipeline.
# Failed attempt
PS C:\> Stop-EC2Instance -Instance $myInstance.InstanceId
Stop-EC2Instance : No instances specified
At line:1 char:17
+ Stop-EC2Instance <<<< -Instance $myInstance.InstanceId
+ CategoryInfo : NotSpecified: (:) [Stop-EC2Instance], AmazonEC2Exception
+ FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.StopEC2InstanceCmdlet
# Successful attempt
PS C:\> Stop-EC2Instance -Instance $myInstance.InstanceId.ToString()
# Goodnight instance...
Which is... odd, because when we use Get-Member
on your instance object we can see that there is a string defined there in InstanceID:
TypeName: Selected.Amazon.EC2.Model.ResourceTag
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
InstanceID NoteProperty System.String InstanceID=i-abcd1234
Name NoteProperty System.String Name=MyInstance
Status NoteProperty Status=null
Passing the InstanceID via pipeline input worked because it can accept System.Object[]
, whereas it seems explicitly using -Instance
would rather you use a string Instance ID. The Stop-EC2Instance documentation corroborates this:
-Instance <Object[]>
Identifies the set of instances to stop or terminate. Accepts a string instance ID or a collection of RunningInstance or Reservation objects. If a Reservation object is supplied, all of the instances in the reservation are processed.
Required? False
Position? 1
Accept pipeline input? True (ByValue, )