新建一个vmlist.txt
1 Ubuntu_32 192.168.71.138
2 CentOS7 192.168.71.141
使用Get-Content来扫描这个List
$file=Get-Content C:\Users\raynorli\Desktop\vmlist.txt
并打印到屏幕上
foreach($hostip in $file)
{
$hostip
#print all row vm information
}
此时执行起来的效果为
PS C:\Users\raynorli> C:\Users\raynorli\Desktop\list4.ps1
1 Ubuntu_32 192.168.71.138
2 CentOS7 192.168.71.141
我们想要实现的效果是,输入“1”的时候登陆到Ubuntu_32这个虚机,输入“2”的时候登陆到CentOS7这个虚机
$name=Read-Host "Choose Your VM to LoGin"
#Input a seq to choose a VM
if ($name -eq 1)
{
$IP=((Get-Content C:\Users\raynorli\Desktop\vmlist.txt -First 1) -split " ")[2]
}
if ($name -ne 1)
{
$IP=((Get-Content C:\Users\raynorli\Desktop\vmlist.txt -TotalCount $name)[-1] -split " " )[2]
}
C:\Users\raynorli\Desktop\putty.exe root@$IP -pw Citrix123 #Login action
Done