Powershell错误输出
伙计们,我需要这里的帮助,我想返回$Location002和$Location003内容看看输出中有什么内容Powershell错误输出,powershell,Powershell,伙计们,我需要这里的帮助,我想返回$Location002和$Location003内容看看输出中有什么内容 $Location = "westus2" $Location002 = "westeurope" $Location003 = "eastasia" [int]$VMCount = Read-Host "How many VMs?" 1..$VMCount | ForEach-Object {
$Location = "westus2"
$Location002 = "westeurope"
$Location003 = "eastasia"
[int]$VMCount = Read-Host "How many VMs?"
1..$VMCount | ForEach-Object {
$i = $_
# define name for VM, will be used for other resources
if ($i -eq 1) {
$locname = "$Location"
Write-Output $locname
}
else {
$locname = $("Location00" + "$i")
Write-Output $locname
}
}
输出:
我需要输出西欧和东亚这不是我要做的,但更直接的问题是,您正在将一个串联字符串分配给
$location
并写入输出流。我认为您要做的是引用前面变量的值
有一些聪明的语法。我很难记住它们。然而,以下将是一个开始
$Location = "westus2"
$Location002 = "westeurope"
$Location003 = "eastasia"
[int]$VMCount = Read-Host "How many VMs?"
1..$VMCount | ForEach-Object {
$i = $_
# define name for VM, will be used for other resources
if ($i -eq 1) {
$name = "$VMName"
$locname = "$Location"
Write-Output $locname
}
else {
$name = "$VMName" + "00" + "$i"
$locname = (Get-Variable ("Location00" + "$i")).Value
Write-Output $locname
}
}
使用备选方案更新:
我仍然不确定目标是什么,但根据原始样本,在位置和虚拟机号码之间似乎存在1:1的关系。也就是说,如果你超过了虚拟机的数量,你就必须根据预期的模式来调整它
$Locations = 'westus2', 'westeurope', 'eastasia'
[int]$VMCount = Read-Host 'How many VMs?'
For( $i = 0; $i -lt $VMCount ; ++$i )
{
$Locations[$i]
}
进一步更新:
关于:
在这种模式中使用模运算符对于将一个列表分布到另一个列表是非常有效的。我写了一篇关于这个和其他一些用法的小帖子。您需要使用
Get variable
检索变量的内容。您还可以避免生成$i
变量的额外步骤,而是使用自动变量$\ucode>
$Location = "westus2"
$Location002 = "westeurope"
$Location003 = "eastasia"
[int]$VMCount = Read-Host "How many VMs?"
1..$VMCount | ForEach-Object {
# define name for VM, will be used for other resources
if ($_ -eq 1) {
$name = "$VMName"
$locname = "$Location"
Write-Output $locname
}
else {
$name = "$VMName" + "00" + "$_"
$locname = "Location00" + "$_"
Write-Output (get-variable $locname).value
}
}
对组或列表中的每个值使用单独的变量有点反模式,您将希望将它们全部放在一个数组中:
# Define array of possible locations
# `$Locations[0]` will resolve to `westus2`
# `$Locations[1]` will resolve to `westeurope`, etc.
$Locations = @(
"westus2"
"westeurope"
"eastasia"
)
[int]$VMCount = Read-Host "How many VMs?"
1..$VMCount | ForEach-Object {
# Define the VM name
$name = "VirtualMachine$_"
# Pick next location from the $Locations array
# the % ensures we "wrap around" when we reach the end
$location = $Locations[($_ - 1) % $Locations.Length]
# Output a new object with Name + Chosen Location
[pscustomobject]@{
VMName = $name
Location = $location
}
}
3个虚拟机的输出:
How many VMs?: 3
VMName Location
------ --------
VirtualMachine1 westus2
VirtualMachine2 westeurope
VirtualMachine3 eastasia
对于初学者来说,您没有$Location003
,这只是我编辑代码的一次失误。我想要的是返回$Location002和$Location003的内容以及我创建的循环谢谢兄弟这是一个更好的方法谢谢兄弟
# Define array of possible locations
# `$Locations[0]` will resolve to `westus2`
# `$Locations[1]` will resolve to `westeurope`, etc.
$Locations = @(
"westus2"
"westeurope"
"eastasia"
)
[int]$VMCount = Read-Host "How many VMs?"
1..$VMCount | ForEach-Object {
# Define the VM name
$name = "VirtualMachine$_"
# Pick next location from the $Locations array
# the % ensures we "wrap around" when we reach the end
$location = $Locations[($_ - 1) % $Locations.Length]
# Output a new object with Name + Chosen Location
[pscustomobject]@{
VMName = $name
Location = $location
}
}
How many VMs?: 3
VMName Location
------ --------
VirtualMachine1 westus2
VirtualMachine2 westeurope
VirtualMachine3 eastasia