[英]Powershell Multiple variables in foreach loop getting AWS-EC2 instance info
I need to get instanceId and PrivateIpAddress from all my EC2 instances in my environment and insert into table. 我需要从我的环境中的所有EC2实例中获取instanceId和PrivateIpAddress,并将其插入表中。 I have this but doesn't seem to work.
我有这个,但似乎没有用。 I seem to get everything not just the IP and ID.
我似乎得到的不仅是IP和ID。
$instancestate = (get-ec2instance).RunningInstance
foreach ($instances in $instancestate)
{
$query = "INSERT INTO prodinstances (Idinstance, IPAddress)
VALUES ('$instances.InstanceId','$instances.PrivateIpAddress')"
$Rows = Execute-MySQLNonQuery $conn $query
}
If I change the code 如果我更改代码
$instancestate = (get-ec2instance).RunningInstance.InstanceId
I get the ID and can insert it in the database. 我获得了ID,可以将其插入数据库中。 I can also change it to
我也可以将其更改为
$instancestate = (get-ec2instance).RunningInstance.PrivateIpAddress
and get the IPAddress and insert that into the database, but when i combine them I get all the info for the EC2 instances which does have .instanceId and .PrivateIpAddress in the list when I hover over the variable $instances. 并获取IPAddress并将其插入数据库,但是当我将它们组合时,当我将鼠标悬停在变量$ instances上时,我会获得列表中确实具有.instanceId和.PrivateIpAddress的EC2实例的所有信息。 Any Idea how to get both those parameters.
任何想法如何获得这两个参数。 My code seems correct but alas it is not...
我的代码似乎正确,但是事实并非如此……
"VALUES ('$instances.InstanceId'"
is the same as 是相同的
"VALUES ('" + $instances + ".InstanceId'"
Now it doesn't seem correct. 现在似乎不正确。 You need
$()
around it, inside the string: 您需要在字符串中包含
$()
:
"VALUES ('$($instances.InstanceId)'"
Fixed works like a charm... 固定作品就像魅力...
$instancestate = (get-ec2instance).RunningInstance
foreach ($instances in $instancestate)
{
$query = "insert into prodinstances (idinstance,IPAddress) VALUES ('$($instances.InstanceId)', '$($instances.PrivateIpAddress)')
ON duplicate key update idinstance='$($instances.InstanceId)',IPAddress='$($instances.PrivateIpAddr ess)'"
$Rows = Execute-MySQLNonQuery $conn $query
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.