[英]PowerShell variable within variable
I am trying to add a variable $numberOfNics
which adds nics based on the number. 我正在尝试添加一个变量
$numberOfNics
,它根据数字添加nics。 Right now this is my code, and I am a little confused as to how to put it into a loop. 现在这是我的代码,我对如何将它放入循环感到困惑。 I know an array is probably the way to go but not quite sure how to make an array in this sense that just calls variables instead of sets them.
我知道一个数组可能是要走的路,但不太确定如何在这个意义上创建一个只调用变量而不是设置它们的数组。
$numberOfNics=1
$networkname1 = "ondemandport1"
$networkname2 = "ondemandport2"
$networkname3 = "ondemandport3"
$networkname4 = "ondemandport4"
$networkname5 = "ondemandport5"
$networkname6 = "ondemandport6"
$networkname7 = "ondemandport7"
$networkname8 = "ondemandport8"
$networkname9 = "ondemandport9"
$networkInterface1 = New-AzureRmNetworkInterface -Name $networkname1
$networkInterface2 = New-AzureRmNetworkInterface -Name $networkname2
$networkInterface3 = New-AzureRmNetworkInterface -Name $networkname3
$networkInterface4 = New-AzureRmNetworkInterface -Name $networkname4
$networkInterface5 = New-AzureRmNetworkInterface -Name $networkname5
To tidy this up, I would like to make a loop something like this 为了整理这个,我想做一个像这样的循环
for ($i = 0; $i -lt $numberOfNics; $i++)
{
$networkInterface$i = New-AzureRmNetworkInterface -Name $networkname$i
}
EDIT: Thanks Martin, but now I am having trouble calling the variable. 编辑:谢谢马丁,但现在我无法调用变量。
$networkInterfaces = 1 .. $numberOfNics | ForEach-Object {
if ($a -eq 0){
New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[$a].Id -PublicIpAddressId $publicIp.Id -force
}
ElseIf ($a=1){
New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[$a].Id -force
}
Else{
New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[2].Id -force
}
}
#
also looking to make this into a array loop #
也希望将其变成一个数组循环
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[1].Id -Primary
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[2].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[3].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[4].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[5].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[6].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[7].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[8].Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[9].Id
Error message: Add-AzureRmVMNetworkInterface : Cannot validate argument on parameter 'Id'. 错误消息:Add-AzureRmVMNetworkInterface:无法验证参数“Id”的参数。 The argument is null or empty.
参数为null或空。 Provide an argument that is not null or empty, and then try the command again.
提供非null或空的参数,然后再次尝试该命令。
You can also do it with one line: 您也可以使用一行:
$networkInterfaces = 1 .. $numberOfNics | ForEach-Object { New-AzureRmNetworkInterface -Name "ondemandport$_" }
Now you can access the interfaces by index, for example: 现在您可以通过索引访问接口,例如:
$networkInterfaces[2]
By the way, consider using ARM templates instead. 顺便说一句,请考虑使用ARM模板。
Edit: 编辑:
$networkInterfaces = 1 .. $numberOfNics | ForEach-Object {
$isPrimary = $_ -eq 0 # set the first one as primary
$nic = Get-AzureRmNetworkInterface -Name "ondemandport$_" -ResourceGroupName $resourceGroupName
Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id -Primary:$isPrimary
}
You should be able to do something like this: 你应该可以做这样的事情:
$numberOfNics= 10
$networkname = "ondemandport"
$networkInterface = @()
for ($i = 0; $i -le $numberOfNics; $i++)
{
$networkInterface += ,@( New-AzureRmNetworkInterface -Name ($networkname + $i ))
}
Not sure if it will work with the New-AzureRmNetworkInterface but works with other New- commands 不确定它是否可以与New-AzureRmNetworkInterface一起使用,但可以与其他New-命令一起使用
You could use PowerShell Get-Variable
cmdlet. 您可以使用PowerShell
Get-Variable
cmdlet。 One thing which I would like to point out is the for loop
which you are using will not be limited till $numberOfNics
, but as a matter of fact, it's length will depend upon $networkname
variable. 我想指出的一件事是你使用的
for loop
在$numberOfNics
之前不会受到限制,但事实上,它的长度将取决于$networkname
变量。 For example, you are having networkname
variables from $networkname1
to $networkname9
, so your loop will look something like this - 例如,你有从
$networkname1
到$networkname9
networkname
变量,所以你的循环看起来像这样 -
$NetworkInfo = @()
for($i = 1; $i -le 9; $i++) {$NetworkInfo += Get-Variable("networkname" + $i)}
Now $NetworkInfo
has 现在
$NetworkInfo
有了
Name Value
---- -----
networkname1 ondemandport1
networkname2 ondemandport2
networkname3 ondemandport3
networkname4 ondemandport4
networkname5 ondemandport5
networkname6 ondemandport6
networkname7 ondemandport7
networkname8 ondemandport8
networkname9 ondemandport9
You can put another for
loop something like this - 你可以把其他
for
循环是这样的-
$NetworkInterface = @()
for ($i = 0; $i -lt $NetworkInfo.Length; $i++)
{
$NetworkInterface += New-AzureRmNetworkInterface -Name $NetworkInfo.Value[$i]
}
Individual elements can then be accessed by $NetworkInterface[0], $NetworkInterface[1]
... and so on. 然后可以通过
$NetworkInterface[0], $NetworkInterface[1]
等访问各个元素,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.