我正在制作一个用户输入驱动的部署脚本。基本上,我是在问用户“您想部署多少个VM”,并取决于我要创建和使用这么多变量的答案。
因此,我的代码当前要求用户输入虚拟机的数量 然后循环循环以创建一个新变量,并要求用户填充该变量,但是我不知道如何使用所创建的变量。
$Vmnumber = Read-Host "Enter number iof VM's that you would like to create."
for($i = 1; $i -le $Vmnumber; $i++){
New-Variable -Name "vmip$i"
Write-Host "Please Enter IP Address for VM$i (Site A): " -ForegroundColor Yellow -NoNewline
do {
if(($vmip + $i = Read-Host "Press enter to accept default IP form PHPIPAM" $SiteAsorting[$i]"or type a differnet one.") -eq ''){$vmip + $i = $SiteAsorting[$1]}else{$vmip + $i}
$ok = $vmip + $1 -match $IPPattern
if ($ok -eq $false) {
Write-Warning ("'{0}' is not an IP address." -f $vmip + $i)
write-host -fore Yellow "Please Enter IP for VM$i : " -NoNewline
}
} until ( $ok )
}
'''
我希望代码创建新变量并要求用户输入每次迭代。
在此先感谢您的帮助
答案 0 :(得分:2)
我不知道如何使用创建的变量。
这很有意义,解决方案很简单:
请改用数组或字典/哈希表!由于在您的示例中,您要以1开头的VM命名虚拟机,以0开头的数组索引命名,因此我们使用哈希表代替:
# First, let's ensure no one enters a nonsensical value like "100000" or "-3" or "ur mom"
while(([int]$numberOfVMs = Read-Host "Enter number of VM's that you would like to create.") -notin 1..20){
Write-Error "Please enter a number between 1 and 20"
}
# Create a hashtable to hold the IP addresses
$VMIPs = @{}
foreach($VMNumber in 1..$numberOfVMs){
do {
Write-Host "Please Enter IP Address for VM${VMNumber} (Site A): " -ForegroundColor Magenta -NoNewline
# Read whatever the user suggests
$suggestedIP = Read-Host "Press enter to accept default IP form PHPIPAM ($($SiteAsorting[$VMNumber]) or type a differnet one."
# Handle the default
if ($suggestedIP -eq '') {
$suggestedIP = $SiteAsorting[$VMNumber]
}
# Now, let's do our validation on the IP (regex is a poor validation tool here, but you can update this later)
$ok = $suggestedIP -match $IPPattern
if (-not $ok) {
# Warn the user that the IP wasn't valid
Write-Warning "${suggestedIP} is not a valid IP address!"
}
else {
# or, store the IP for later reference
$VMIPs["VM${VMNumber}"] = $suggestedIP
}
} until ( $ok )
}
请注意,我们在上面的哈希表键中使用的字符串值:"VM${VMNumber}"
-因此,现在,如果我在第一个提示中输入了3
,我们应该期望哈希表包含键{{1} },VM1
和VM2
,因此以后我们可以这样做:
VM3
答案 1 :(得分:0)
rootdir: python, inifile: pytest.ini, testpaths: test_cases
plugins: metadata-1.8.0, html-1.22.0, sugar-0.9.2, timeout-1.3.3, forked-1.0.2, xdist-1.29.0, repeat-0.8.0
timeout: 3600.0s
timeout method: signal
timeout func_only: False
gw0 ok / gw1 ok / gw2 ok
../python-venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../python-venv/lib/python2.7/site-packages/_pytest/python.py:234: in pytest_pycollect_makeitem
res = list(collector._genfunctions(name, obj))
../python-venv/lib/python2.7/site-packages/_pytest/python.py:410: in _genfunctions
self.ihook.pytest_generate_tests(metafunc=metafunc)
../python-venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../../modules/test/python/test_cases/uts_plugin.py:20: in pytest_generate_tests
for build_config in metafunc.config.test_system.build_configuration.get(
E AttributeError: 'Config' object has no attribute 'test_system'
gw0 [1] / gw1 ok / gw2 ok