我从scvmm获取VM的数据,如名称和VMhost,并存储到变量$ a
我有VM名称ex:win10-jde我想通过使用if语句获取win10-jde名称和vmhost详细信息它没有任何帮助
$b = "win10-jde"
$a = Get-SCVirtualMachine | Select-Object name, vmhost
if ($b -match $a){
Write-Output "$b"
}
$a
输出为:
PS C:\Users\scvmmadmin> $a
Name VMHost
---- ------
ks-finaltest slcae661.us.oracle.com
slc12vbv slcaf782.us.oracle.com
slc12vbw slcap322.us.oracle.com
iSCSItestK-4 slcae212.us.oracle.com
slc12uzz slcae661.us.oracle.com
slc12uzs slcae661.us.oracle.com
Test_Stage01 slcae212.us.oracle.com
slc12vbi slcae662.us.oracle.com
slc12vca slcaf782.us.oracle.com
slc12vaa slcae660.us.oracle.com
slc12vau slcap323.us.oracle.com
slc12uzu slcae661.us.oracle.com
slc12vbz slcac038.us.oracle.com
slc12uys slcae212.us.oracle.com
1511Test_01 slcae662.us.oracle.com
slc12vbk slcac038.us.oracle.com
slc12vbq slcac038.us.oracle.com
slc12vbm slcap324.us.oracle.com
slc12vat slcap322.us.oracle.com
iSCSITestG-2 slcae212.us.oracle.com
slc12uyx slcap323.us.oracle.com
slc12vbu slcac038.us.oracle.com
slc12vcb slcae662.us.oracle.com
slc12vas slcae212.us.oracle.com
slc12uyl slcae212.us.oracle.com
Win2016-JDE slcac038.us.oracle.com
slc12vab slcae660.us.oracle.com
slc12vac slcap323.us.oracle.com
Win10JDE-Bare slcac038.us.oracle.com
slc12vcc slcaf782.us.oracle.com
JOuten-WorkStation (Do Not Delete) slcap322.us.oracle.com
Win10-JDE slcac038.us.oracle.com
slc12uyw slcaf782.us.oracle.com
slc12uza slcae212.us.oracle.com
slc12vag slcac038.us.oracle.com
slc12uyv slcae662.us.oracle.com
slc12uyt slcap322.us.oracle.com
Win10JDE-Bare slcac038.us.oracle.com
iSCSItestD-3 slcae212.us.oracle.com
slc12vax-original slcap322.us.oracle.com
2012x64Test slcaf782.us.oracle.com
slc12uyu slcaf782.us.oracle.com
slc12vbf slcap324.us.oracle.com
iSCSItestK-2 slcae212.us.oracle.com
iSCSItestF-1 slcae212.us.oracle.com
slc12vas slcae212.us.oracle.com
Clone_Win2012R2BareOS-May-2017 slcae212.us.oracle.com
slc12vao slcac038.us.oracle.com
slc12uzb slcap322.us.oracle.com
slc12var slcae212.us.oracle.com
1511Test_02 slcae212.us.oracle.com
Win10-JDE slcac038.us.oracle.com
slc12vbl slcae662.us.oracle.com
答案 0 :(得分:0)
您需要通过foreach循环运行包含VM详细信息的对象。而且,我会使用-eq而不是-match,因为-match用于匹配正则表达式。更多关于以下内容...尝试这样的事情:
$b = "win10-jde"
$VMs = Get-SCVirtualMachine | Select-Object name, vmhost
ForEach ($VM in $VMs)
{
$Name = $VM.Name
$VMHost = $VM.vmhost
if ($b -eq $Name)
{
Write-Output "$VM"
}
}
现在,-match运算符。一个变量将匹配"另一个变量,如果它等于或包含在所比较的变量中。例如。 "蓝色"将匹配"蓝鲸"。比较运算符-eq要求一个变量与要比较的变量相同,以便比较为真。
我希望这有帮助!
答案 1 :(得分:0)
在这里,您尝试将字符串$b
与对象$a
进行比较,该对象永远不会返回true
。您只需将$b
与$a.name
进行比较即可。请更改您的代码。
$b = "win10-jde"
$a = Get-SCVirtualMachine | Select-Object name, vmhost
foreach($vm in $a)
{
if ($b -eq $vm.name)
{
Write-Output "$vm"
}
}