将属性从对象传递到Stop-EC2Instance cmdlet时出错

时间:2014-01-07 09:59:09

标签: powershell amazon-ec2 powershell-v3.0 aws-powershell

我正在编写许多函数,用于通过名称标记而不是ID来启动和停止EC2实例。首先,我写了一个报告函数,可以在下面找到。

Function Get-EC2InstanceReport{
    If((Get-Module -Name AWSPowerShell).Name -ne 'AWSPowerShell'){
        Throw 'AWSPowerShell module is not loaded'
    }
    Get-EC2Tag | `
        Where-Object {$_.ResourceType -eq 'instance' -and $_.Key -eq 'Name'} | `
        Select-Object @{Name='InstanceID'; Expression={$_.ResourceID}}, @{Name='Name'; Expression={$_.Value}}, `
        @{Name='Status'; Expression={Get-EC2InstanceStatus -IncludeAllInstances $true -InstanceId $_.ResourceID | %     {$_.InstanceState.Name}}}
}

启动实例的功能可以正常工作。

Function Start-EC2InstanceByName ([string]$Name){
    If((Get-Module -Name AWSPowerShell).Name -ne 'AWSPowerShell'){
        Throw 'AWSPowerShell module is not loaded'
    }
    [object]$EC2Instance = Get-EC2InstanceReport | Where-Object {$_.Name -eq $Name}

    Try{
        If($EC2Instance[0].Status -eq 'stopped'){
            Start-EC2Instance -InstanceId $EC2Instance[0].InstanceId | Out-Null
            Test-EC2InstanceStatus -Name $Name -EndState 'running'
        }
        Else{
            $ErrorMsg = "EC2 instance " + $EC2Instance[0].Name + " is not in the stopped state. It is " + $EC2Instance[0].Status + "."
            Throw $ErrorMsg
        }
    }
    Catch{
        $_
    }
}

但是当使用类似的方法来停止实例时,我会收到错误。

Function Stop-EC2InstanceByName ([string]$Name){
    If((Get-Module -Name AWSPowerShell).Name -ne 'AWSPowerShell'){
        Throw 'AWSPowerShell module is not loaded'
    }
    [object]$EC2Instance = Get-EC2InstanceReport | Where-Object {$_.Name -eq $Name}

    Try{
        If($EC2Instance[0].Status -eq 'running'){
            Stop-EC2Instance -Instance $EC2Instance[0].InstanceID | Out-Null
            Test-EC2InstanceStatus -Name $Name -EndState 'stopped'
        }
        Else{
            $ErrorMsg = "EC2 instance " + $EC2Instance[0].Name + " is not in the running state. It is " + $EC2Instance[0].Status + "."
            Throw $ErrorMsg
        }
    }
    Catch{
        $_
    }
}

错误可以在下面找到。

Stop-EC2Instance : No instances specified
At C:\GitProjects\DBA\aws-powershell-scripts\AWSFunctions.psm1:61 char:4
+             Stop-EC2Instance -Instance $EC2Instance[0].InstanceID | Out-Null
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Stop-EC2Instance], AmazonEC2Exception
    + FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.StopEC2InstanceCmdlet

非常感谢任何帮助。如果您需要任何进一步的信息,请告诉我。

进一步发展。

是的,没有解决错误发生的原因,并在亚马逊的AWS论坛上打开https://forums.aws.amazon.com/thread.jspa?threadID=143319

但是可以通过将功能更改为以下功能来创建所需的行为。

Function Stop-EC2InstanceByName([string] $ Name){     If((Get-Module -Name AWSPowerShell).Name -ne'AWSPowerShell'){         抛出'AWSPowerShell模块未加载'     }     [object] $ EC2Instance = Get-EC2InstanceReport | Where-Object {$ _。Name -eq $ Name}

Try{
    If($EC2Instance[0].Status -eq 'running'){
        Get-EC2Instance -Filter @{Name="tag:Name"; Value=$Name} | Stop-EC2Instance | Out-Null
        Test-EC2InstanceStatus -Name $Name -EndState 'stopped'
    }
    Else{
        $ErrorMsg = "EC2 instance " + $EC2Instance[0].Name + " is not in the running state. It is " + $EC2Instance[0].Status + "."
        Throw $ErrorMsg
    }
}
Catch{
$_
}
}

3 个答案:

答案 0 :(得分:1)

只需通过.ToString()转换即可获得所需的结果,而无需通过管道传递输入。

# 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...

哪个是......很奇怪,因为当我们在你的实例对象上使用Get-Member时,我们可以看到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

通过管道输入传递InstanceID是有效的,因为它可以接受System.Object[],而它似乎明确使用-Instance,而是使用字符串实例ID。 Stop-EC2Instance documentation证实了这一点:

-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, )

答案 1 :(得分:0)

同盟,

我相信你的停止语法错误。

你的开始命令(工作):

Start-EC2Instance -InstanceId $EC2Instance[0].InstanceId

你的停止命令(不工作):

Stop-EC2Instance -Instance $EC2Instance[0].InstanceID

尝试将'-Instance'标志更新为'-InstanceId',看看它是怎么回事。 : - )

-Tim

答案 2 :(得分:0)

是的,没有解决错误发生的原因,并在亚马逊的AWS论坛上打开https://forums.aws.amazon.com/thread.jspa?threadID=143319

但是可以通过将功能更改为以下功能来创建所需的行为。

Function Stop-EC2InstanceByName ([string]$Name){
    If((Get-Module -Name AWSPowerShell).Name -ne 'AWSPowerShell'){
        Throw 'AWSPowerShell module is not loaded'
    }
    [object]$EC2Instance = Get-EC2InstanceReport | Where-Object {$_.Name -eq $Name}

    Try{
        If($EC2Instance[0].Status -eq 'running'){
            Get-EC2Instance -Filter @{Name="tag:Name"; Value=$Name} | Stop-EC2Instance | Out-Null
            Test-EC2InstanceStatus -Name $Name -EndState 'stopped'
        }
        Else{
            $ErrorMsg = "EC2 instance " + $EC2Instance[0].Name + " is not in the running state. It is " + $EC2Instance[0].Status + "."
            Throw $ErrorMsg
        }
    }
    Catch{
    $_
    }
}